resource_converter.cc revision 0f1bc08d4cfcc34181b0b5cbf065c40f687bf740
13551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 23551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 33551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// found in the LICENSE file. 43551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 53551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "content/renderer/pepper/resource_converter.h" 63551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 73551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "base/bind.h" 83551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "base/message_loop/message_loop.h" 9424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "content/public/renderer/renderer_ppapi_host.h" 100f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "content/renderer/pepper/pepper_file_system_host.h" 11424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "ipc/ipc_message.h" 120f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "ppapi/host/ppapi_host.h" 130f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "ppapi/host/resource_host.h" 140f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "ppapi/proxy/ppapi_messages.h" 154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "ppapi/shared_impl/resource_var.h" 16424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "ppapi/shared_impl/scoped_pp_var.h" 170f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "third_party/WebKit/public/platform/WebFileSystem.h" 180f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "third_party/WebKit/public/web/WebDOMFileSystem.h" 193551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 20424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)namespace { 213551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 22424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)void FlushComplete( 23424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) const base::Callback<void(bool)>& callback, 244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) const std::vector<scoped_refptr<content::HostResourceVar> >& browser_vars, 25424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) const std::vector<int>& pending_host_ids) { 264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) CHECK(browser_vars.size() == pending_host_ids.size()); 274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) for (size_t i = 0; i < browser_vars.size(); ++i) { 284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) browser_vars[i]->set_pending_browser_host_id(pending_host_ids[i]); 29424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) } 30424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) callback.Run(true); 313551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 323551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 330f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Converts a WebKit::WebFileSystem::Type to a PP_FileSystemType. 340f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)PP_FileSystemType WebFileSystemTypeToPPAPI(WebKit::WebFileSystem::Type type) { 350f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) switch (type) { 360f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) case WebKit::WebFileSystem::TypeTemporary: 370f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) return PP_FILESYSTEMTYPE_LOCALTEMPORARY; 380f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) case WebKit::WebFileSystem::TypePersistent: 390f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) return PP_FILESYSTEMTYPE_LOCALPERSISTENT; 400f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) case WebKit::WebFileSystem::TypeIsolated: 410f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) return PP_FILESYSTEMTYPE_ISOLATED; 420f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) case WebKit::WebFileSystem::TypeExternal: 430f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) return PP_FILESYSTEMTYPE_EXTERNAL; 440f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) default: 450f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) NOTREACHED(); 460f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) return PP_FILESYSTEMTYPE_LOCALTEMPORARY; 470f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) } 480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)} 490f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 500f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Given a V8 value containing a DOMFileSystem, creates a resource host and 510f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// returns the resource information for serialization. 520f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// On error, false. 530f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)bool DOMFileSystemToResource( 540f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) PP_Instance instance, 550f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) content::RendererPpapiHost* host, 560f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) const WebKit::WebDOMFileSystem& dom_file_system, 570f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) int* pending_renderer_id, 580f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) scoped_ptr<IPC::Message>* create_message, 590f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) scoped_ptr<IPC::Message>* browser_host_create_message) { 600f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) DCHECK(!dom_file_system.isNull()); 610f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 620f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) PP_FileSystemType file_system_type = 630f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) WebFileSystemTypeToPPAPI(dom_file_system.type()); 640f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) GURL root_url = dom_file_system.rootURL(); 650f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 660f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // External file systems are not currently supported. (Without this check, 670f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // there would be a CHECK-fail in FileRefResource.) 680f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // TODO(mgiuca): Support external file systems. 690f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) if (file_system_type == PP_FILESYSTEMTYPE_EXTERNAL) 700f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) return false; 710f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 720f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) *pending_renderer_id = host->GetPpapiHost()->AddPendingResourceHost( 730f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) scoped_ptr<ppapi::host::ResourceHost>( 740f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) new content::PepperFileSystemHost(host, instance, 0, root_url, 750f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) file_system_type))); 760f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) if (*pending_renderer_id == 0) 770f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) return false; 780f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 790f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) create_message->reset( 800f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) new PpapiPluginMsg_FileSystem_CreateFromPendingHost(file_system_type)); 810f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 820f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) browser_host_create_message->reset( 830f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) new PpapiHostMsg_FileSystem_CreateFromRenderer(root_url.spec(), 840f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) file_system_type)); 850f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) return true; 860f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)} 870f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 88424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)} // namespace 89424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 90424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)namespace content { 91424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 92424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)ResourceConverter::~ResourceConverter() {} 93424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 94424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)ResourceConverterImpl::ResourceConverterImpl(PP_Instance instance, 95424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) RendererPpapiHost* host) 96424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) : instance_(instance), 97424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) host_(host) { 983551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 993551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 1003551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)ResourceConverterImpl::~ResourceConverterImpl() { 101424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // Verify Flush() was called. 102424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) DCHECK(browser_host_create_messages_.empty()); 1034e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) DCHECK(browser_vars.empty()); 104424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)} 105424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 1064e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)bool ResourceConverterImpl::FromV8Value(v8::Handle<v8::Object> val, 107424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) v8::Handle<v8::Context> context, 1084e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) PP_Var* result, 1094e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) bool* was_resource) { 1104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) v8::Context::Scope context_scope(context); 1114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) v8::HandleScope handle_scope(context->GetIsolate()); 112424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 1134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) *was_resource = false; 1144e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 1150f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) WebKit::WebDOMFileSystem dom_file_system = 1160f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) WebKit::WebDOMFileSystem::fromV8Value(val); 1170f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) if (!dom_file_system.isNull()) { 1180f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) int pending_renderer_id; 1190f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) scoped_ptr<IPC::Message> create_message; 1200f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) scoped_ptr<IPC::Message> browser_host_create_message; 1210f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) if (!DOMFileSystemToResource(instance_, host_, dom_file_system, 1220f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) &pending_renderer_id, &create_message, 1230f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) &browser_host_create_message)) { 1240f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) return false; 1250f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) } 1260f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) DCHECK(create_message); 1270f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) DCHECK(browser_host_create_message); 1280f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) scoped_refptr<HostResourceVar> result_var = 1290f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) CreateResourceVarWithBrowserHost( 1300f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) pending_renderer_id, *create_message, *browser_host_create_message); 1310f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) *result = result_var->GetPPVar(); 1320f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) *was_resource = true; 1330f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) return true; 1340f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) } 1350f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 1360f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // The value was not convertible to a resource. Return true with 1370f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // |was_resource| set to false. As per the interface of FromV8Value, |result| 1380f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // may be left unmodified in this case. 1394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) return true; 140424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)} 141424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 142424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)void ResourceConverterImpl::Flush(const base::Callback<void(bool)>& callback) { 143424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) host_->CreateBrowserResourceHosts( 144424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) instance_, 145424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) browser_host_create_messages_, 1464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) base::Bind(&FlushComplete, callback, browser_vars)); 147424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) browser_host_create_messages_.clear(); 1484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) browser_vars.clear(); 149424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)} 150424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 1514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)scoped_refptr<HostResourceVar> ResourceConverterImpl::CreateResourceVar( 1524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) int pending_renderer_id, 153424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) const IPC::Message& create_message) { 1544e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) return new HostResourceVar(pending_renderer_id, create_message); 1553551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 1563551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 1574e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)scoped_refptr<HostResourceVar> 1584e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)ResourceConverterImpl::CreateResourceVarWithBrowserHost( 1594e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) int pending_renderer_id, 160424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) const IPC::Message& create_message, 161424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) const IPC::Message& browser_host_create_message) { 1624e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) scoped_refptr<HostResourceVar> result = 1634e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) CreateResourceVar(pending_renderer_id, create_message); 164424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) browser_host_create_messages_.push_back(browser_host_create_message); 1654e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) browser_vars.push_back(result); 166424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) return result; 1673551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 1683551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 1693551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} // namespace content 170