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