Reland the ULONG -> SIZE_T change from 317177
[chromium-blink-merge.git] / extensions / renderer / app_window_custom_bindings.cc
blobf1a20f5dce763847888a4d139090f7a46daa2cb3
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/scoped_persistent.h"
19 #include "extensions/renderer/script_context.h"
20 #include "extensions/renderer/script_context_set.h"
21 #include "grit/extensions_renderer_resources.h"
22 #include "third_party/WebKit/public/web/WebLocalFrame.h"
23 #include "third_party/WebKit/public/web/WebView.h"
24 #include "ui/base/resource/resource_bundle.h"
25 #include "v8/include/v8.h"
27 namespace extensions {
29 class DidCreateDocumentElementObserver : public content::RenderViewObserver {
30 public:
31 DidCreateDocumentElementObserver(content::RenderView* view,
32 Dispatcher* dispatcher)
33 : content::RenderViewObserver(view), dispatcher_(dispatcher) {}
35 void DidCreateDocumentElement(blink::WebLocalFrame* frame) override {
36 DCHECK(frame);
37 DCHECK(dispatcher_);
38 // Don't attempt to inject the titlebar into iframes.
39 if (frame->parent())
40 return;
41 ScriptContext* script_context =
42 dispatcher_->script_context_set().GetByV8Context(
43 frame->mainWorldScriptContext());
44 if (!script_context)
45 return;
46 script_context->module_system()->CallModuleMethod(
47 "injectAppTitlebar", "didCreateDocumentElement");
50 private:
51 Dispatcher* dispatcher_;
54 AppWindowCustomBindings::AppWindowCustomBindings(Dispatcher* dispatcher,
55 ScriptContext* context)
56 : ObjectBackedNativeHandler(context), dispatcher_(dispatcher) {
57 RouteFunction("GetView",
58 base::Bind(&AppWindowCustomBindings::GetView,
59 base::Unretained(this)));
61 RouteFunction("GetWindowControlsHtmlTemplate",
62 base::Bind(&AppWindowCustomBindings::GetWindowControlsHtmlTemplate,
63 base::Unretained(this)));
66 void AppWindowCustomBindings::GetView(
67 const v8::FunctionCallbackInfo<v8::Value>& args) {
68 // TODO(jeremya): convert this to IDL nocompile to get validation, and turn
69 // these argument checks into CHECK().
70 if (args.Length() != 2)
71 return;
73 if (!args[0]->IsInt32())
74 return;
76 if (!args[1]->IsBoolean())
77 return;
79 int view_id = args[0]->Int32Value();
81 bool inject_titlebar = args[1]->BooleanValue();
83 if (view_id == MSG_ROUTING_NONE)
84 return;
86 content::RenderView* view = content::RenderView::FromRoutingID(view_id);
87 if (!view)
88 return;
90 if (inject_titlebar)
91 new DidCreateDocumentElementObserver(view, dispatcher_);
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
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.
97 content::RenderView* render_view = context()->GetRenderView();
98 if (!render_view)
99 return;
100 blink::WebFrame* opener = render_view->GetWebView()->mainFrame();
101 blink::WebFrame* frame = view->GetWebView()->mainFrame();
102 frame->setOpener(opener);
103 content::RenderThread::Get()->Send(
104 new ExtensionHostMsg_ResumeRequests(view->GetRoutingID()));
106 v8::Local<v8::Value> window = frame->mainWorldScriptContext()->Global();
107 args.GetReturnValue().Set(window);
110 void AppWindowCustomBindings::GetWindowControlsHtmlTemplate(
111 const v8::FunctionCallbackInfo<v8::Value>& args) {
112 CHECK_EQ(args.Length(), 0);
114 v8::Handle<v8::Value> result = v8::String::Empty(args.GetIsolate());
115 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
116 switches::kEnableAppWindowControls)) {
117 base::StringValue value(
118 ResourceBundle::GetSharedInstance()
119 .GetRawDataResource(IDR_WINDOW_CONTROLS_TEMPLATE_HTML)
120 .as_string());
121 scoped_ptr<content::V8ValueConverter> converter(
122 content::V8ValueConverter::create());
123 result = converter->ToV8Value(&value, context()->v8_context());
125 args.GetReturnValue().Set(result);
128 } // namespace extensions