Chromium Code Reviews| Index: chrome/browser/renderer_host/pepper/pepper_ext_crx_file_system_browser_host.cc |
| diff --git a/chrome/browser/renderer_host/pepper/pepper_ext_crx_file_system_browser_host.cc b/chrome/browser/renderer_host/pepper/pepper_ext_crx_file_system_browser_host.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e9930230022ec8ce9ebbb18fa08c630bd5bcc54e |
| --- /dev/null |
| +++ b/chrome/browser/renderer_host/pepper/pepper_ext_crx_file_system_browser_host.cc |
| @@ -0,0 +1,132 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/renderer_host/pepper/pepper_ext_crx_file_system_browser_host.h" |
| + |
| +#include "base/files/file_path.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/extensions/extension_service.h" |
| +#include "chrome/browser/extensions/extension_system.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/profiles/profile_manager.h" |
| +#include "chrome/common/extensions/extension.h" |
| +#include "content/public/browser/browser_ppapi_host.h" |
| +#include "content/public/browser/child_process_security_policy.h" |
| +#include "extensions/common/constants.h" |
| +#include "googleurl/src/gurl.h" |
| +#include "ppapi/c/pp_errors.h" |
| +#include "ppapi/host/dispatch_host_message.h" |
| +#include "ppapi/host/host_message_context.h" |
| +#include "ppapi/host/ppapi_host.h" |
| +#include "ppapi/proxy/ppapi_messages.h" |
| +#include "ppapi/proxy/resource_message_params.h" |
| +#include "webkit/fileapi/isolated_context.h" |
| + |
| +namespace chrome { |
| + |
| +namespace { |
| + |
| +// Returns filesystem id of isolated filesystem if accesss, or empty string |
| +// otherwise. This is expected to run in UI thread as ProfileManager only |
| +// allows UI thread access. |
| +std::string MountCrxDirectory( |
| + const base::FilePath& profile_directory, |
| + const GURL& url) { |
| + if (!url.SchemeIs(extensions::kExtensionScheme)) |
| + return std::string(); |
| + |
| + ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| + Profile* profile = profile_manager->GetProfile(profile_directory); |
| + extensions::ExtensionSystem* ext_system = |
|
kinuko
2013/04/18 13:45:03
nit: can we name this 'extension_system' (rather t
victorhsieh
2013/04/18 19:28:38
Done.
|
| + extensions::ExtensionSystem::Get(profile); |
| + if (!ext_system) |
| + return std::string(); |
| + |
| + const ExtensionService* ext_service = ext_system->extension_service(); |
|
kinuko
2013/04/18 13:45:03
ditto
victorhsieh
2013/04/18 19:28:38
Done.
|
| + if (!ext_service) |
| + return std::string(); |
| + |
| + const extensions::Extension* ext = ext_service->GetExtensionById(url.host(), |
| + false); |
| + if (!ext) |
| + return std::string(); |
| + |
| + // First level directory for isolated filesystem to lookup. |
| + std::string kFirstLevelDirectory("crxfs"); |
| + return fileapi::IsolatedContext::GetInstance()-> |
| + RegisterFileSystemForPath(fileapi::kFileSystemTypeNativeLocal, |
| + ext->path(), |
| + &kFirstLevelDirectory); |
| +} |
| + |
| +void MakeAndGrantCrxDirectoryAccess( |
| + content::BrowserPpapiHost* browser_ppapi_host, |
| + ppapi::host::ReplyMessageContext reply_context, |
| + PP_Instance instance) { |
| + // Create an isolated file system for current extension. |
| + const GURL url = browser_ppapi_host->GetDocumentURLForInstance(instance); |
|
teravest
2013/04/18 16:10:38
Did you mean const GURL& url?
victorhsieh
2013/04/18 19:28:38
Done.
|
| + std::string fsid = MountCrxDirectory( |
| + browser_ppapi_host->GetProfileDataDirectory(), url); |
| + if (fsid.empty()) { |
| + reply_context.params.set_result(PP_ERROR_NOTSUPPORTED); |
| + browser_ppapi_host->GetPpapiHost()->SendReply( |
| + reply_context, |
| + PpapiPluginMsg_Ext_CrxFileSystem_BrowserOpenReply(std::string(), |
| + std::string())); |
| + return; |
| + } |
| + |
| + // Grant readonly access of isolated filesystem to renderer process. |
| + int render_process_id; |
| + int unused_render_view_id; |
| + browser_ppapi_host->GetRenderViewIDsForInstance(instance, |
| + &render_process_id, |
| + &unused_render_view_id); |
| + content::ChildProcessSecurityPolicy* policy = |
| + content::ChildProcessSecurityPolicy::GetInstance(); |
| + policy->GrantReadFileSystem(render_process_id, fsid); |
| + |
| + reply_context.params.set_result(PP_OK); |
| + browser_ppapi_host->GetPpapiHost()->SendReply( |
| + reply_context, |
| + PpapiPluginMsg_Ext_CrxFileSystem_BrowserOpenReply(url.host(), fsid)); |
| +} |
| + |
| +} // namespace |
| + |
| +PepperExtCrxFileSystemBrowserHost::PepperExtCrxFileSystemBrowserHost( |
| + content::BrowserPpapiHost* host, |
| + PP_Instance instance, |
| + PP_Resource resource) |
| + : ppapi::host::ResourceHost(host->GetPpapiHost(), instance, resource), |
| + browser_ppapi_host_(host) { |
| +} |
| + |
| +PepperExtCrxFileSystemBrowserHost::~PepperExtCrxFileSystemBrowserHost() { |
| +} |
| + |
| +int32_t PepperExtCrxFileSystemBrowserHost::OnResourceMessageReceived( |
| + const IPC::Message& msg, |
| + ppapi::host::HostMessageContext* context) { |
| + IPC_BEGIN_MESSAGE_MAP(PepperExtCrxFileSystemBrowserHost, msg) |
| + PPAPI_DISPATCH_HOST_RESOURCE_CALL_0( |
| + PpapiBrowserHostMsg_Ext_CrxFileSystem_Open, OnOpenFileSystem); |
| + IPC_END_MESSAGE_MAP() |
| + return PP_ERROR_FAILED; |
| +} |
| + |
| +int32_t PepperExtCrxFileSystemBrowserHost::OnOpenFileSystem( |
| + ppapi::host::HostMessageContext* context) { |
| + // Post to UI thread. We need to get extension by first calling |
| + // ProfileManager at this time, and ProfileManager lives in UI thread. |
| + BrowserThread::PostTask( |
| + BrowserThread::UI, FROM_HERE, |
| + base::Bind(&MakeAndGrantCrxDirectoryAccess, |
| + browser_ppapi_host_, |
| + context->MakeReplyMessageContext(), |
| + pp_instance())); |
| + return PP_OK_COMPLETIONPENDING; |
| +} |
| + |
| +} // namespace chrome |