Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/renderer/extensions/app_window_custom_bindings.h" | 5 #include "chrome/renderer/extensions/app_window_custom_bindings.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "chrome/common/extensions/extension_action.h" | 10 #include "chrome/common/extensions/extension_action.h" |
| 11 #include "chrome/common/extensions/extension_messages.h" | 11 #include "chrome/common/extensions/extension_messages.h" |
| 12 #include "chrome/common/url_constants.h" | 12 #include "chrome/common/url_constants.h" |
| 13 #include "chrome/renderer/extensions/chrome_v8_context.h" | |
| 13 #include "chrome/renderer/extensions/dispatcher.h" | 14 #include "chrome/renderer/extensions/dispatcher.h" |
| 14 #include "chrome/renderer/extensions/extension_helper.h" | 15 #include "chrome/renderer/extensions/extension_helper.h" |
| 15 #include "content/public/renderer/render_thread.h" | 16 #include "content/public/renderer/render_thread.h" |
| 16 #include "content/public/renderer/render_view.h" | 17 #include "content/public/renderer/render_view.h" |
| 17 #include "grit/renderer_resources.h" | 18 #include "grit/renderer_resources.h" |
| 19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques t.h" | |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNavigationPolicy.h " | 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNavigationPolicy.h " |
| 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebWindowFeatures.h" | 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebWindowFeatures.h" |
| 22 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques t.h" | |
| 23 #include "v8/include/v8.h" | 24 #include "v8/include/v8.h" |
| 24 #include "webkit/glue/webkit_glue.h" | 25 #include "webkit/glue/webkit_glue.h" |
| 25 | 26 |
| 26 #include "content/public/renderer/render_view.h" | 27 #include "content/public/renderer/render_view.h" |
| 27 #include "content/public/renderer/render_view_visitor.h" | 28 #include "content/public/renderer/render_view_visitor.h" |
| 28 | 29 |
| 29 namespace extensions { | 30 namespace extensions { |
| 30 | 31 |
| 32 class DidCreateDocumentElementObserver : public content::RenderViewObserver { | |
| 33 public: | |
| 34 DidCreateDocumentElementObserver( | |
| 35 content::RenderView* view, Dispatcher* dispatcher, bool inject_titlebar) | |
| 36 : content::RenderViewObserver(view), dispatcher_(dispatcher), | |
| 37 inject_titlebar_(inject_titlebar) { | |
| 38 } | |
| 39 virtual void DidCreateDocumentElement(WebKit::WebFrame* frame) { | |
|
Mihai Parparita -not on Chrome
2012/08/30 01:13:47
Nit: OVERRIDE
jeremya
2012/08/30 02:21:02
Done.
| |
| 40 v8::HandleScope handle_scope; | |
| 41 DCHECK(frame); | |
| 42 DCHECK(dispatcher_); | |
| 43 ChromeV8Context* v8_context = | |
| 44 dispatcher_->v8_context_set().GetByV8Context( | |
| 45 frame->mainWorldScriptContext()); | |
| 46 | |
| 47 if (!v8_context) | |
| 48 return; | |
| 49 v8::Context::Scope context_scope(v8_context->v8_context()); | |
| 50 | |
| 51 v8::Local<v8::Value> inject_app_titlebar = | |
| 52 v8::Local<v8::Value>::New( | |
| 53 v8_context->module_system()->RequireForJsInner( | |
| 54 v8::String::New("injectAppTitlebar"))); | |
| 55 if (inject_app_titlebar.IsEmpty()) | |
| 56 return; | |
| 57 v8::Local<v8::Value> value = | |
| 58 v8::Local<v8::Object>::Cast(inject_app_titlebar)->Get( | |
|
abarth-chromium
2012/08/29 23:51:20
How do you know this case is legit? We should pro
jeremya
2012/08/30 02:21:02
Done.
| |
| 59 v8::String::New("didCreateDocumentElement")); | |
| 60 DCHECK(!value.IsEmpty() && value->IsFunction()); | |
|
abarth-chromium
2012/08/29 23:51:20
How do you know these conditions are met? These a
jeremya
2012/08/30 02:21:02
I believe the v8 extensions system is sufficient t
| |
| 61 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast(value); | |
| 62 v8::Handle<v8::Value> argv[1]; | |
| 63 argv[0] = v8::Boolean::New(inject_titlebar_); | |
| 64 frame->callFunctionEvenIfScriptDisabled(function, | |
| 65 v8::Object::New(), | |
| 66 arraysize(argv), | |
| 67 argv); | |
| 68 } | |
| 69 | |
| 70 private: | |
| 71 Dispatcher* dispatcher_; | |
| 72 bool inject_titlebar_; | |
| 73 }; | |
| 74 | |
| 31 AppWindowCustomBindings::AppWindowCustomBindings(Dispatcher* dispatcher) | 75 AppWindowCustomBindings::AppWindowCustomBindings(Dispatcher* dispatcher) |
| 32 : ChromeV8Extension(dispatcher) { | 76 : ChromeV8Extension(dispatcher) { |
| 33 RouteStaticFunction("GetView", &GetView); | 77 RouteFunction("GetView", |
| 78 base::Bind(&AppWindowCustomBindings::GetView, | |
| 79 base::Unretained(this))); | |
| 34 } | 80 } |
| 35 | 81 |
| 36 namespace { | 82 namespace { |
| 37 class FindViewByID : public content::RenderViewVisitor { | 83 class FindViewByID : public content::RenderViewVisitor { |
| 38 public: | 84 public: |
| 39 explicit FindViewByID(int route_id) : route_id_(route_id), view_(NULL) { | 85 explicit FindViewByID(int route_id) : route_id_(route_id), view_(NULL) { |
| 40 } | 86 } |
| 41 | 87 |
| 42 content::RenderView* view() { return view_; } | 88 content::RenderView* view() { return view_; } |
| 43 | 89 |
| 44 // Returns false to terminate the iteration. | 90 // Returns false to terminate the iteration. |
| 45 virtual bool Visit(content::RenderView* render_view) { | 91 virtual bool Visit(content::RenderView* render_view) { |
| 46 if (render_view->GetRoutingID() == route_id_) { | 92 if (render_view->GetRoutingID() == route_id_) { |
| 47 view_ = render_view; | 93 view_ = render_view; |
| 48 return false; | 94 return false; |
| 49 } | 95 } |
| 50 return true; | 96 return true; |
| 51 } | 97 } |
| 52 | 98 |
| 53 private: | 99 private: |
| 54 int route_id_; | 100 int route_id_; |
| 55 content::RenderView* view_; | 101 content::RenderView* view_; |
| 56 }; | 102 }; |
| 57 } // namespace | 103 } // namespace |
| 58 | 104 |
| 59 v8::Handle<v8::Value> AppWindowCustomBindings::GetView( | 105 v8::Handle<v8::Value> AppWindowCustomBindings::GetView( |
| 60 const v8::Arguments& args) { | 106 const v8::Arguments& args) { |
| 61 // TODO(jeremya): convert this to IDL nocompile to get validation, and turn | 107 // TODO(jeremya): convert this to IDL nocompile to get validation, and turn |
| 62 // these argument checks into CHECK(). | 108 // these argument checks into CHECK(). |
| 63 if (args.Length() != 1) | 109 if (args.Length() != 2) |
| 64 return v8::Undefined(); | 110 return v8::Undefined(); |
| 65 | 111 |
| 66 if (!args[0]->IsInt32()) | 112 if (!args[0]->IsInt32()) |
| 67 return v8::Undefined(); | 113 return v8::Undefined(); |
| 68 | 114 |
| 115 if (!args[1]->IsBoolean()) | |
| 116 return v8::Undefined(); | |
| 117 | |
| 69 int view_id = args[0]->Int32Value(); | 118 int view_id = args[0]->Int32Value(); |
| 70 | 119 |
| 120 bool inject_titlebar = args[1]->BooleanValue(); | |
| 121 | |
| 71 if (view_id == MSG_ROUTING_NONE) | 122 if (view_id == MSG_ROUTING_NONE) |
| 72 return v8::Undefined(); | 123 return v8::Undefined(); |
| 73 | 124 |
| 74 // TODO(jeremya): there exists a direct map of routing_id -> RenderView as | 125 // TODO(jeremya): there exists a direct map of routing_id -> RenderView as |
| 75 // ChildThread::current()->ResolveRoute(), but ResolveRoute returns an | 126 // ChildThread::current()->ResolveRoute(), but ResolveRoute returns an |
| 76 // IPC::Listener*, which RenderView doesn't inherit from (RenderViewImpl | 127 // IPC::Listener*, which RenderView doesn't inherit from (RenderViewImpl |
| 77 // does, indirectly, via RenderWidget, but the link isn't exposed outside of | 128 // does, indirectly, via RenderWidget, but the link isn't exposed outside of |
| 78 // content/.) | 129 // content/.) |
| 79 FindViewByID view_finder(view_id); | 130 FindViewByID view_finder(view_id); |
| 80 content::RenderView::ForEach(&view_finder); | 131 content::RenderView::ForEach(&view_finder); |
| 81 content::RenderView* view = view_finder.view(); | 132 content::RenderView* view = view_finder.view(); |
| 82 if (!view) | 133 if (!view) |
| 83 return v8::Undefined(); | 134 return v8::Undefined(); |
| 84 | 135 |
| 136 new DidCreateDocumentElementObserver(view, dispatcher(), inject_titlebar); | |
|
abarth-chromium
2012/08/29 23:51:20
I assume someone else manages the lifetime of this
jeremya
2012/08/30 02:21:02
See content/public/renderer/render_view_observer.h
| |
| 137 | |
| 85 // TODO(jeremya): it doesn't really make sense to set the opener here, but we | 138 // TODO(jeremya): it doesn't really make sense to set the opener here, but we |
| 86 // need to make sure the security origin is set up before returning the DOM | 139 // need to make sure the security origin is set up before returning the DOM |
| 87 // reference. A better way to do this would be to have the browser pass the | 140 // reference. A better way to do this would be to have the browser pass the |
| 88 // opener through so opener_id is set in RenderViewImpl's constructor. | 141 // opener through so opener_id is set in RenderViewImpl's constructor. |
| 89 content::RenderView* render_view = GetCurrentRenderView(); | 142 content::RenderView* render_view = GetCurrentRenderView(); |
| 90 if (!render_view) | 143 if (!render_view) |
| 91 return v8::Undefined(); | 144 return v8::Undefined(); |
| 92 WebKit::WebFrame* opener = render_view->GetWebView()->mainFrame(); | 145 WebKit::WebFrame* opener = render_view->GetWebView()->mainFrame(); |
| 93 WebKit::WebFrame* frame = view->GetWebView()->mainFrame(); | 146 WebKit::WebFrame* frame = view->GetWebView()->mainFrame(); |
| 94 frame->setOpener(opener); | 147 frame->setOpener(opener); |
| 95 content::RenderThread::Get()->Send( | 148 content::RenderThread::Get()->Send( |
| 96 new ExtensionHostMsg_ResumeRequests(view->GetRoutingID())); | 149 new ExtensionHostMsg_ResumeRequests(view->GetRoutingID())); |
| 97 | 150 |
| 98 v8::Local<v8::Value> window = frame->mainWorldScriptContext()->Global(); | 151 v8::Local<v8::Value> window = frame->mainWorldScriptContext()->Global(); |
| 99 return window; | 152 return window; |
| 100 } | 153 } |
| 101 | 154 |
| 102 } // namespace extensions | 155 } // namespace extensions |
| OLD | NEW |