NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / nacl_host / nacl_browser_delegate_impl.cc
blobdec90e3a668236928dcd9cd49af868cde89926e9
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::ShowNaClInfobar(int render_process_id,
114 int render_view_id,
115 int error_id) {
116 DCHECK_EQ(PP_NACL_MANIFEST_MISSING_ARCH, error_id);
117 content::BrowserThread::PostTask(
118 content::BrowserThread::UI, FROM_HERE,
119 base::Bind(&NaClInfoBarDelegate::Create, render_process_id,
120 render_view_id));
123 bool NaClBrowserDelegateImpl::DialogsAreSuppressed() {
124 return logging::DialogsAreSuppressed();
127 bool NaClBrowserDelegateImpl::GetCacheDirectory(base::FilePath* cache_dir) {
128 base::FilePath user_data_dir;
129 if (!PathService::Get(chrome::DIR_USER_DATA, &user_data_dir))
130 return false;
131 chrome::GetUserCacheDirectory(user_data_dir, cache_dir);
132 return true;
135 bool NaClBrowserDelegateImpl::GetPluginDirectory(base::FilePath* plugin_dir) {
136 return PathService::Get(chrome::DIR_INTERNAL_PLUGINS, plugin_dir);
139 bool NaClBrowserDelegateImpl::GetPnaclDirectory(base::FilePath* pnacl_dir) {
140 return PathService::Get(chrome::DIR_PNACL_COMPONENT, pnacl_dir);
143 bool NaClBrowserDelegateImpl::GetUserDirectory(base::FilePath* user_dir) {
144 return PathService::Get(chrome::DIR_USER_DATA, user_dir);
147 std::string NaClBrowserDelegateImpl::GetVersionString() const {
148 return chrome::VersionInfo().CreateVersionString();
151 ppapi::host::HostFactory* NaClBrowserDelegateImpl::CreatePpapiHostFactory(
152 content::BrowserPpapiHost* ppapi_host) {
153 return new chrome::ChromeBrowserPepperHostFactory(ppapi_host);
156 void NaClBrowserDelegateImpl::SetDebugPatterns(std::string debug_patterns) {
157 if (!debug_patterns.empty() && debug_patterns[0] == '!') {
158 inverse_debug_patterns_ = true;
159 debug_patterns.erase(0, 1);
161 if (debug_patterns.empty()) {
162 return;
164 std::vector<std::string> patterns;
165 base::SplitString(debug_patterns, ',', &patterns);
166 for (std::vector<std::string>::iterator iter = patterns.begin();
167 iter != patterns.end(); ++iter) {
168 URLPattern pattern;
169 if (pattern.Parse(*iter) == URLPattern::PARSE_SUCCESS) {
170 // If URL pattern has scheme equal to *, Parse method resets valid
171 // schemes mask to http and https only, so we need to reset it after
172 // Parse to include chrome-extension scheme that can be used by NaCl
173 // manifest files.
174 pattern.SetValidSchemes(URLPattern::SCHEME_ALL);
175 debug_patterns_.push_back(pattern);
180 bool NaClBrowserDelegateImpl::URLMatchesDebugPatterns(
181 const GURL& manifest_url) {
182 // Empty patterns are forbidden so we ignore them.
183 if (debug_patterns_.empty()) {
184 return true;
186 bool matches = false;
187 for (std::vector<URLPattern>::iterator iter = debug_patterns_.begin();
188 iter != debug_patterns_.end(); ++iter) {
189 if (iter->MatchesURL(manifest_url)) {
190 matches = true;
191 break;
194 if (inverse_debug_patterns_) {
195 return !matches;
196 } else {
197 return matches;
201 // This function is security sensitive. Be sure to check with a security
202 // person before you modify it.
203 bool NaClBrowserDelegateImpl::MapUrlToLocalFilePath(
204 const GURL& file_url, bool use_blocking_api, base::FilePath* file_path) {
205 DCHECK(extension_info_map_);
206 // Check that the URL is recognized by the extension system.
207 const extensions::Extension* extension =
208 extension_info_map_->extensions().GetExtensionOrAppByURL(file_url);
209 if (!extension)
210 return false;
212 // This is a short-cut which avoids calling a blocking file operation
213 // (GetFilePath()), so that this can be called on the IO thread. It only
214 // handles a subset of the urls.
215 if (!use_blocking_api) {
216 if (file_url.SchemeIs(extensions::kExtensionScheme)) {
217 std::string path = file_url.path();
218 base::TrimString(path, "/", &path); // Remove first slash
219 *file_path = extension->path().AppendASCII(path);
220 return true;
222 return false;
225 std::string path = file_url.path();
226 extensions::ExtensionResource resource;
228 if (SharedModuleInfo::IsImportedPath(path)) {
229 // Check if this is a valid path that is imported for this extension.
230 std::string new_extension_id;
231 std::string new_relative_path;
232 SharedModuleInfo::ParseImportedPath(path, &new_extension_id,
233 &new_relative_path);
234 const extensions::Extension* new_extension =
235 extension_info_map_->extensions().GetByID(new_extension_id);
236 if (!new_extension)
237 return false;
239 if (!SharedModuleInfo::ImportsExtensionById(extension, new_extension_id) ||
240 !SharedModuleInfo::IsExportAllowed(new_extension, new_relative_path)) {
241 return false;
244 resource = new_extension->GetResource(new_relative_path);
245 } else {
246 // Check that the URL references a resource in the extension.
247 resource = extension->GetResource(path);
250 if (resource.empty())
251 return false;
253 // GetFilePath is a blocking function call.
254 const base::FilePath resource_file_path = resource.GetFilePath();
255 if (resource_file_path.empty())
256 return false;
258 *file_path = resource_file_path;
259 return true;
262 content::BrowserPpapiHost::OnKeepaliveCallback
263 NaClBrowserDelegateImpl::GetOnKeepaliveCallback() {
264 return base::Bind(&OnKeepalive);