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 "content/renderer/pepper/resource_converter.h"
8 #include "base/message_loop/message_loop.h"
9 #include "content/public/renderer/renderer_ppapi_host.h"
10 #include "content/renderer/pepper/pepper_file_system_host.h"
11 #include "ipc/ipc_message.h"
12 #include "ppapi/host/ppapi_host.h"
13 #include "ppapi/host/resource_host.h"
14 #include "ppapi/proxy/ppapi_messages.h"
15 #include "ppapi/shared_impl/resource_var.h"
16 #include "ppapi/shared_impl/scoped_pp_var.h"
17 #include "third_party/WebKit/public/platform/WebFileSystem.h"
18 #include "third_party/WebKit/public/web/WebDOMFileSystem.h"
23 const base::Callback<void(bool)>& callback,
24 const std::vector<scoped_refptr<content::HostResourceVar> >& browser_vars,
25 const std::vector<int>& pending_host_ids) {
26 CHECK(browser_vars.size() == pending_host_ids.size());
27 for (size_t i = 0; i < browser_vars.size(); ++i) {
28 browser_vars[i]->set_pending_browser_host_id(pending_host_ids[i]);
33 // Converts a WebKit::WebFileSystem::Type to a PP_FileSystemType.
34 PP_FileSystemType WebFileSystemTypeToPPAPI(WebKit::WebFileSystem::Type type) {
36 case WebKit::WebFileSystem::TypeTemporary:
37 return PP_FILESYSTEMTYPE_LOCALTEMPORARY;
38 case WebKit::WebFileSystem::TypePersistent:
39 return PP_FILESYSTEMTYPE_LOCALPERSISTENT;
40 case WebKit::WebFileSystem::TypeIsolated:
41 return PP_FILESYSTEMTYPE_ISOLATED;
42 case WebKit::WebFileSystem::TypeExternal:
43 return PP_FILESYSTEMTYPE_EXTERNAL;
46 return PP_FILESYSTEMTYPE_LOCALTEMPORARY;
50 // Given a V8 value containing a DOMFileSystem, creates a resource host and
51 // returns the resource information for serialization.
53 bool DOMFileSystemToResource(
55 content::RendererPpapiHost* host,
56 const WebKit::WebDOMFileSystem& dom_file_system,
57 int* pending_renderer_id,
58 scoped_ptr<IPC::Message>* create_message,
59 scoped_ptr<IPC::Message>* browser_host_create_message) {
60 DCHECK(!dom_file_system.isNull());
62 PP_FileSystemType file_system_type =
63 WebFileSystemTypeToPPAPI(dom_file_system.type());
64 GURL root_url = dom_file_system.rootURL();
66 // External file systems are not currently supported. (Without this check,
67 // there would be a CHECK-fail in FileRefResource.)
68 // TODO(mgiuca): Support external file systems.
69 if (file_system_type == PP_FILESYSTEMTYPE_EXTERNAL)
72 *pending_renderer_id = host->GetPpapiHost()->AddPendingResourceHost(
73 scoped_ptr<ppapi::host::ResourceHost>(
74 new content::PepperFileSystemHost(host, instance, 0, root_url,
76 if (*pending_renderer_id == 0)
79 create_message->reset(
80 new PpapiPluginMsg_FileSystem_CreateFromPendingHost(file_system_type));
82 browser_host_create_message->reset(
83 new PpapiHostMsg_FileSystem_CreateFromRenderer(root_url.spec(),
92 ResourceConverter::~ResourceConverter() {}
94 ResourceConverterImpl::ResourceConverterImpl(PP_Instance instance,
95 RendererPpapiHost* host)
96 : instance_(instance),
100 ResourceConverterImpl::~ResourceConverterImpl() {
101 // Verify Flush() was called.
102 DCHECK(browser_host_create_messages_.empty());
103 DCHECK(browser_vars.empty());
106 bool ResourceConverterImpl::FromV8Value(v8::Handle<v8::Object> val,
107 v8::Handle<v8::Context> context,
109 bool* was_resource) {
110 v8::Context::Scope context_scope(context);
111 v8::HandleScope handle_scope(context->GetIsolate());
113 *was_resource = false;
115 WebKit::WebDOMFileSystem dom_file_system =
116 WebKit::WebDOMFileSystem::fromV8Value(val);
117 if (!dom_file_system.isNull()) {
118 int pending_renderer_id;
119 scoped_ptr<IPC::Message> create_message;
120 scoped_ptr<IPC::Message> browser_host_create_message;
121 if (!DOMFileSystemToResource(instance_, host_, dom_file_system,
122 &pending_renderer_id, &create_message,
123 &browser_host_create_message)) {
126 DCHECK(create_message);
127 DCHECK(browser_host_create_message);
128 scoped_refptr<HostResourceVar> result_var =
129 CreateResourceVarWithBrowserHost(
130 pending_renderer_id, *create_message, *browser_host_create_message);
131 *result = result_var->GetPPVar();
132 *was_resource = true;
136 // The value was not convertible to a resource. Return true with
137 // |was_resource| set to false. As per the interface of FromV8Value, |result|
138 // may be left unmodified in this case.
142 void ResourceConverterImpl::Flush(const base::Callback<void(bool)>& callback) {
143 host_->CreateBrowserResourceHosts(
145 browser_host_create_messages_,
146 base::Bind(&FlushComplete, callback, browser_vars));
147 browser_host_create_messages_.clear();
148 browser_vars.clear();
151 scoped_refptr<HostResourceVar> ResourceConverterImpl::CreateResourceVar(
152 int pending_renderer_id,
153 const IPC::Message& create_message) {
154 return new HostResourceVar(pending_renderer_id, create_message);
157 scoped_refptr<HostResourceVar>
158 ResourceConverterImpl::CreateResourceVarWithBrowserHost(
159 int pending_renderer_id,
160 const IPC::Message& create_message,
161 const IPC::Message& browser_host_create_message) {
162 scoped_refptr<HostResourceVar> result =
163 CreateResourceVar(pending_renderer_id, create_message);
164 browser_host_create_messages_.push_back(browser_host_create_message);
165 browser_vars.push_back(result);
169 } // namespace content