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 "chrome/browser/browser_process.h"
10 #include "chrome/browser/component_updater/pnacl/pnacl_component_installer.h"
11 #if defined(ENABLE_EXTENSIONS)
12 #include "chrome/browser/extensions/extension_service.h"
14 #include "chrome/browser/nacl_host/nacl_infobar_delegate.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/profiles/profile_manager.h"
17 #include "chrome/browser/renderer_host/pepper/chrome_browser_pepper_host_factory.h"
18 #include "chrome/common/chrome_paths.h"
19 #include "chrome/common/chrome_paths_internal.h"
20 #include "chrome/common/chrome_version_info.h"
21 #include "chrome/common/logging_chrome.h"
22 #include "chrome/common/pepper_permission_util.h"
23 #include "content/public/browser/browser_thread.h"
24 #if defined(ENABLE_EXTENSIONS)
25 #include "extensions/browser/extension_system.h"
26 #include "extensions/browser/info_map.h"
27 #include "extensions/browser/process_manager.h"
28 #include "extensions/common/constants.h"
29 #include "extensions/common/extension.h"
30 #include "extensions/common/url_pattern.h"
36 // These are temporarily needed for testing non-sfi mode on ChromeOS without
37 // passing command-line arguments to Chrome.
38 const char* const kAllowedNonSfiOrigins
[] = {
39 "6EAED1924DB611B6EEF2A664BD077BE7EAD33B8F", // see http://crbug.com/355141
40 "4EB74897CB187C7633357C2FE832E0AD6A44883A" // see http://crbug.com/355141
43 // Handles an extension's NaCl process transitioning in or out of idle state by
44 // relaying the state to the extension's process manager.
46 // A NaCl instance, when active (making PPAPI calls or receiving callbacks),
47 // sends keepalive IPCs to the browser process BrowserPpapiHost at a throttled
48 // rate. The content::BrowserPpapiHost passes context information up to the
49 // chrome level NaClProcessHost where we use the instance's context to find the
50 // associated extension process manager.
52 // There is a 1:many relationship for extension:nacl-embeds, but only a
53 // 1:1 relationship for NaClProcessHost:PP_Instance. The content layer doesn't
54 // rely on this knowledge because it routes messages for ppapi non-nacl
55 // instances as well, though they won't have callbacks set. Here the 1:1
56 // assumption is made and DCHECKed.
57 void OnKeepaliveOnUIThread(
58 const content::BrowserPpapiHost::OnKeepaliveInstanceData
& instance_data
,
59 const base::FilePath
& profile_data_directory
) {
60 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
62 // Only one instance will exist for NaCl embeds, even when more than one
63 // embed of the same plugin exists on the same page.
64 DCHECK(instance_data
.size() == 1);
65 if (instance_data
.size() < 1)
68 #if defined(ENABLE_EXTENSIONS)
69 extensions::ProcessManager::OnKeepaliveFromPlugin(
70 instance_data
[0].render_process_id
,
71 instance_data
[0].render_frame_id
,
72 instance_data
[0].document_url
.host());
76 // Calls OnKeepaliveOnUIThread on UI thread.
78 const content::BrowserPpapiHost::OnKeepaliveInstanceData
& instance_data
,
79 const base::FilePath
& profile_data_directory
) {
80 DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
81 content::BrowserThread::PostTask(content::BrowserThread::UI
, FROM_HERE
,
82 base::Bind(&OnKeepaliveOnUIThread
,
84 profile_data_directory
));
89 NaClBrowserDelegateImpl::NaClBrowserDelegateImpl(
90 ProfileManager
* profile_manager
)
91 : profile_manager_(profile_manager
), inverse_debug_patterns_(false) {
92 DCHECK(profile_manager_
);
93 for (size_t i
= 0; i
< arraysize(kAllowedNonSfiOrigins
); ++i
) {
94 allowed_nonsfi_origins_
.insert(kAllowedNonSfiOrigins
[i
]);
98 NaClBrowserDelegateImpl::~NaClBrowserDelegateImpl() {
101 void NaClBrowserDelegateImpl::ShowMissingArchInfobar(int render_process_id
,
102 int render_view_id
) {
103 content::BrowserThread::PostTask(
104 content::BrowserThread::UI
, FROM_HERE
,
105 base::Bind(&NaClInfoBarDelegate::Create
, render_process_id
,
109 bool NaClBrowserDelegateImpl::DialogsAreSuppressed() {
110 return logging::DialogsAreSuppressed();
113 bool NaClBrowserDelegateImpl::GetCacheDirectory(base::FilePath
* cache_dir
) {
114 base::FilePath user_data_dir
;
115 if (!PathService::Get(chrome::DIR_USER_DATA
, &user_data_dir
))
117 chrome::GetUserCacheDirectory(user_data_dir
, cache_dir
);
121 bool NaClBrowserDelegateImpl::GetPluginDirectory(base::FilePath
* plugin_dir
) {
122 return PathService::Get(chrome::DIR_INTERNAL_PLUGINS
, plugin_dir
);
125 bool NaClBrowserDelegateImpl::GetPnaclDirectory(base::FilePath
* pnacl_dir
) {
126 return PathService::Get(chrome::DIR_PNACL_COMPONENT
, pnacl_dir
);
129 bool NaClBrowserDelegateImpl::GetUserDirectory(base::FilePath
* user_dir
) {
130 return PathService::Get(chrome::DIR_USER_DATA
, user_dir
);
133 std::string
NaClBrowserDelegateImpl::GetVersionString() const {
134 return chrome::VersionInfo().CreateVersionString();
137 ppapi::host::HostFactory
* NaClBrowserDelegateImpl::CreatePpapiHostFactory(
138 content::BrowserPpapiHost
* ppapi_host
) {
139 return new chrome::ChromeBrowserPepperHostFactory(ppapi_host
);
142 void NaClBrowserDelegateImpl::SetDebugPatterns(
143 const std::string
& debug_patterns
) {
144 #if defined(ENABLE_EXTENSIONS)
145 if (debug_patterns
.empty()) {
148 std::vector
<std::string
> patterns
;
149 if (debug_patterns
[0] == '!') {
150 std::string negated_patterns
= debug_patterns
;
151 inverse_debug_patterns_
= true;
152 negated_patterns
.erase(0, 1);
153 base::SplitString(negated_patterns
, ',', &patterns
);
155 base::SplitString(debug_patterns
, ',', &patterns
);
157 for (std::vector
<std::string
>::iterator iter
= patterns
.begin();
158 iter
!= patterns
.end(); ++iter
) {
159 // Allow chrome:// schema, which is used to filter out the internal
160 // PNaCl translator. Also allow chrome-extension:// schema (which
161 // can have NaCl modules). The default is to disallow these schema
162 // since they can be dangerous in the context of chrome extension
163 // permissions, but they are okay here, for NaCl GDB avoidance.
164 URLPattern
pattern(URLPattern::SCHEME_ALL
);
165 if (pattern
.Parse(*iter
) == URLPattern::PARSE_SUCCESS
) {
166 // If URL pattern has scheme equal to *, Parse method resets valid
167 // schemes mask to http and https only, so we need to reset it after
168 // Parse to re-include chrome-extension and chrome schema.
169 pattern
.SetValidSchemes(URLPattern::SCHEME_ALL
);
170 debug_patterns_
.push_back(pattern
);
173 #endif // defined(ENABLE_EXTENSIONS)
176 bool NaClBrowserDelegateImpl::URLMatchesDebugPatterns(
177 const GURL
& manifest_url
) {
178 #if defined(ENABLE_EXTENSIONS)
179 // Empty patterns are forbidden so we ignore them.
180 if (debug_patterns_
.empty()) {
183 bool matches
= false;
184 for (std::vector
<URLPattern
>::iterator iter
= debug_patterns_
.begin();
185 iter
!= debug_patterns_
.end(); ++iter
) {
186 if (iter
->MatchesURL(manifest_url
)) {
191 if (inverse_debug_patterns_
) {
198 #endif // defined(ENABLE_EXTENSIONS)
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
,
205 bool use_blocking_api
,
206 const base::FilePath
& profile_directory
,
207 base::FilePath
* file_path
) {
208 #if defined(ENABLE_EXTENSIONS)
209 scoped_refptr
<extensions::InfoMap
> extension_info_map
=
210 GetExtensionInfoMap(profile_directory
);
211 return extension_info_map
->MapUrlToLocalFilePath(
212 file_url
, use_blocking_api
, file_path
);
218 content::BrowserPpapiHost::OnKeepaliveCallback
219 NaClBrowserDelegateImpl::GetOnKeepaliveCallback() {
220 return base::Bind(&OnKeepalive
);
223 bool NaClBrowserDelegateImpl::IsNonSfiModeAllowed(
224 const base::FilePath
& profile_directory
,
225 const GURL
& manifest_url
) {
226 #if defined(ENABLE_EXTENSIONS)
227 const extensions::ExtensionSet
* extension_set
=
228 &GetExtensionInfoMap(profile_directory
)->extensions();
229 return chrome::IsExtensionOrSharedModuleWhitelisted(
230 manifest_url
, extension_set
, allowed_nonsfi_origins_
);
236 #if defined(ENABLE_EXTENSIONS)
237 scoped_refptr
<extensions::InfoMap
> NaClBrowserDelegateImpl::GetExtensionInfoMap(
238 const base::FilePath
& profile_directory
) {
239 // Get the profile associated with the renderer.
240 Profile
* profile
= profile_manager_
->GetProfileByPath(profile_directory
);
242 scoped_refptr
<extensions::InfoMap
> extension_info_map
=
243 extensions::ExtensionSystem::Get(profile
)->info_map();
244 DCHECK(extension_info_map
.get());
245 return extension_info_map
;