【Chromium】sandboxed window问题记录
问题发现
在业务逻辑中发现有时使用chrome.app.window.create这个API创建出来的窗口无法使用其他的API,不仅其他chrome.app.window的API说window is undefined而且还有奇怪的警告和报错
Creating sandboxed window, it doesn't have access to the chrome.app API. The chrome.app.window.create callback will be called, but there will be no object provided for the sandboxed window.
Error handling response: TypeError: Cannot read property 'window' of undefined
ar extensions::app.window:149:49
第一个报警是sandboxed window的报警,提示当前创建的窗口加载的页面可能是一个sandboxed page
查询了官方文档,发现如果需要创建一个sandboxed window需要在chrome app的manifest文件中添加如下声明
"sandbox": {
"pages": ["index.html"]
},
但是我并没有添加类似的声明。最后发现是同时创建了两个id一样使用资源一样的窗口就会报这个错误。一开始认为是资源占用,于是我修改了两个窗口加载的资源
chrome.app.runtime.onLaunched.addListener(function() {
chrome.app.window.create("index.html", {id: "test"});
chrome.app.window.create("index2.html", {id: "test"});
});
仍然会出现上述报错,原因可能是出在同时创建了两个id一样的窗口了。而且出现这个错误的时候第二个窗口其实是创建不出来的。
第二次测试
如果不同时创建会出这个问题么?怀着疑问我做了第二次测试。
app window的创建是一个异步的过程,当窗口真正创建完成的时候会有一个回调函数。google官方也说明如果想要通过chrome.app.window.get(id)这个API通过id来获取窗口对象的话应该在回调内部执行,确保窗口已经完全创建完成。否则可能会拿到不完整的窗口对象。
于是测试代码被改成了这样
chrome.app.runtime.onLaunched.addListener(function() {
chrome.app.window.create("index.html", {id: "test"}, function(window) {
chrome.app.window.create("index2.html", {id: "test"});
});
});
此时不会再报错,但是第二个窗口依然不会被创建。
id是用来标识唯一窗口的,猜也猜得到如果创建两个id相同的窗口是会被拒绝的,但是控制台并没有类似的日志或者提示。
回到一开始的那个Error报错。指向了一个js文件的149行,查找了一下这个js是chrome的内部编译进去的js代码,于是我搞来了chromium的源码,看到了这个文件app_window_custom_bindings.js的149行。
if (windowParams.existingWindow) {
// Not creating a new window, but activating an existing one, so trigger
// callback with existing window and don't do anything else.
let windowResult = view ? view.chrome.app.window.current() : undefined;
maybeCallback(windowResult);
return;
}
和猜想的一样是已存在一个相同id的窗口的话是不允许创建新窗口的。同时也是let windowResult = view ? view.chrome.app.window.current() : undefined;这句代码报的错。而这个代码的下面就是那句报警
if (!view || !view.chrome.app) {
var sandbox_window_message = 'Creating sandboxed window, it doesn\'t ' +
'have access to the chrome.app API.';
if (callback) {
sandbox_window_message = sandbox_window_message +
' The chrome.app.window.create callback will be called, but ' +
'there will be no object provided for the sandboxed window.';
}
console.warn(sandbox_window_message);
maybeCallback(undefined);
return;
}
原因就是view.chrome.app这个东西是undefined,view这个对象是通过v8调用c++的接口拿到的let view = appWindowNatives.GetFrame(windowParams.frameId,true /* notifyBrowser */);
void AppWindowCustomBindings::GetFrame(
const v8::FunctionCallbackInfo<v8::Value>& args) {
// TODO(jeremya): convert this to IDL nocompile to get validation, and turn
// these argument checks into CHECK().
if (args.Length() != 2)
return;
if (!args[0]->IsInt32() || !args[1]->IsBoolean())
return;
int frame_id = args[0].As<v8::Int32>()->Value();
bool notify_browser = args[1].As<v8::Boolean>()->Value();
if (frame_id == MSG_ROUTING_NONE)
return;
content::RenderFrame* app_frame =
content::RenderFrame::FromRoutingID(frame_id);
if (!app_frame)
return;
if (notify_browser) {
app_frame->Send(
new ExtensionHostMsg_AppWindowReady(app_frame->GetRoutingID()));
}
v8::Local<v8::Value> window =
app_frame->GetWebFrame()->MainWorldScriptContext()->Global();
// If the new window loads a sandboxed page and has started loading its
// document, its security origin is unique and the background script is not
// allowed accessing its window.
v8::Local<v8::Context> caller_context =
args.GetIsolate()->GetCurrentContext();
if (!ContextCanAccessObject(caller_context,
v8::Local<v8::Object>::Cast(window), true)) {
return;
}
args.GetReturnValue().Set(window);
}
是最后一句返回了对象args.GetReturnValue().Set(window);,当在创建窗口完成之前就去获取view的话,此时的view是没有绑定app的接口的,所以是访问不到app接口的。这个绑定接口的操作在src/extensions/renderer/dispatcher.cc中
std::unique_ptr<ExtensionBindingsSystem> Dispatcher::CreateBindingsSystem(
std::unique_ptr<IPCMessageSender> ipc_sender) {
std::unique_ptr<ExtensionBindingsSystem> bindings_system;
if (base::FeatureList::IsEnabled(extensions_features::kNativeCrxBindings)) {
auto system =
std::make_unique<NativeExtensionBindingsSystem>(std::move(ipc_sender));
delegate_->InitializeBindingsSystem(this, system.get());
bindings_system = std::move(system);
} else {
bindings_system = std::make_unique<JsExtensionBindingsSystem>(
&source_map_, std::move(ipc_sender));
}
return bindings_system;
}
当这个函数的执行时机晚于获取view函数的执行时机的话,就会出现app undefined的报错
【Chromium】sandboxed window问题记录的更多相关文章
- 使用window.localStorage,window.localStorage记录点击次数
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- sublime window 配置记录 (转)
大家好,今天给大家分享一款编辑器:sublime text2 我用过很多编辑器,EditPlus.EmEditor.Notepad++.Notepad2.UltraEdit.Editra.Vim ...
- android应用开发之Window,View和WindowManager .
ViewManager vm = a.getWindowManager(); vm.add(view,l); window :一个抽象的窗口基类,控制顶层窗口的外观和行为.作为顶层窗口,可控制窗口背 ...
- chromium之task
// A task is a generic runnable thingy, usually used for running code on a // different thread or fo ...
- UI Framework-1: Browser Window
Browser Window The Chromium browser window is represented by several objects, some of which are incl ...
- I have Flash Player installed, but I am unable to view Flash content in Chromium. How do I enable Flash Player to view this content?
I have Flash Player installed, but I am unable to view Flash content in Chromium. How do I enable Fl ...
- 页面元素坐标和偏移(clientX/pageX/screenX/layerX/offsetWidth/scrollWidth/clientWidth等)相关整理
鼠标事件都是在特定位置发生的,我们可以通过event事件对象的各种属性来获得事件发生的坐标位置,有相对于视口的,有相对于整个文档的,同样页面元素的位置也有相对视口的,也有滚动后的,这些都比较容易混淆, ...
- Html定位精要
Html定位基础 Html的布局是文档流模型,块元素独占一行,内联元素并列一行. 相对定位 position: relative 相对于自己定位 不脱离文档流,元素原有位置被保留 绝对定位 posit ...
- Window7下vagrant的部署
1. 下载并安装VirtualBox 下载地址:https://www.virtualbox.org/wiki/Downloads,下载最新的安装包,接下来的安装步骤就是下一步下一步了,你懂的 ...
随机推荐
- 基于.NET的开源搜索引擎-DotLucene(2)
NLucene是将 Lucene 从 Java 移植到 .NET 的一个 SourceForge 项目,它从 Lucene 1.2 版本转化而来. Lucene.Net因为 NLucene 项目到20 ...
- [VB6.0-->VB.NET]关于VB6.0升级到VB.NET的微软官方文档
升级流程大体是这样的: 1.用VS2008打开Vb6.0的工程(此时针对语言层面自动升级). 注: VS更新多版了(当前最新VS2017),用最新版再打开2008升级后的工程的时候还是会有自动升级,相 ...
- Linux nl --让输出的文件内容自动加上行号
nl命令在linux系统中用来计算文件中行号.nl 可以将输出的文件内容自动的加上行号!其默认的结果与 cat -n 有点不太一样, nl 可以将行号做比较多的显示设计,包括位数与是否自动补齐 0 等 ...
- jscover使用说明-总体说明
1.总体说明 这个文档现在是完善和准确的,不管怎样,尽量去参考JSCoverage documentation. 1.1.介绍 JSCove是一个用来显示JavaScript项目代码覆盖率的工具,它是 ...
- Python-Django编程问题汇总
OS:Windows10 64 IDE:JetBrain Python Community Edition 2017.3.4 Python:python-3.6.4 Django:V2.0.3 问题一 ...
- User类 新增共有属性Current ID
一.题目描述 每个用户有用户编号(id),用户名(name),密码(passwd)三个属性.其中: 用户编号(id)由系统自动顺序编号,用户名和密码都是字母.数字.符合的组合,新用户密码,默认“111 ...
- 01-django项目环境搭建
一.Web应用框架----Django http服务器:用来接受用户请求,并将请求转发给web应用框架进行处理. Web应用框架处理完以后再发送给http服务器,http服务器再返回给用户 二.工具准 ...
- 【HNOI2019】部分题简要题解
题意懒得写了 LOJ Day 1 T1 鱼 个人做法比较猎奇,如果有哪位大佬会证明能分享一下的话感激不尽. 题解:枚举鱼尾和鱼身的交点D,将所有其他点按照到D的距离排序,距离相同的分一组. 感性的理解 ...
- hdu1024 Max Sum Plus Plus 滚动dp
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- CSS动态控制DIV居中
1.所谓的动态:就是即使手动去拖拉浏览器,DIV还是会自动居中 2.之前一直以为这个事情是JavaScript做的, 步骤:通过先获取页面的Height和Width, 然后定义DIV的Height和W ...