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/renderer_host/pepper/pepper_isolated_file_system_message_filter.h"
7 #include "chrome/browser/browser_process.h"
8 #include "chrome/browser/extensions/extension_service.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/profiles/profile_manager.h"
11 #include "chrome/common/chrome_switches.h"
12 #include "chrome/common/pepper_permission_util.h"
13 #include "content/public/browser/browser_ppapi_host.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/child_process_security_policy.h"
16 #include "content/public/browser/render_view_host.h"
17 #include "extensions/browser/extension_system.h"
18 #include "extensions/common/constants.h"
19 #include "extensions/common/extension.h"
20 #include "extensions/common/extension_set.h"
21 #include "ppapi/c/pp_errors.h"
22 #include "ppapi/host/dispatch_host_message.h"
23 #include "ppapi/host/host_message_context.h"
24 #include "ppapi/host/ppapi_host.h"
25 #include "ppapi/proxy/ppapi_messages.h"
26 #include "ppapi/shared_impl/file_system_util.h"
27 #include "webkit/browser/fileapi/isolated_context.h"
33 const char* kPredefinedAllowedCrxFsOrigins[] = {
34 "6EAED1924DB611B6EEF2A664BD077BE7EAD33B8F", // see crbug.com/234789
35 "4EB74897CB187C7633357C2FE832E0AD6A44883A" // see crbug.com/234789
41 PepperIsolatedFileSystemMessageFilter*
42 PepperIsolatedFileSystemMessageFilter::Create(PP_Instance instance,
43 content::BrowserPpapiHost* host) {
44 int render_process_id;
45 int unused_render_frame_id;
46 if (!host->GetRenderFrameIDsForInstance(
47 instance, &render_process_id, &unused_render_frame_id)) {
50 return new PepperIsolatedFileSystemMessageFilter(
52 host->GetProfileDataDirectory(),
53 host->GetDocumentURLForInstance(instance),
54 host->GetPpapiHost());
57 PepperIsolatedFileSystemMessageFilter::PepperIsolatedFileSystemMessageFilter(
58 int render_process_id,
59 const base::FilePath& profile_directory,
60 const GURL& document_url,
61 ppapi::host::PpapiHost* ppapi_host)
62 : render_process_id_(render_process_id),
63 profile_directory_(profile_directory),
64 document_url_(document_url),
65 ppapi_host_(ppapi_host) {
66 for (size_t i = 0; i < arraysize(kPredefinedAllowedCrxFsOrigins); ++i)
67 allowed_crxfs_origins_.insert(kPredefinedAllowedCrxFsOrigins[i]);
70 PepperIsolatedFileSystemMessageFilter::
71 ~PepperIsolatedFileSystemMessageFilter() {}
73 scoped_refptr<base::TaskRunner>
74 PepperIsolatedFileSystemMessageFilter::OverrideTaskRunnerForMessage(
75 const IPC::Message& msg) {
76 // In order to reach ExtensionSystem, we need to get ProfileManager first.
77 // ProfileManager lives in UI thread, so we need to do this in UI thread.
78 return content::BrowserThread::GetMessageLoopProxyForThread(
79 content::BrowserThread::UI);
82 int32_t PepperIsolatedFileSystemMessageFilter::OnResourceMessageReceived(
83 const IPC::Message& msg,
84 ppapi::host::HostMessageContext* context) {
85 IPC_BEGIN_MESSAGE_MAP(PepperIsolatedFileSystemMessageFilter, msg)
86 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_IsolatedFileSystem_BrowserOpen,
89 return PP_ERROR_FAILED;
92 Profile* PepperIsolatedFileSystemMessageFilter::GetProfile() {
93 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
94 ProfileManager* profile_manager = g_browser_process->profile_manager();
95 return profile_manager->GetProfile(profile_directory_);
98 std::string PepperIsolatedFileSystemMessageFilter::CreateCrxFileSystem(
100 extensions::ExtensionSystem* extension_system =
101 extensions::ExtensionSystem::Get(profile);
102 if (!extension_system)
103 return std::string();
105 const ExtensionService* extension_service =
106 extension_system->extension_service();
107 if (!extension_service)
108 return std::string();
110 const extensions::Extension* extension =
111 extension_service->GetExtensionById(document_url_.host(), false);
113 return std::string();
115 // First level directory for isolated filesystem to lookup.
116 std::string kFirstLevelDirectory("crxfs");
117 return fileapi::IsolatedContext::GetInstance()->RegisterFileSystemForPath(
118 fileapi::kFileSystemTypeNativeLocal,
120 &kFirstLevelDirectory);
123 int32_t PepperIsolatedFileSystemMessageFilter::OnOpenFileSystem(
124 ppapi::host::HostMessageContext* context,
125 PP_IsolatedFileSystemType_Private type) {
127 case PP_ISOLATEDFILESYSTEMTYPE_PRIVATE_INVALID:
129 case PP_ISOLATEDFILESYSTEMTYPE_PRIVATE_CRX:
130 return OpenCrxFileSystem(context);
131 case PP_ISOLATEDFILESYSTEMTYPE_PRIVATE_PLUGINPRIVATE:
132 return OpenPluginPrivateFileSystem(context);
136 PpapiPluginMsg_IsolatedFileSystem_BrowserOpenReply(std::string());
137 return PP_ERROR_FAILED;
140 int32_t PepperIsolatedFileSystemMessageFilter::OpenCrxFileSystem(
141 ppapi::host::HostMessageContext* context) {
142 Profile* profile = GetProfile();
143 const extensions::ExtensionSet* extension_set = NULL;
145 extension_set = extensions::ExtensionSystem::Get(profile)
146 ->extension_service()
149 if (!IsExtensionOrSharedModuleWhitelisted(
150 document_url_, extension_set, allowed_crxfs_origins_) &&
151 !IsHostAllowedByCommandLine(
152 document_url_, extension_set, switches::kAllowNaClCrxFsAPI)) {
153 LOG(ERROR) << "Host " << document_url_.host() << " cannot use CrxFs API.";
154 return PP_ERROR_NOACCESS;
157 // TODO(raymes): When we remove FileSystem from the renderer, we should create
158 // a pending PepperFileSystemBrowserHost here with the fsid and send the
159 // pending host ID back to the plugin.
160 const std::string fsid = CreateCrxFileSystem(profile);
163 PpapiPluginMsg_IsolatedFileSystem_BrowserOpenReply(std::string());
164 return PP_ERROR_NOTSUPPORTED;
167 // Grant readonly access of isolated filesystem to renderer process.
168 content::ChildProcessSecurityPolicy* policy =
169 content::ChildProcessSecurityPolicy::GetInstance();
170 policy->GrantReadFileSystem(render_process_id_, fsid);
172 context->reply_msg = PpapiPluginMsg_IsolatedFileSystem_BrowserOpenReply(fsid);
176 int32_t PepperIsolatedFileSystemMessageFilter::OpenPluginPrivateFileSystem(
177 ppapi::host::HostMessageContext* context) {
179 // Only plugins with private permission can open the filesystem.
180 if (!ppapi_host_->permissions().HasPermission(ppapi::PERMISSION_PRIVATE))
181 return PP_ERROR_NOACCESS;
183 const std::string& root_name = ppapi::IsolatedFileSystemTypeToRootName(
184 PP_ISOLATEDFILESYSTEMTYPE_PRIVATE_PLUGINPRIVATE);
185 const std::string& fsid =
186 fileapi::IsolatedContext::GetInstance()->RegisterFileSystemForVirtualPath(
187 fileapi::kFileSystemTypePluginPrivate, root_name, base::FilePath());
189 // Grant full access of isolated filesystem to renderer process.
190 content::ChildProcessSecurityPolicy* policy =
191 content::ChildProcessSecurityPolicy::GetInstance();
192 policy->GrantCreateReadWriteFileSystem(render_process_id_, fsid);
194 context->reply_msg = PpapiPluginMsg_IsolatedFileSystem_BrowserOpenReply(fsid);
198 } // namespace chrome