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/extensions/extension_system.h"
14 #include "chrome/browser/nacl_host/nacl_infobar_delegate.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/renderer_host/pepper/chrome_browser_pepper_host_factory.h"
17 #include "chrome/common/chrome_paths.h"
18 #include "chrome/common/chrome_paths_internal.h"
19 #include "chrome/common/chrome_version_info.h"
20 #include "chrome/common/logging_chrome.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "content/public/browser/render_frame_host.h"
23 #include "content/public/browser/site_instance.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
;
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)
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
)
67 content::SiteInstance
* site_instance
= render_frame_host
->GetSiteInstance();
71 extensions::ExtensionSystem
* extension_system
=
72 extensions::ExtensionSystem::GetForBrowserContext(
73 site_instance
->GetBrowserContext());
74 if (!extension_system
)
77 const ExtensionService
* extension_service
=
78 extension_system
->extension_service();
79 if (!extension_service
)
82 const extensions::Extension
* extension
= extension_service
->GetExtensionById(
83 instance_data
[0].document_url
.host(), false);
87 extensions::ProcessManager
* pm
= extension_system
->process_manager();
91 pm
->KeepaliveImpulse(extension
);
94 // Calls OnKeepaliveOnUIThread on UI thread.
96 const content::BrowserPpapiHost::OnKeepaliveInstanceData
& instance_data
,
97 const base::FilePath
& profile_data_directory
) {
98 DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
99 content::BrowserThread::PostTask(content::BrowserThread::UI
, FROM_HERE
,
100 base::Bind(&OnKeepaliveOnUIThread
,
102 profile_data_directory
));
107 NaClBrowserDelegateImpl::NaClBrowserDelegateImpl(
108 extensions::InfoMap
* extension_info_map
)
109 : extension_info_map_(extension_info_map
), inverse_debug_patterns_(false) {}
111 NaClBrowserDelegateImpl::~NaClBrowserDelegateImpl() {
114 void NaClBrowserDelegateImpl::ShowNaClInfobar(int render_process_id
,
117 DCHECK_EQ(PP_NACL_MANIFEST_MISSING_ARCH
, error_id
);
118 content::BrowserThread::PostTask(
119 content::BrowserThread::UI
, FROM_HERE
,
120 base::Bind(&NaClInfoBarDelegate::Create
, render_process_id
,
124 bool NaClBrowserDelegateImpl::DialogsAreSuppressed() {
125 return logging::DialogsAreSuppressed();
128 bool NaClBrowserDelegateImpl::GetCacheDirectory(base::FilePath
* cache_dir
) {
129 base::FilePath user_data_dir
;
130 if (!PathService::Get(chrome::DIR_USER_DATA
, &user_data_dir
))
132 chrome::GetUserCacheDirectory(user_data_dir
, cache_dir
);
136 bool NaClBrowserDelegateImpl::GetPluginDirectory(base::FilePath
* plugin_dir
) {
137 return PathService::Get(chrome::DIR_INTERNAL_PLUGINS
, plugin_dir
);
140 bool NaClBrowserDelegateImpl::GetPnaclDirectory(base::FilePath
* pnacl_dir
) {
141 return PathService::Get(chrome::DIR_PNACL_COMPONENT
, pnacl_dir
);
144 bool NaClBrowserDelegateImpl::GetUserDirectory(base::FilePath
* user_dir
) {
145 return PathService::Get(chrome::DIR_USER_DATA
, user_dir
);
148 std::string
NaClBrowserDelegateImpl::GetVersionString() const {
149 return chrome::VersionInfo().CreateVersionString();
152 ppapi::host::HostFactory
* NaClBrowserDelegateImpl::CreatePpapiHostFactory(
153 content::BrowserPpapiHost
* ppapi_host
) {
154 return new chrome::ChromeBrowserPepperHostFactory(ppapi_host
);
157 void NaClBrowserDelegateImpl::SetDebugPatterns(std::string debug_patterns
) {
158 if (!debug_patterns
.empty() && debug_patterns
[0] == '!') {
159 inverse_debug_patterns_
= true;
160 debug_patterns
.erase(0, 1);
162 if (debug_patterns
.empty()) {
165 std::vector
<std::string
> patterns
;
166 base::SplitString(debug_patterns
, ',', &patterns
);
167 for (std::vector
<std::string
>::iterator iter
= patterns
.begin();
168 iter
!= patterns
.end(); ++iter
) {
170 if (pattern
.Parse(*iter
) == URLPattern::PARSE_SUCCESS
) {
171 // If URL pattern has scheme equal to *, Parse method resets valid
172 // schemes mask to http and https only, so we need to reset it after
173 // Parse to include chrome-extension scheme that can be used by NaCl
175 pattern
.SetValidSchemes(URLPattern::SCHEME_ALL
);
176 debug_patterns_
.push_back(pattern
);
181 bool NaClBrowserDelegateImpl::URLMatchesDebugPatterns(
182 const GURL
& manifest_url
) {
183 // Empty patterns are forbidden so we ignore them.
184 if (debug_patterns_
.empty()) {
187 bool matches
= false;
188 for (std::vector
<URLPattern
>::iterator iter
= debug_patterns_
.begin();
189 iter
!= debug_patterns_
.end(); ++iter
) {
190 if (iter
->MatchesURL(manifest_url
)) {
195 if (inverse_debug_patterns_
) {
202 // This function is security sensitive. Be sure to check with a security
203 // person before you modify it.
204 bool NaClBrowserDelegateImpl::MapUrlToLocalFilePath(
205 const GURL
& file_url
, bool use_blocking_api
, base::FilePath
* file_path
) {
206 DCHECK(extension_info_map_
);
207 // Check that the URL is recognized by the extension system.
208 const extensions::Extension
* extension
=
209 extension_info_map_
->extensions().GetExtensionOrAppByURL(file_url
);
213 // This is a short-cut which avoids calling a blocking file operation
214 // (GetFilePath()), so that this can be called on the IO thread. It only
215 // handles a subset of the urls.
216 if (!use_blocking_api
) {
217 if (file_url
.SchemeIs(extensions::kExtensionScheme
)) {
218 std::string path
= file_url
.path();
219 base::TrimString(path
, "/", &path
); // Remove first slash
220 *file_path
= extension
->path().AppendASCII(path
);
226 std::string path
= file_url
.path();
227 extensions::ExtensionResource resource
;
229 if (SharedModuleInfo::IsImportedPath(path
)) {
230 // Check if this is a valid path that is imported for this extension.
231 std::string new_extension_id
;
232 std::string new_relative_path
;
233 SharedModuleInfo::ParseImportedPath(path
, &new_extension_id
,
235 const extensions::Extension
* new_extension
=
236 extension_info_map_
->extensions().GetByID(new_extension_id
);
240 if (!SharedModuleInfo::ImportsExtensionById(extension
, new_extension_id
) ||
241 !SharedModuleInfo::IsExportAllowed(new_extension
, new_relative_path
)) {
245 resource
= new_extension
->GetResource(new_relative_path
);
247 // Check that the URL references a resource in the extension.
248 resource
= extension
->GetResource(path
);
251 if (resource
.empty())
254 // GetFilePath is a blocking function call.
255 const base::FilePath resource_file_path
= resource
.GetFilePath();
256 if (resource_file_path
.empty())
259 *file_path
= resource_file_path
;
263 content::BrowserPpapiHost::OnKeepaliveCallback
264 NaClBrowserDelegateImpl::GetOnKeepaliveCallback() {
265 return base::Bind(&OnKeepalive
);