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 "extensions/renderer/app_window_custom_bindings.h" | 5 #include "extensions/renderer/app_window_custom_bindings.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "content/public/child/v8_value_converter.h" | 10 #include "content/public/child/v8_value_converter.h" |
11 #include "content/public/renderer/render_frame.h" | 11 #include "content/public/renderer/render_frame.h" |
| 12 #include "content/public/renderer/render_frame_observer.h" |
12 #include "content/public/renderer/render_thread.h" | 13 #include "content/public/renderer/render_thread.h" |
13 #include "content/public/renderer/render_view.h" | 14 #include "content/public/renderer/render_view.h" |
14 #include "content/public/renderer/render_view_observer.h" | |
15 #include "content/public/renderer/render_view_visitor.h" | |
16 #include "extensions/common/extension_messages.h" | 15 #include "extensions/common/extension_messages.h" |
17 #include "extensions/common/switches.h" | 16 #include "extensions/common/switches.h" |
18 #include "extensions/renderer/dispatcher.h" | |
19 #include "extensions/renderer/script_context.h" | 17 #include "extensions/renderer/script_context.h" |
20 #include "extensions/renderer/script_context_set.h" | 18 #include "extensions/renderer/script_context_set.h" |
21 #include "grit/extensions_renderer_resources.h" | 19 #include "grit/extensions_renderer_resources.h" |
22 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 20 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
23 #include "third_party/WebKit/public/web/WebView.h" | 21 #include "third_party/WebKit/public/web/WebView.h" |
24 #include "ui/base/resource/resource_bundle.h" | 22 #include "ui/base/resource/resource_bundle.h" |
25 #include "v8/include/v8.h" | 23 #include "v8/include/v8.h" |
26 | 24 |
27 namespace extensions { | 25 namespace extensions { |
28 | 26 |
29 class DidCreateDocumentElementObserver : public content::RenderViewObserver { | 27 class DidCreateDocumentElementObserver : public content::RenderFrameObserver { |
30 public: | 28 public: |
31 DidCreateDocumentElementObserver(content::RenderView* view, | 29 DidCreateDocumentElementObserver(content::RenderFrame* frame, |
32 Dispatcher* dispatcher) | 30 const ScriptContextSet* script_context_set) |
33 : content::RenderViewObserver(view), dispatcher_(dispatcher) {} | 31 : content::RenderFrameObserver(frame), |
| 32 script_context_set_(script_context_set) { |
| 33 DCHECK(script_context_set_); |
| 34 } |
34 | 35 |
35 void DidCreateDocumentElement(blink::WebLocalFrame* frame) override { | 36 void DidCreateDocumentElement() override { |
36 DCHECK(frame); | 37 blink::WebLocalFrame* web_frame = render_frame()->GetWebFrame(); |
37 DCHECK(dispatcher_); | |
38 // Don't attempt to inject the titlebar into iframes. | 38 // Don't attempt to inject the titlebar into iframes. |
39 if (frame->parent()) | 39 if (web_frame->parent()) |
40 return; | 40 return; |
41 ScriptContext* script_context = | 41 ScriptContext* script_context = script_context_set_->GetByV8Context( |
42 dispatcher_->script_context_set().GetByV8Context( | 42 web_frame->mainWorldScriptContext()); |
43 frame->mainWorldScriptContext()); | |
44 if (!script_context) | 43 if (!script_context) |
45 return; | 44 return; |
46 script_context->module_system()->CallModuleMethod( | 45 script_context->module_system()->CallModuleMethod( |
47 "injectAppTitlebar", "didCreateDocumentElement"); | 46 "injectAppTitlebar", "didCreateDocumentElement"); |
48 } | 47 } |
49 | 48 |
50 private: | 49 private: |
51 Dispatcher* dispatcher_; | 50 const ScriptContextSet* script_context_set_; |
| 51 |
| 52 DISALLOW_COPY_AND_ASSIGN(DidCreateDocumentElementObserver); |
52 }; | 53 }; |
53 | 54 |
54 AppWindowCustomBindings::AppWindowCustomBindings(Dispatcher* dispatcher, | 55 AppWindowCustomBindings::AppWindowCustomBindings( |
55 ScriptContext* context) | 56 const ScriptContextSet* script_context_set, |
56 : ObjectBackedNativeHandler(context), dispatcher_(dispatcher) { | 57 ScriptContext* context) |
57 RouteFunction("GetView", | 58 : ObjectBackedNativeHandler(context), |
58 base::Bind(&AppWindowCustomBindings::GetView, | 59 script_context_set_(script_context_set) { |
59 base::Unretained(this))); | 60 RouteFunction("GetFrame", base::Bind(&AppWindowCustomBindings::GetFrame, |
| 61 base::Unretained(this))); |
60 | 62 |
61 RouteFunction("GetWindowControlsHtmlTemplate", | 63 RouteFunction("GetWindowControlsHtmlTemplate", |
62 base::Bind(&AppWindowCustomBindings::GetWindowControlsHtmlTemplate, | 64 base::Bind(&AppWindowCustomBindings::GetWindowControlsHtmlTemplate, |
63 base::Unretained(this))); | 65 base::Unretained(this))); |
64 } | 66 } |
65 | 67 |
66 void AppWindowCustomBindings::GetView( | 68 void AppWindowCustomBindings::GetFrame( |
67 const v8::FunctionCallbackInfo<v8::Value>& args) { | 69 const v8::FunctionCallbackInfo<v8::Value>& args) { |
68 // TODO(jeremya): convert this to IDL nocompile to get validation, and turn | 70 // TODO(jeremya): convert this to IDL nocompile to get validation, and turn |
69 // these argument checks into CHECK(). | 71 // these argument checks into CHECK(). |
70 if (args.Length() != 2) | 72 if (args.Length() != 2) |
71 return; | 73 return; |
72 | 74 |
73 if (!args[0]->IsInt32()) | 75 if (!args[0]->IsInt32()) |
74 return; | 76 return; |
75 | 77 |
76 if (!args[1]->IsBoolean()) | 78 if (!args[1]->IsBoolean()) |
77 return; | 79 return; |
78 | 80 |
79 int view_id = args[0]->Int32Value(); | 81 int frame_id = args[0]->Int32Value(); |
80 | 82 |
81 bool inject_titlebar = args[1]->BooleanValue(); | 83 bool inject_titlebar = args[1]->BooleanValue(); |
82 | 84 |
83 if (view_id == MSG_ROUTING_NONE) | 85 if (frame_id == MSG_ROUTING_NONE) |
84 return; | 86 return; |
85 | 87 |
86 content::RenderView* view = content::RenderView::FromRoutingID(view_id); | 88 content::RenderFrame* app_frame = |
87 if (!view) | 89 content::RenderFrame::FromRoutingID(frame_id); |
| 90 if (!app_frame) |
88 return; | 91 return; |
89 | 92 |
90 if (inject_titlebar) | |
91 new DidCreateDocumentElementObserver(view, dispatcher_); | |
92 | |
93 // TODO(jeremya): it doesn't really make sense to set the opener here, but we | 93 // TODO(jeremya): it doesn't really make sense to set the opener here, but we |
94 // need to make sure the security origin is set up before returning the DOM | 94 // need to make sure the security origin is set up before returning the DOM |
95 // reference. A better way to do this would be to have the browser pass the | 95 // reference. A better way to do this would be to have the browser pass the |
96 // opener through so opener_id is set in RenderViewImpl's constructor. | 96 // opener through so opener_id is set in RenderViewImpl's constructor. |
97 content::RenderFrame* render_frame = context()->GetRenderFrame(); | 97 content::RenderFrame* context_render_frame = context()->GetRenderFrame(); |
98 content::RenderView* render_view = | 98 if (!context_render_frame) |
99 render_frame ? render_frame->GetRenderView() : nullptr; | |
100 if (!render_view) | |
101 return; | 99 return; |
102 blink::WebFrame* opener = render_view->GetWebView()->mainFrame(); | 100 |
103 blink::WebFrame* frame = view->GetWebView()->mainFrame(); | 101 if (inject_titlebar) |
104 frame->setOpener(opener); | 102 new DidCreateDocumentElementObserver(app_frame, script_context_set_); |
| 103 |
| 104 blink::WebFrame* opener = context_render_frame->GetWebFrame(); |
| 105 blink::WebLocalFrame* app_web_frame = app_frame->GetWebFrame(); |
| 106 app_web_frame->setOpener(opener); |
105 content::RenderThread::Get()->Send( | 107 content::RenderThread::Get()->Send( |
106 new ExtensionHostMsg_ResumeRequests(view->GetRoutingID())); | 108 new ExtensionHostMsg_ResumeRequests(app_frame->GetRoutingID())); |
107 | 109 |
108 v8::Local<v8::Value> window = frame->mainWorldScriptContext()->Global(); | 110 v8::Local<v8::Value> window = |
| 111 app_web_frame->mainWorldScriptContext()->Global(); |
109 args.GetReturnValue().Set(window); | 112 args.GetReturnValue().Set(window); |
110 } | 113 } |
111 | 114 |
112 void AppWindowCustomBindings::GetWindowControlsHtmlTemplate( | 115 void AppWindowCustomBindings::GetWindowControlsHtmlTemplate( |
113 const v8::FunctionCallbackInfo<v8::Value>& args) { | 116 const v8::FunctionCallbackInfo<v8::Value>& args) { |
114 CHECK_EQ(args.Length(), 0); | 117 CHECK_EQ(args.Length(), 0); |
115 | 118 |
116 v8::Local<v8::Value> result = v8::String::Empty(args.GetIsolate()); | 119 v8::Local<v8::Value> result = v8::String::Empty(args.GetIsolate()); |
117 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 120 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
118 switches::kEnableAppWindowControls)) { | 121 switches::kEnableAppWindowControls)) { |
119 base::StringValue value( | 122 base::StringValue value( |
120 ResourceBundle::GetSharedInstance() | 123 ResourceBundle::GetSharedInstance() |
121 .GetRawDataResource(IDR_WINDOW_CONTROLS_TEMPLATE_HTML) | 124 .GetRawDataResource(IDR_WINDOW_CONTROLS_TEMPLATE_HTML) |
122 .as_string()); | 125 .as_string()); |
123 scoped_ptr<content::V8ValueConverter> converter( | 126 scoped_ptr<content::V8ValueConverter> converter( |
124 content::V8ValueConverter::create()); | 127 content::V8ValueConverter::create()); |
125 result = converter->ToV8Value(&value, context()->v8_context()); | 128 result = converter->ToV8Value(&value, context()->v8_context()); |
126 } | 129 } |
127 args.GetReturnValue().Set(result); | 130 args.GetReturnValue().Set(result); |
128 } | 131 } |
129 | 132 |
130 } // namespace extensions | 133 } // namespace extensions |
OLD | NEW |