Fix infinite recursion on hiding panel when created during fullscreen mode.
[chromium-blink-merge.git] / chrome / browser / nacl_host / nacl_browser_delegate_impl.cc
blobb30a3ae0789a1dfb82969b250b67b19187abd5e8
1 // Copyright 2013 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/nacl_host/nacl_browser_delegate_impl.h"
7 #include "base/path_service.h"
8 #include "base/strings/string_split.h"
9 #include "base/strings/string_util.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/component_updater/pnacl/pnacl_component_installer.h"
12 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/nacl_host/nacl_infobar_delegate.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/renderer_host/pepper/chrome_browser_pepper_host_factory.h"
16 #include "chrome/common/chrome_paths.h"
17 #include "chrome/common/chrome_paths_internal.h"
18 #include "chrome/common/chrome_version_info.h"
19 #include "chrome/common/logging_chrome.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "content/public/browser/render_frame_host.h"
22 #include "content/public/browser/site_instance.h"
23 #include "extensions/browser/extension_system.h"
24 #include "extensions/browser/info_map.h"
25 #include "extensions/browser/process_manager.h"
26 #include "extensions/common/constants.h"
27 #include "extensions/common/extension.h"
28 #include "extensions/common/manifest_handlers/shared_module_info.h"
29 #include "extensions/common/url_pattern.h"
30 #include "ppapi/c/private/ppb_nacl_private.h"
32 using extensions::SharedModuleInfo;
34 namespace {
36 // Handles an extension's NaCl process transitioning in or out of idle state by
37 // relaying the state to the extension's process manager.
39 // A NaCl instance, when active (making PPAPI calls or receiving callbacks),
40 // sends keepalive IPCs to the browser process BrowserPpapiHost at a throttled
41 // rate. The content::BrowserPpapiHost passes context information up to the
42 // chrome level NaClProcessHost where we use the instance's context to find the
43 // associated extension process manager.
45 // There is a 1:many relationship for extension:nacl-embeds, but only a
46 // 1:1 relationship for NaClProcessHost:PP_Instance. The content layer doesn't
47 // rely on this knowledge because it routes messages for ppapi non-nacl
48 // instances as well, though they won't have callbacks set. Here the 1:1
49 // assumption is made and DCHECKed.
50 void OnKeepaliveOnUIThread(
51 const content::BrowserPpapiHost::OnKeepaliveInstanceData& instance_data,
52 const base::FilePath& profile_data_directory) {
53 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
55 // Only one instance will exist for NaCl embeds, even when more than one
56 // embed of the same plugin exists on the same page.
57 DCHECK(instance_data.size() == 1);
58 if (instance_data.size() < 1)
59 return;
61 content::RenderFrameHost* render_frame_host =
62 content::RenderFrameHost::FromID(
63 instance_data[0].render_process_id, instance_data[0].render_frame_id);
64 if (!render_frame_host)
65 return;
67 content::SiteInstance* site_instance = render_frame_host->GetSiteInstance();
68 if (!site_instance)
69 return;
71 extensions::ExtensionSystem* extension_system =
72 extensions::ExtensionSystem::Get(site_instance->GetBrowserContext());
73 if (!extension_system)
74 return;
76 const ExtensionService* extension_service =
77 extension_system->extension_service();
78 if (!extension_service)
79 return;
81 const extensions::Extension* extension = extension_service->GetExtensionById(
82 instance_data[0].document_url.host(), false);
83 if (!extension)
84 return;
86 extensions::ProcessManager* pm = extension_system->process_manager();
87 if (!pm)
88 return;
90 pm->KeepaliveImpulse(extension);
93 // Calls OnKeepaliveOnUIThread on UI thread.
94 void OnKeepalive(
95 const content::BrowserPpapiHost::OnKeepaliveInstanceData& instance_data,
96 const base::FilePath& profile_data_directory) {
97 DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
98 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
99 base::Bind(&OnKeepaliveOnUIThread,
100 instance_data,
101 profile_data_directory));
104 } // namespace
106 NaClBrowserDelegateImpl::NaClBrowserDelegateImpl(
107 extensions::InfoMap* extension_info_map)
108 : extension_info_map_(extension_info_map), inverse_debug_patterns_(false) {}
110 NaClBrowserDelegateImpl::~NaClBrowserDelegateImpl() {
113 void NaClBrowserDelegateImpl::ShowMissingArchInfobar(int render_process_id,
114 int render_view_id) {
115 content::BrowserThread::PostTask(
116 content::BrowserThread::UI, FROM_HERE,
117 base::Bind(&NaClInfoBarDelegate::Create, render_process_id,
118 render_view_id));
121 bool NaClBrowserDelegateImpl::DialogsAreSuppressed() {
122 return logging::DialogsAreSuppressed();
125 bool NaClBrowserDelegateImpl::GetCacheDirectory(base::FilePath* cache_dir) {
126 base::FilePath user_data_dir;
127 if (!PathService::Get(chrome::DIR_USER_DATA, &user_data_dir))
128 return false;
129 chrome::GetUserCacheDirectory(user_data_dir, cache_dir);
130 return true;
133 bool NaClBrowserDelegateImpl::GetPluginDirectory(base::FilePath* plugin_dir) {
134 return PathService::Get(chrome::DIR_INTERNAL_PLUGINS, plugin_dir);
137 bool NaClBrowserDelegateImpl::GetPnaclDirectory(base::FilePath* pnacl_dir) {
138 return PathService::Get(chrome::DIR_PNACL_COMPONENT, pnacl_dir);
141 bool NaClBrowserDelegateImpl::GetUserDirectory(base::FilePath* user_dir) {
142 return PathService::Get(chrome::DIR_USER_DATA, user_dir);
145 std::string NaClBrowserDelegateImpl::GetVersionString() const {
146 return chrome::VersionInfo().CreateVersionString();
149 ppapi::host::HostFactory* NaClBrowserDelegateImpl::CreatePpapiHostFactory(
150 content::BrowserPpapiHost* ppapi_host) {
151 return new chrome::ChromeBrowserPepperHostFactory(ppapi_host);
154 void NaClBrowserDelegateImpl::SetDebugPatterns(std::string debug_patterns) {
155 if (!debug_patterns.empty() && debug_patterns[0] == '!') {
156 inverse_debug_patterns_ = true;
157 debug_patterns.erase(0, 1);
159 if (debug_patterns.empty()) {
160 return;
162 std::vector<std::string> patterns;
163 base::SplitString(debug_patterns, ',', &patterns);
164 for (std::vector<std::string>::iterator iter = patterns.begin();
165 iter != patterns.end(); ++iter) {
166 // Allow chrome:// schema, which is used to filter out the internal
167 // PNaCl translator. Also allow chrome-extension:// schema (which
168 // can have NaCl modules). The default is to disallow these schema
169 // since they can be dangerous in the context of chrome extension
170 // permissions, but they are okay here, for NaCl GDB avoidance.
171 URLPattern pattern(URLPattern::SCHEME_ALL);
172 if (pattern.Parse(*iter) == URLPattern::PARSE_SUCCESS) {
173 // If URL pattern has scheme equal to *, Parse method resets valid
174 // schemes mask to http and https only, so we need to reset it after
175 // Parse to re-include chrome-extension and chrome schema.
176 pattern.SetValidSchemes(URLPattern::SCHEME_ALL);
177 debug_patterns_.push_back(pattern);
182 bool NaClBrowserDelegateImpl::URLMatchesDebugPatterns(
183 const GURL& manifest_url) {
184 // Empty patterns are forbidden so we ignore them.
185 if (debug_patterns_.empty()) {
186 return true;
188 bool matches = false;
189 for (std::vector<URLPattern>::iterator iter = debug_patterns_.begin();
190 iter != debug_patterns_.end(); ++iter) {
191 if (iter->MatchesURL(manifest_url)) {
192 matches = true;
193 break;
196 if (inverse_debug_patterns_) {
197 return !matches;
198 } else {
199 return matches;
203 // This function is security sensitive. Be sure to check with a security
204 // person before you modify it.
205 bool NaClBrowserDelegateImpl::MapUrlToLocalFilePath(
206 const GURL& file_url, bool use_blocking_api, base::FilePath* file_path) {
207 DCHECK(extension_info_map_);
208 // Check that the URL is recognized by the extension system.
209 const extensions::Extension* extension =
210 extension_info_map_->extensions().GetExtensionOrAppByURL(file_url);
211 if (!extension)
212 return false;
214 // This is a short-cut which avoids calling a blocking file operation
215 // (GetFilePath()), so that this can be called on the IO thread. It only
216 // handles a subset of the urls.
217 if (!use_blocking_api) {
218 if (file_url.SchemeIs(extensions::kExtensionScheme)) {
219 std::string path = file_url.path();
220 base::TrimString(path, "/", &path); // Remove first slash
221 *file_path = extension->path().AppendASCII(path);
222 return true;
224 return false;
227 std::string path = file_url.path();
228 extensions::ExtensionResource resource;
230 if (SharedModuleInfo::IsImportedPath(path)) {
231 // Check if this is a valid path that is imported for this extension.
232 std::string new_extension_id;
233 std::string new_relative_path;
234 SharedModuleInfo::ParseImportedPath(path, &new_extension_id,
235 &new_relative_path);
236 const extensions::Extension* new_extension =
237 extension_info_map_->extensions().GetByID(new_extension_id);
238 if (!new_extension)
239 return false;
241 if (!SharedModuleInfo::ImportsExtensionById(extension, new_extension_id) ||
242 !SharedModuleInfo::IsExportAllowed(new_extension, new_relative_path)) {
243 return false;
246 resource = new_extension->GetResource(new_relative_path);
247 } else {
248 // Check that the URL references a resource in the extension.
249 resource = extension->GetResource(path);
252 if (resource.empty())
253 return false;
255 // GetFilePath is a blocking function call.
256 const base::FilePath resource_file_path = resource.GetFilePath();
257 if (resource_file_path.empty())
258 return false;
260 *file_path = resource_file_path;
261 return true;
264 content::BrowserPpapiHost::OnKeepaliveCallback
265 NaClBrowserDelegateImpl::GetOnKeepaliveCallback() {
266 return base::Bind(&OnKeepalive);