码迷,mamicode.com
首页 > 数据库 > 详细

【Chromium】sandboxed window问题记录

时间:2019-03-16 11:07:18      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:unique   相同   sage   ati   star   绑定   业务逻辑   ble   man   

问题发现

在业务逻辑中发现有时使用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 appmanifest文件中添加如下声明

"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这个东西是undefinedview这个对象是通过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问题记录

标签:unique   相同   sage   ati   star   绑定   业务逻辑   ble   man   

原文地址:https://www.cnblogs.com/lenomirei/p/10541117.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!