Fix omnibox logging for zero suggest.
[chromium-blink-merge.git] / extensions / renderer / app_window_custom_bindings.cc
blob448a46c589a0b401f689636aff93368faa9fb3f0
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_frame.h"
12 #include "content/public/renderer/render_frame_observer.h"
13 #include "content/public/renderer/render_thread.h"
14 #include "content/public/renderer/render_view.h"
15 #include "extensions/common/extension_messages.h"
16 #include "extensions/common/switches.h"
17 #include "extensions/renderer/script_context.h"
18 #include "extensions/renderer/script_context_set.h"
19 #include "grit/extensions_renderer_resources.h"
20 #include "third_party/WebKit/public/web/WebLocalFrame.h"
21 #include "third_party/WebKit/public/web/WebView.h"
22 #include "ui/base/resource/resource_bundle.h"
23 #include "v8/include/v8.h"
25 namespace extensions {
27 class DidCreateDocumentElementObserver : public content::RenderFrameObserver {
28 public:
29 DidCreateDocumentElementObserver(content::RenderFrame* frame,
30 const ScriptContextSet* script_context_set)
31 : content::RenderFrameObserver(frame),
32 script_context_set_(script_context_set) {
33 DCHECK(script_context_set_);
36 void DidCreateDocumentElement() override {
37 blink::WebLocalFrame* web_frame = render_frame()->GetWebFrame();
38 // Don't attempt to inject the titlebar into iframes.
39 if (web_frame->parent())
40 return;
41 ScriptContext* script_context = script_context_set_->GetByV8Context(
42 web_frame->mainWorldScriptContext());
43 if (!script_context)
44 return;
45 script_context->module_system()->CallModuleMethod(
46 "injectAppTitlebar", "didCreateDocumentElement");
49 private:
50 const ScriptContextSet* script_context_set_;
52 DISALLOW_COPY_AND_ASSIGN(DidCreateDocumentElementObserver);
55 AppWindowCustomBindings::AppWindowCustomBindings(
56 const ScriptContextSet* script_context_set,
57 ScriptContext* context)
58 : ObjectBackedNativeHandler(context),
59 script_context_set_(script_context_set) {
60 RouteFunction("GetFrame", base::Bind(&AppWindowCustomBindings::GetFrame,
61 base::Unretained(this)));
63 RouteFunction("GetWindowControlsHtmlTemplate",
64 base::Bind(&AppWindowCustomBindings::GetWindowControlsHtmlTemplate,
65 base::Unretained(this)));
68 void AppWindowCustomBindings::GetFrame(
69 const v8::FunctionCallbackInfo<v8::Value>& args) {
70 // TODO(jeremya): convert this to IDL nocompile to get validation, and turn
71 // these argument checks into CHECK().
72 if (args.Length() != 2)
73 return;
75 if (!args[0]->IsInt32())
76 return;
78 if (!args[1]->IsBoolean())
79 return;
81 int frame_id = args[0]->Int32Value();
83 bool inject_titlebar = args[1]->BooleanValue();
85 if (frame_id == MSG_ROUTING_NONE)
86 return;
88 content::RenderFrame* app_frame =
89 content::RenderFrame::FromRoutingID(frame_id);
90 if (!app_frame)
91 return;
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::RenderFrame* context_render_frame = context()->GetRenderFrame();
98 if (!context_render_frame)
99 return;
101 if (inject_titlebar)
102 new DidCreateDocumentElementObserver(app_frame, script_context_set_);
104 blink::WebFrame* opener = context_render_frame->GetWebFrame();
105 blink::WebLocalFrame* app_web_frame = app_frame->GetWebFrame();
106 app_web_frame->setOpener(opener);
108 content::RenderThread::Get()->Send(new ExtensionHostMsg_AppWindowReady(
109 app_frame->GetRenderView()->GetRoutingID()));
111 v8::Local<v8::Value> window =
112 app_web_frame->mainWorldScriptContext()->Global();
113 args.GetReturnValue().Set(window);
116 void AppWindowCustomBindings::GetWindowControlsHtmlTemplate(
117 const v8::FunctionCallbackInfo<v8::Value>& args) {
118 CHECK_EQ(args.Length(), 0);
120 v8::Local<v8::Value> result = v8::String::Empty(args.GetIsolate());
121 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
122 switches::kEnableAppWindowControls)) {
123 base::StringValue value(
124 ResourceBundle::GetSharedInstance()
125 .GetRawDataResource(IDR_WINDOW_CONTROLS_TEMPLATE_HTML)
126 .as_string());
127 scoped_ptr<content::V8ValueConverter> converter(
128 content::V8ValueConverter::create());
129 result = converter->ToV8Value(&value, context()->v8_context());
131 args.GetReturnValue().Set(result);
134 } // namespace extensions