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..ed6b0e21769459afe35802528022c8df0f530522 |
| --- /dev/null |
| +++ b/chrome/browser/renderer_host/pepper/pepper_ext_crx_file_system_browser_host.cc |
| @@ -0,0 +1,165 @@ |
| +// 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/host/resource_message_filter.h" |
| +#include "ppapi/proxy/ppapi_messages.h" |
| +#include "webkit/fileapi/isolated_context.h" |
| + |
| +namespace chrome { |
| + |
| +namespace { |
| + |
| +class CrxFileSystemFilter : public ppapi::host::ResourceMessageFilter { |
| + public: |
| + CrxFileSystemFilter(content::BrowserPpapiHost* browser_ppapi_host, |
| + PP_Instance instance) |
| + : browser_ppapi_host_(browser_ppapi_host), instance_(instance) { } |
|
yzshen1
2013/05/02 23:22:01
nit: zero space between {}
victorhsieh
2013/05/03 17:26:58
Done.
|
| + |
| + // ppapi::host::ResourceMessageFilter implementation. |
| + virtual scoped_refptr<base::TaskRunner> OverrideTaskRunnerForMessage( |
| + const IPC::Message& msg) OVERRIDE; |
| + virtual int32_t OnResourceMessageReceived( |
| + const IPC::Message& msg, |
| + ppapi::host::HostMessageContext* context) OVERRIDE; |
| + |
| + private: |
| + ~CrxFileSystemFilter() {} |
|
yzshen1
2013/05/02 23:22:01
use virtual, please (I know it makes no difference
victorhsieh
2013/05/03 17:26:58
Done.
|
| + |
| + // Returns filesystem id of isolated filesystem if valid, or empty string |
| + // otherwise. This is expected to run in UI thread as ProfileManager only |
| + // allows UI thread access. |
| + std::string CreateIsolatedFileSystem(const base::FilePath& profile_directory, |
| + const GURL& url); |
| + |
| + int32_t OnOpenFileSystem(ppapi::host::HostMessageContext* context); |
| + |
| + void Reply(ppapi::host::HostMessageContext* context, |
| + int32_t pp_error, |
| + const std::string& fsid); |
| + |
| + content::BrowserPpapiHost* browser_ppapi_host_; |
|
yzshen1
2013/05/02 23:22:01
This can only be used on IO thread. You should ret
victorhsieh
2013/05/03 17:26:58
Done.
|
| + PP_Instance instance_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(CrxFileSystemFilter); |
| +}; |
| + |
| +scoped_refptr<base::TaskRunner> |
| +CrxFileSystemFilter::OverrideTaskRunnerForMessage( |
| + const IPC::Message& msg) { |
| + // In order to reach ExtensionSystem, we need to get ProfileManager first. |
| + // ProfileManager lives in UI thread, so we need to do this in UI thread. |
| + return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI); |
| +} |
| + |
| +int32_t CrxFileSystemFilter::OnResourceMessageReceived( |
| + const IPC::Message& msg, |
| + ppapi::host::HostMessageContext* context) { |
| + IPC_BEGIN_MESSAGE_MAP(CrxFileSystemFilter, msg) |
| + PPAPI_DISPATCH_HOST_RESOURCE_CALL_0( |
| + PpapiHostMsg_Ext_CrxFileSystem_BrowserOpen, OnOpenFileSystem); |
| + IPC_END_MESSAGE_MAP() |
| + return PP_ERROR_FAILED; |
| +} |
| + |
| +std::string CrxFileSystemFilter::CreateIsolatedFileSystem( |
| + const base::FilePath& profile_directory, |
| + const GURL& url) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + 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* extension_system = |
| + extensions::ExtensionSystem::Get(profile); |
| + if (!extension_system) |
| + return std::string(); |
| + |
| + const ExtensionService* extension_service = |
| + extension_system->extension_service(); |
| + if (!extension_service) |
| + return std::string(); |
| + |
| + const extensions::Extension* extension = |
| + extension_service->GetExtensionById(url.host(), false); |
| + if (!extension) |
| + return std::string(); |
| + |
| + // First level directory for isolated filesystem to lookup. |
| + std::string kFirstLevelDirectory("crxfs"); |
| + return fileapi::IsolatedContext::GetInstance()-> |
| + RegisterFileSystemForPath(fileapi::kFileSystemTypeNativeLocal, |
| + extension->path(), |
| + &kFirstLevelDirectory); |
| +} |
| + |
| +int32_t CrxFileSystemFilter::OnOpenFileSystem( |
| + ppapi::host::HostMessageContext* context) { |
| + const std::string fsid = CreateIsolatedFileSystem( |
| + browser_ppapi_host_->GetProfileDataDirectory(), |
| + browser_ppapi_host_->GetDocumentURLForInstance(instance_)); |
| + |
| + if (fsid.empty()) { |
| + Reply(context, PP_ERROR_NOTSUPPORTED, std::string()); |
| + return PP_ERROR_FAILED; |
| + } |
| + |
| + // 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, PP_OK, fsid); |
| + return PP_OK_COMPLETIONPENDING; |
|
yzshen1
2013/05/02 23:22:01
You could set the context->reply_msg and return PP
victorhsieh
2013/05/03 17:26:58
Done.
|
| +} |
| + |
| +void CrxFileSystemFilter::Reply( |
| + ppapi::host::HostMessageContext* context, |
| + int32_t pp_error, |
| + const std::string& fsid) { |
| + ppapi::host::ReplyMessageContext reply_context = |
| + context->MakeReplyMessageContext(); |
| + reply_context.params.set_result(pp_error); |
| + browser_ppapi_host_->GetPpapiHost()->SendReply( |
|
yzshen1
2013/05/02 23:22:01
This should not be used on UI thread. You should u
victorhsieh
2013/05/03 17:26:58
Done.
|
| + reply_context, |
| + PpapiPluginMsg_Ext_CrxFileSystem_BrowserOpenReply(fsid)); |
| +} |
| + |
| +} // namespace |
| + |
| +PepperExtCrxFileSystemBrowserHost::PepperExtCrxFileSystemBrowserHost( |
| + content::BrowserPpapiHost* host, |
| + PP_Instance instance, |
| + PP_Resource resource) |
| + : ppapi::host::ResourceHost(host->GetPpapiHost(), instance, resource) { |
| + AddFilter(scoped_refptr<ppapi::host::ResourceMessageFilter>( |
| + new CrxFileSystemFilter(host, instance))); |
| +} |
| + |
| +PepperExtCrxFileSystemBrowserHost::~PepperExtCrxFileSystemBrowserHost() { |
| +} |
| + |
| +} // namespace chrome |