Fix build break
[chromium-blink-merge.git] / chrome / browser / renderer_host / chrome_render_view_host_observer.cc
blob199d8d41c74d94aa94952db229c0b163c85a9ef3
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 "chrome/browser/renderer_host/chrome_render_view_host_observer.h"
7 #include "base/command_line.h"
8 #include "chrome/browser/extensions/extension_service.h"
9 #include "chrome/browser/extensions/extension_system.h"
10 #include "chrome/browser/net/predictor.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/common/chrome_notification_types.h"
13 #include "chrome/common/chrome_switches.h"
14 #include "chrome/common/extensions/extension.h"
15 #include "chrome/common/extensions/extension_messages.h"
16 #include "chrome/common/extensions/manifest.h"
17 #include "chrome/common/render_messages.h"
18 #include "chrome/common/url_constants.h"
19 #include "content/public/browser/child_process_security_policy.h"
20 #include "content/public/browser/notification_service.h"
21 #include "content/public/browser/render_process_host.h"
22 #include "content/public/browser/render_view_host.h"
23 #include "content/public/browser/site_instance.h"
24 #include "extensions/common/constants.h"
26 #if defined(OS_WIN)
27 #include "base/win/win_util.h"
28 #endif // OS_WIN
30 using content::ChildProcessSecurityPolicy;
31 using content::RenderViewHost;
32 using content::SiteInstance;
33 using extensions::Extension;
34 using extensions::Manifest;
36 ChromeRenderViewHostObserver::ChromeRenderViewHostObserver(
37 RenderViewHost* render_view_host, chrome_browser_net::Predictor* predictor)
38 : content::RenderViewHostObserver(render_view_host),
39 predictor_(predictor) {
40 SiteInstance* site_instance = render_view_host->GetSiteInstance();
41 profile_ = Profile::FromBrowserContext(
42 site_instance->GetBrowserContext());
44 InitRenderViewForExtensions();
47 ChromeRenderViewHostObserver::~ChromeRenderViewHostObserver() {
48 if (render_view_host())
49 RemoveRenderViewHostForExtensions(render_view_host());
52 void ChromeRenderViewHostObserver::RenderViewHostInitialized() {
53 // This reinitializes some state in the case where a render process crashes
54 // but we keep the same RenderViewHost instance.
55 InitRenderViewForExtensions();
58 void ChromeRenderViewHostObserver::RenderViewHostDestroyed(
59 RenderViewHost* rvh) {
60 RemoveRenderViewHostForExtensions(rvh);
61 delete this;
64 void ChromeRenderViewHostObserver::Navigate(const GURL& url) {
65 if (!predictor_)
66 return;
67 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kChromeFrame) &&
68 (url.SchemeIs(chrome::kHttpScheme) || url.SchemeIs(chrome::kHttpsScheme)))
69 predictor_->PreconnectUrlAndSubresources(url);
72 bool ChromeRenderViewHostObserver::OnMessageReceived(
73 const IPC::Message& message) {
74 bool handled = true;
75 IPC_BEGIN_MESSAGE_MAP(ChromeRenderViewHostObserver, message)
76 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_FocusedNodeTouched,
77 OnFocusedNodeTouched)
78 IPC_MESSAGE_UNHANDLED(handled = false)
79 IPC_END_MESSAGE_MAP()
80 return handled;
83 void ChromeRenderViewHostObserver::InitRenderViewForExtensions() {
84 const Extension* extension = GetExtension();
85 if (!extension)
86 return;
88 content::RenderProcessHost* process = render_view_host()->GetProcess();
90 // Some extensions use chrome:// URLs.
91 // This is a temporary solution. Replace it with access to chrome-static://
92 // once it is implemented. See: crbug.com/226927.
93 Manifest::Type type = extension->GetType();
94 if (type == Manifest::TYPE_EXTENSION ||
95 type == Manifest::TYPE_LEGACY_PACKAGED_APP ||
96 (type == Manifest::TYPE_PLATFORM_APP &&
97 extension->location() == Manifest::COMPONENT)) {
98 ChildProcessSecurityPolicy::GetInstance()->GrantScheme(
99 process->GetID(), chrome::kChromeUIScheme);
102 // Some extensions use file:// URLs.
103 if (type == Manifest::TYPE_EXTENSION ||
104 type == Manifest::TYPE_LEGACY_PACKAGED_APP) {
105 if (extensions::ExtensionSystem::Get(profile_)->extension_service()->
106 extension_prefs()->AllowFileAccess(extension->id())) {
107 ChildProcessSecurityPolicy::GetInstance()->GrantScheme(
108 process->GetID(), chrome::kFileScheme);
112 switch (type) {
113 case Manifest::TYPE_EXTENSION:
114 case Manifest::TYPE_USER_SCRIPT:
115 case Manifest::TYPE_HOSTED_APP:
116 case Manifest::TYPE_LEGACY_PACKAGED_APP:
117 case Manifest::TYPE_PLATFORM_APP:
118 // Always send a Loaded message before ActivateExtension so that
119 // ExtensionDispatcher knows what Extension is active, not just its ID.
120 // This is important for classifying the Extension's JavaScript context
121 // correctly (see ExtensionDispatcher::ClassifyJavaScriptContext).
122 Send(new ExtensionMsg_Loaded(
123 std::vector<ExtensionMsg_Loaded_Params>(
124 1, ExtensionMsg_Loaded_Params(extension))));
125 Send(new ExtensionMsg_ActivateExtension(extension->id()));
126 break;
128 case Manifest::TYPE_UNKNOWN:
129 case Manifest::TYPE_THEME:
130 break;
134 const Extension* ChromeRenderViewHostObserver::GetExtension() {
135 // Note that due to ChromeContentBrowserClient::GetEffectiveURL(), hosted apps
136 // (excluding bookmark apps) will have a chrome-extension:// URL for their
137 // site, so we can ignore that wrinkle here.
138 SiteInstance* site_instance = render_view_host()->GetSiteInstance();
139 const GURL& site = site_instance->GetSiteURL();
141 if (!site.SchemeIs(extensions::kExtensionScheme))
142 return NULL;
144 ExtensionService* service =
145 extensions::ExtensionSystem::Get(profile_)->extension_service();
146 if (!service)
147 return NULL;
149 // Reload the extension if it has crashed.
150 // TODO(yoz): This reload doesn't happen synchronously for unpacked
151 // extensions. It seems to be fast enough, but there is a race.
152 // We should delay loading until the extension has reloaded.
153 if (service->GetTerminatedExtension(site.host()))
154 service->ReloadExtension(site.host());
156 // May be null if the extension doesn't exist, for example if somebody typos
157 // a chrome-extension:// URL.
158 return service->extensions()->GetByID(site.host());
161 void ChromeRenderViewHostObserver::RemoveRenderViewHostForExtensions(
162 RenderViewHost* rvh) {
163 ExtensionProcessManager* process_manager =
164 extensions::ExtensionSystem::Get(profile_)->process_manager();
165 if (process_manager)
166 process_manager->UnregisterRenderViewHost(rvh);
169 void ChromeRenderViewHostObserver::OnFocusedNodeTouched(bool editable) {
170 if (editable) {
171 #if defined(OS_WIN)
172 base::win::DisplayVirtualKeyboard();
173 #endif
174 content::NotificationService::current()->Notify(
175 chrome::NOTIFICATION_FOCUSED_NODE_TOUCHED,
176 content::Source<RenderViewHost>(render_view_host()),
177 content::Details<bool>(&editable));
178 } else {
179 #if defined(OS_WIN)
180 base::win::DismissVirtualKeyboard();
181 #endif