app_window_custom_bindings.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "extensions/renderer/app_window_custom_bindings.h"
  5. #include "base/bind.h"
  6. #include "base/command_line.h"
  7. #include "content/public/renderer/render_frame.h"
  8. #include "content/public/renderer/render_thread.h"
  9. #include "content/public/renderer/v8_value_converter.h"
  10. #include "extensions/common/extension_messages.h"
  11. #include "extensions/common/switches.h"
  12. #include "extensions/grit/extensions_renderer_resources.h"
  13. #include "extensions/renderer/script_context.h"
  14. #include "third_party/blink/public/web/web_document_loader.h"
  15. #include "third_party/blink/public/web/web_local_frame.h"
  16. #include "third_party/blink/public/web/web_view.h"
  17. #include "ui/base/resource/resource_bundle.h"
  18. #include "v8/include/v8-function-callback.h"
  19. #include "v8/include/v8-isolate.h"
  20. #include "v8/include/v8-object.h"
  21. #include "v8/include/v8-primitive.h"
  22. namespace extensions {
  23. AppWindowCustomBindings::AppWindowCustomBindings(ScriptContext* context)
  24. : ObjectBackedNativeHandler(context) {}
  25. void AppWindowCustomBindings::AddRoutes() {
  26. RouteHandlerFunction("GetFrame",
  27. base::BindRepeating(&AppWindowCustomBindings::GetFrame,
  28. base::Unretained(this)));
  29. RouteHandlerFunction(
  30. "ResumeParser",
  31. base::BindRepeating(&AppWindowCustomBindings::ResumeParser,
  32. base::Unretained(this)));
  33. }
  34. void AppWindowCustomBindings::GetFrame(
  35. const v8::FunctionCallbackInfo<v8::Value>& args) {
  36. // TODO(jeremya): convert this to IDL nocompile to get validation, and turn
  37. // these argument checks into CHECK().
  38. if (args.Length() != 2)
  39. return;
  40. if (!args[0]->IsInt32() || !args[1]->IsBoolean())
  41. return;
  42. int frame_id = args[0].As<v8::Int32>()->Value();
  43. bool notify_browser = args[1].As<v8::Boolean>()->Value();
  44. if (frame_id == MSG_ROUTING_NONE)
  45. return;
  46. content::RenderFrame* app_frame =
  47. content::RenderFrame::FromRoutingID(frame_id);
  48. if (!app_frame)
  49. return;
  50. if (notify_browser) {
  51. app_frame->Send(
  52. new ExtensionHostMsg_AppWindowReady(app_frame->GetRoutingID()));
  53. }
  54. v8::Local<v8::Value> window =
  55. app_frame->GetWebFrame()->MainWorldScriptContext()->Global();
  56. // If the new window loads a sandboxed page and has started loading its
  57. // document, its security origin is unique and the background script is not
  58. // allowed accessing its window.
  59. v8::Local<v8::Context> caller_context =
  60. args.GetIsolate()->GetCurrentContext();
  61. if (!ContextCanAccessObject(caller_context,
  62. v8::Local<v8::Object>::Cast(window), true)) {
  63. return;
  64. }
  65. args.GetReturnValue().Set(window);
  66. }
  67. void AppWindowCustomBindings::ResumeParser(
  68. const v8::FunctionCallbackInfo<v8::Value>& args) {
  69. if (args.Length() != 1 || !args[0]->IsInt32()) {
  70. NOTREACHED();
  71. return;
  72. }
  73. int frame_id = args[0].As<v8::Int32>()->Value();
  74. content::RenderFrame* app_frame =
  75. content::RenderFrame::FromRoutingID(frame_id);
  76. if (!app_frame) {
  77. NOTREACHED();
  78. return;
  79. }
  80. blink::WebDocumentLoader* loader =
  81. app_frame->GetWebFrame()->GetDocumentLoader();
  82. if (!loader) {
  83. NOTREACHED();
  84. return;
  85. }
  86. loader->ResumeParser();
  87. }
  88. } // namespace extensions