Convert env to a defaultdict in run_executable() to fix other callers of that function.
[chromium-blink-merge.git] / extensions / shell / renderer / shell_content_renderer_client.cc
blobc6504210aa52a4262716b548fc2852f47de7d66c
1 // Copyright 2014 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/shell/renderer/shell_content_renderer_client.h"
7 #include "content/public/renderer/render_frame.h"
8 #include "content/public/renderer/render_frame_observer.h"
9 #include "content/public/renderer/render_frame_observer_tracker.h"
10 #include "content/public/renderer/render_thread.h"
11 #include "extensions/common/extensions_client.h"
12 #include "extensions/renderer/default_dispatcher_delegate.h"
13 #include "extensions/renderer/dispatcher.h"
14 #include "extensions/renderer/extension_helper.h"
15 #include "extensions/renderer/guest_view/guest_view_container.h"
16 #include "extensions/shell/common/shell_extensions_client.h"
17 #include "extensions/shell/renderer/shell_extensions_renderer_client.h"
18 #include "third_party/WebKit/public/web/WebLocalFrame.h"
20 #if !defined(DISABLE_NACL)
21 #include "components/nacl/common/nacl_constants.h"
22 #include "components/nacl/renderer/nacl_helper.h"
23 #include "components/nacl/renderer/ppb_nacl_private_impl.h"
24 #include "ppapi/c/private/ppb_nacl_private.h"
25 #endif
27 using blink::WebFrame;
28 using blink::WebString;
29 using content::RenderThread;
31 namespace extensions {
33 namespace {
35 // TODO: promote ExtensionFrameHelper to a common place and share with this.
36 class ShellFrameHelper
37 : public content::RenderFrameObserver,
38 public content::RenderFrameObserverTracker<ShellFrameHelper> {
39 public:
40 ShellFrameHelper(content::RenderFrame* render_frame,
41 Dispatcher* extension_dispatcher);
42 virtual ~ShellFrameHelper();
44 // RenderFrameObserver implementation.
45 virtual void WillReleaseScriptContext(v8::Handle<v8::Context>,
46 int world_id) override;
48 private:
49 Dispatcher* extension_dispatcher_;
51 DISALLOW_COPY_AND_ASSIGN(ShellFrameHelper);
54 ShellFrameHelper::ShellFrameHelper(content::RenderFrame* render_frame,
55 Dispatcher* extension_dispatcher)
56 : content::RenderFrameObserver(render_frame),
57 content::RenderFrameObserverTracker<ShellFrameHelper>(render_frame),
58 extension_dispatcher_(extension_dispatcher) {
61 ShellFrameHelper::~ShellFrameHelper() {
64 void ShellFrameHelper::WillReleaseScriptContext(v8::Handle<v8::Context> context,
65 int world_id) {
66 extension_dispatcher_->WillReleaseScriptContext(
67 render_frame()->GetWebFrame(), context, world_id);
70 } // namespace
72 ShellContentRendererClient::ShellContentRendererClient() {
75 ShellContentRendererClient::~ShellContentRendererClient() {
78 void ShellContentRendererClient::RenderThreadStarted() {
79 RenderThread* thread = RenderThread::Get();
81 extensions_client_.reset(new ShellExtensionsClient);
82 ExtensionsClient::Set(extensions_client_.get());
84 extensions_renderer_client_.reset(new ShellExtensionsRendererClient);
85 ExtensionsRendererClient::Set(extensions_renderer_client_.get());
87 extension_dispatcher_delegate_.reset(new DefaultDispatcherDelegate());
89 // Must be initialized after ExtensionsRendererClient.
90 extension_dispatcher_.reset(
91 new Dispatcher(extension_dispatcher_delegate_.get()));
92 thread->AddObserver(extension_dispatcher_.get());
94 // TODO(jamescook): Init WebSecurityPolicy for chrome-extension: schemes.
95 // See ChromeContentRendererClient for details.
98 void ShellContentRendererClient::RenderFrameCreated(
99 content::RenderFrame* render_frame) {
100 // ShellFrameHelper destroys itself when the RenderFrame is destroyed.
101 new ShellFrameHelper(render_frame, extension_dispatcher_.get());
103 // TODO(jamescook): Do we need to add a new PepperHelper(render_frame) here?
104 // It doesn't seem necessary for either Pepper or NaCl.
105 // http://crbug.com/403004
106 #if !defined(DISABLE_NACL)
107 new nacl::NaClHelper(render_frame);
108 #endif
111 void ShellContentRendererClient::RenderViewCreated(
112 content::RenderView* render_view) {
113 new ExtensionHelper(render_view, extension_dispatcher_.get());
114 extension_dispatcher_->OnRenderViewCreated(render_view);
117 bool ShellContentRendererClient::OverrideCreatePlugin(
118 content::RenderFrame* render_frame,
119 blink::WebLocalFrame* frame,
120 const blink::WebPluginParams& params,
121 blink::WebPlugin** plugin) {
122 // Allow the content module to create the plugin.
123 return false;
126 blink::WebPlugin* ShellContentRendererClient::CreatePluginReplacement(
127 content::RenderFrame* render_frame,
128 const base::FilePath& plugin_path) {
129 // Don't provide a custom "failed to load" plugin.
130 return NULL;
133 bool ShellContentRendererClient::WillSendRequest(
134 blink::WebFrame* frame,
135 ui::PageTransition transition_type,
136 const GURL& url,
137 const GURL& first_party_for_cookies,
138 GURL* new_url) {
139 // TODO(jamescook): Cause an error for bad extension scheme requests?
140 return false;
143 void ShellContentRendererClient::DidCreateScriptContext(
144 WebFrame* frame,
145 v8::Handle<v8::Context> context,
146 int extension_group,
147 int world_id) {
148 extension_dispatcher_->DidCreateScriptContext(
149 frame, context, extension_group, world_id);
152 const void* ShellContentRendererClient::CreatePPAPIInterface(
153 const std::string& interface_name) {
154 #if !defined(DISABLE_NACL)
155 if (interface_name == PPB_NACL_PRIVATE_INTERFACE)
156 return nacl::GetNaClPrivateInterface();
157 #endif
158 return NULL;
161 bool ShellContentRendererClient::IsExternalPepperPlugin(
162 const std::string& module_name) {
163 #if !defined(DISABLE_NACL)
164 // TODO(bbudge) remove this when the trusted NaCl plugin has been removed.
165 // We must defer certain plugin events for NaCl instances since we switch
166 // from the in-process to the out-of-process proxy after instantiating them.
167 return module_name == nacl::kNaClPluginName;
168 #else
169 return false;
170 #endif
173 bool ShellContentRendererClient::ShouldEnableSiteIsolationPolicy() const {
174 // Extension renderers don't need site isolation.
175 return false;
178 content::BrowserPluginDelegate*
179 ShellContentRendererClient::CreateBrowserPluginDelegate(
180 content::RenderFrame* render_frame,
181 const std::string& mime_type) {
182 return new extensions::GuestViewContainer(render_frame, mime_type);
185 } // namespace extensions