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"
9 #include "base/path_service.h"
10 #include "base/strings/string_split.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/component_updater/pnacl_component_installer.h"
13 #if defined(ENABLE_EXTENSIONS)
14 #include "chrome/browser/extensions/extension_service.h"
16 #include "chrome/browser/nacl_host/nacl_infobar_delegate.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/profiles/profile_manager.h"
19 #include "chrome/browser/renderer_host/pepper/chrome_browser_pepper_host_factory.h"
20 #include "chrome/common/chrome_paths.h"
21 #include "chrome/common/chrome_paths_internal.h"
22 #include "chrome/common/chrome_version_info.h"
23 #include "chrome/common/logging_chrome.h"
24 #include "chrome/common/pepper_permission_util.h"
25 #include "content/public/browser/browser_thread.h"
26 #if defined(ENABLE_EXTENSIONS)
27 #include "extensions/browser/extension_system.h"
28 #include "extensions/browser/info_map.h"
29 #include "extensions/browser/process_manager.h"
30 #include "extensions/common/constants.h"
31 #include "extensions/common/extension.h"
32 #include "extensions/common/url_pattern.h"
38 // These are temporarily needed for testing non-sfi mode on ChromeOS without
39 // passing command-line arguments to Chrome.
40 const char* const kAllowedNonSfiOrigins
[] = {
41 "6EAED1924DB611B6EEF2A664BD077BE7EAD33B8F", // see http://crbug.com/355141
42 "4EB74897CB187C7633357C2FE832E0AD6A44883A" // see http://crbug.com/355141
45 // Handles an extension's NaCl process transitioning in or out of idle state by
46 // relaying the state to the extension's process manager.
48 // A NaCl instance, when active (making PPAPI calls or receiving callbacks),
49 // sends keepalive IPCs to the browser process BrowserPpapiHost at a throttled
50 // rate. The content::BrowserPpapiHost passes context information up to the
51 // chrome level NaClProcessHost where we use the instance's context to find the
52 // associated extension process manager.
54 // There is a 1:many relationship for extension:nacl-embeds, but only a
55 // 1:1 relationship for NaClProcessHost:PP_Instance. The content layer doesn't
56 // rely on this knowledge because it routes messages for ppapi non-nacl
57 // instances as well, though they won't have callbacks set. Here the 1:1
58 // assumption is made and DCHECKed.
59 void OnKeepaliveOnUIThread(
60 const content::BrowserPpapiHost::OnKeepaliveInstanceData
& instance_data
,
61 const base::FilePath
& profile_data_directory
) {
62 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
64 // Only one instance will exist for NaCl embeds, even when more than one
65 // embed of the same plugin exists on the same page.
66 DCHECK_EQ(1U, instance_data
.size());
67 if (instance_data
.size() < 1)
70 #if defined(ENABLE_EXTENSIONS)
71 extensions::ProcessManager::OnKeepaliveFromPlugin(
72 instance_data
[0].render_process_id
,
73 instance_data
[0].render_frame_id
,
74 instance_data
[0].document_url
.host());
78 // Calls OnKeepaliveOnUIThread on UI thread.
80 const content::BrowserPpapiHost::OnKeepaliveInstanceData
& instance_data
,
81 const base::FilePath
& profile_data_directory
) {
82 DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
83 content::BrowserThread::PostTask(content::BrowserThread::UI
, FROM_HERE
,
84 base::Bind(&OnKeepaliveOnUIThread
,
86 profile_data_directory
));
91 NaClBrowserDelegateImpl::NaClBrowserDelegateImpl(
92 ProfileManager
* profile_manager
)
93 : profile_manager_(profile_manager
), inverse_debug_patterns_(false) {
94 DCHECK(profile_manager_
);
95 for (size_t i
= 0; i
< arraysize(kAllowedNonSfiOrigins
); ++i
) {
96 allowed_nonsfi_origins_
.insert(kAllowedNonSfiOrigins
[i
]);
100 NaClBrowserDelegateImpl::~NaClBrowserDelegateImpl() {
103 void NaClBrowserDelegateImpl::ShowMissingArchInfobar(int render_process_id
,
104 int render_view_id
) {
105 content::BrowserThread::PostTask(
106 content::BrowserThread::UI
, FROM_HERE
,
107 base::Bind(&NaClInfoBarDelegate::Create
, render_process_id
,
111 bool NaClBrowserDelegateImpl::DialogsAreSuppressed() {
112 return logging::DialogsAreSuppressed();
115 bool NaClBrowserDelegateImpl::GetCacheDirectory(base::FilePath
* cache_dir
) {
116 base::FilePath user_data_dir
;
117 if (!PathService::Get(chrome::DIR_USER_DATA
, &user_data_dir
))
119 chrome::GetUserCacheDirectory(user_data_dir
, cache_dir
);
123 bool NaClBrowserDelegateImpl::GetPluginDirectory(base::FilePath
* plugin_dir
) {
124 return PathService::Get(chrome::DIR_INTERNAL_PLUGINS
, plugin_dir
);
127 bool NaClBrowserDelegateImpl::GetPnaclDirectory(base::FilePath
* pnacl_dir
) {
128 return PathService::Get(chrome::DIR_PNACL_COMPONENT
, pnacl_dir
);
131 bool NaClBrowserDelegateImpl::GetUserDirectory(base::FilePath
* user_dir
) {
132 return PathService::Get(chrome::DIR_USER_DATA
, user_dir
);
135 std::string
NaClBrowserDelegateImpl::GetVersionString() const {
136 return chrome::VersionInfo().CreateVersionString();
139 ppapi::host::HostFactory
* NaClBrowserDelegateImpl::CreatePpapiHostFactory(
140 content::BrowserPpapiHost
* ppapi_host
) {
141 return new chrome::ChromeBrowserPepperHostFactory(ppapi_host
);
144 void NaClBrowserDelegateImpl::SetDebugPatterns(
145 const std::string
& debug_patterns
) {
146 #if defined(ENABLE_EXTENSIONS)
147 if (debug_patterns
.empty()) {
150 std::vector
<std::string
> patterns
;
151 if (debug_patterns
[0] == '!') {
152 std::string negated_patterns
= debug_patterns
;
153 inverse_debug_patterns_
= true;
154 negated_patterns
.erase(0, 1);
155 patterns
= base::SplitString(
156 negated_patterns
, ",", base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
);
158 patterns
= base::SplitString(
159 debug_patterns
, ",", base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
);
161 for (const std::string
& pattern_str
: patterns
) {
162 // Allow chrome:// schema, which is used to filter out the internal
163 // PNaCl translator. Also allow chrome-extension:// schema (which
164 // can have NaCl modules). The default is to disallow these schema
165 // since they can be dangerous in the context of chrome extension
166 // permissions, but they are okay here, for NaCl GDB avoidance.
167 URLPattern
pattern(URLPattern::SCHEME_ALL
);
168 if (pattern
.Parse(pattern_str
) == URLPattern::PARSE_SUCCESS
) {
169 // If URL pattern has scheme equal to *, Parse method resets valid
170 // schemes mask to http and https only, so we need to reset it after
171 // Parse to re-include chrome-extension and chrome schema.
172 pattern
.SetValidSchemes(URLPattern::SCHEME_ALL
);
173 debug_patterns_
.push_back(pattern
);
176 #endif // defined(ENABLE_EXTENSIONS)
179 bool NaClBrowserDelegateImpl::URLMatchesDebugPatterns(
180 const GURL
& manifest_url
) {
181 #if defined(ENABLE_EXTENSIONS)
182 // Empty patterns are forbidden so we ignore them.
183 if (debug_patterns_
.empty()) {
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
)) {
194 if (inverse_debug_patterns_
) {
201 #endif // defined(ENABLE_EXTENSIONS)
204 // This function is security sensitive. Be sure to check with a security
205 // person before you modify it.
206 bool NaClBrowserDelegateImpl::MapUrlToLocalFilePath(
207 const GURL
& file_url
,
208 bool use_blocking_api
,
209 const base::FilePath
& profile_directory
,
210 base::FilePath
* file_path
) {
211 #if defined(ENABLE_EXTENSIONS)
212 scoped_refptr
<extensions::InfoMap
> extension_info_map
=
213 GetExtensionInfoMap(profile_directory
);
214 return extension_info_map
->MapUrlToLocalFilePath(
215 file_url
, use_blocking_api
, file_path
);
221 content::BrowserPpapiHost::OnKeepaliveCallback
222 NaClBrowserDelegateImpl::GetOnKeepaliveCallback() {
223 return base::Bind(&OnKeepalive
);
226 bool NaClBrowserDelegateImpl::IsNonSfiModeAllowed(
227 const base::FilePath
& profile_directory
,
228 const GURL
& manifest_url
) {
229 #if defined(ENABLE_EXTENSIONS)
230 const extensions::ExtensionSet
* extension_set
=
231 &GetExtensionInfoMap(profile_directory
)->extensions();
232 return chrome::IsExtensionOrSharedModuleWhitelisted(
233 manifest_url
, extension_set
, allowed_nonsfi_origins_
);
239 #if defined(ENABLE_EXTENSIONS)
240 scoped_refptr
<extensions::InfoMap
> NaClBrowserDelegateImpl::GetExtensionInfoMap(
241 const base::FilePath
& profile_directory
) {
242 // Get the profile associated with the renderer.
243 Profile
* profile
= profile_manager_
->GetProfileByPath(profile_directory
);
245 scoped_refptr
<extensions::InfoMap
> extension_info_map
=
246 extensions::ExtensionSystem::Get(profile
)->info_map();
247 DCHECK(extension_info_map
.get());
248 return extension_info_map
;