OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ppapi/proxy/ppb_file_system_proxy.h" | 5 #include "ppapi/proxy/ppb_file_system_proxy.h" |
6 | 6 |
7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "base/task.h" | 8 #include "base/task.h" |
9 #include "ppapi/c/dev/ppb_file_system_dev.h" | 9 #include "ppapi/c/dev/ppb_file_system_dev.h" |
10 #include "ppapi/c/pp_errors.h" | 10 #include "ppapi/c/pp_errors.h" |
| 11 #include "ppapi/proxy/enter_proxy.h" |
11 #include "ppapi/proxy/host_dispatcher.h" | 12 #include "ppapi/proxy/host_dispatcher.h" |
12 #include "ppapi/proxy/plugin_dispatcher.h" | 13 #include "ppapi/proxy/plugin_dispatcher.h" |
13 #include "ppapi/proxy/plugin_resource.h" | 14 #include "ppapi/proxy/plugin_resource.h" |
14 #include "ppapi/proxy/ppapi_messages.h" | 15 #include "ppapi/proxy/ppapi_messages.h" |
15 #include "ppapi/proxy/serialized_var.h" | 16 #include "ppapi/proxy/serialized_var.h" |
| 17 #include "ppapi/thunk/enter.h" |
| 18 #include "ppapi/thunk/ppb_file_system_api.h" |
| 19 #include "ppapi/thunk/resource_creation_api.h" |
| 20 #include "ppapi/thunk/thunk.h" |
| 21 |
| 22 using ppapi::thunk::EnterFunctionNoLock; |
| 23 using ppapi::thunk::PPB_FileSystem_API; |
| 24 using ppapi::thunk::ResourceCreationAPI; |
16 | 25 |
17 namespace pp { | 26 namespace pp { |
18 namespace proxy { | 27 namespace proxy { |
19 | 28 |
| 29 namespace { |
| 30 |
| 31 InterfaceProxy* CreateFileSystemProxy(Dispatcher* dispatcher, |
| 32 const void* target_interface) { |
| 33 return new PPB_FileSystem_Proxy(dispatcher, target_interface); |
| 34 } |
| 35 |
| 36 } // namespace |
| 37 |
20 // This object maintains most of the state of the ref in the plugin for fast | 38 // This object maintains most of the state of the ref in the plugin for fast |
21 // querying. It's all set in the constructor from the "create info" sent from | 39 // querying. It's all set in the constructor from the "create info" sent from |
22 // the host. | 40 // the host. |
23 class FileSystem : public PluginResource { | 41 class FileSystem : public PluginResource, public PPB_FileSystem_API { |
24 public: | 42 public: |
25 FileSystem(const HostResource& host_resource, PP_FileSystemType_Dev type); | 43 FileSystem(const HostResource& host_resource, PP_FileSystemType_Dev type); |
26 virtual ~FileSystem(); | 44 virtual ~FileSystem(); |
27 | 45 |
28 virtual FileSystem* AsFileSystem(); | 46 // ResourceObjectBase override. |
| 47 virtual PPB_FileSystem_API* AsPPB_FileSystem_API() OVERRIDE; |
29 | 48 |
| 49 // PPB_FileSystem_APi implementation. |
| 50 virtual int32_t Open(int64_t expected_size, |
| 51 PP_CompletionCallback callback) OVERRIDE; |
| 52 virtual PP_FileSystemType_Dev GetType() OVERRIDE; |
| 53 |
| 54 // Called when the host has responded to our open request. |
| 55 void OpenComplete(int32_t result); |
| 56 |
| 57 private: |
30 PP_FileSystemType_Dev type_; | 58 PP_FileSystemType_Dev type_; |
31 bool called_open_; | 59 bool called_open_; |
32 PP_CompletionCallback current_open_callback_; | 60 PP_CompletionCallback current_open_callback_; |
33 | 61 |
34 private: | |
35 DISALLOW_COPY_AND_ASSIGN(FileSystem); | 62 DISALLOW_COPY_AND_ASSIGN(FileSystem); |
36 }; | 63 }; |
37 | 64 |
38 FileSystem::FileSystem(const HostResource& host_resource, | 65 FileSystem::FileSystem(const HostResource& host_resource, |
39 PP_FileSystemType_Dev type) | 66 PP_FileSystemType_Dev type) |
40 : PluginResource(host_resource), | 67 : PluginResource(host_resource), |
41 type_(type), | 68 type_(type), |
42 called_open_(false), | 69 called_open_(false), |
43 current_open_callback_(PP_MakeCompletionCallback(NULL, NULL)) { | 70 current_open_callback_(PP_MakeCompletionCallback(NULL, NULL)) { |
44 } | 71 } |
45 | 72 |
46 // TODO(brettw) this logic is duplicated with some other resource objects | 73 // TODO(brettw) this logic is duplicated with some other resource objects |
47 // like FileChooser. It would be nice to look at all of the different resources | 74 // like FileChooser. It would be nice to look at all of the different resources |
48 // that need callback tracking and design something that they can all re-use. | 75 // that need callback tracking and design something that they can all re-use. |
49 FileSystem::~FileSystem() { | 76 FileSystem::~FileSystem() { |
50 // Ensure the callback is always fired. | 77 // Ensure the callback is always fired. |
51 if (current_open_callback_.func) { | 78 if (current_open_callback_.func) { |
52 // TODO(brettw) the callbacks at this level should be refactored with a | 79 // TODO(brettw) the callbacks at this level should be refactored with a |
53 // more automatic tracking system like we have in the renderer. | 80 // more automatic tracking system like we have in the renderer. |
54 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableFunction( | 81 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableFunction( |
55 current_open_callback_.func, current_open_callback_.user_data, | 82 current_open_callback_.func, current_open_callback_.user_data, |
56 static_cast<int32_t>(PP_ERROR_ABORTED))); | 83 static_cast<int32_t>(PP_ERROR_ABORTED))); |
57 } | 84 } |
58 } | 85 } |
59 | 86 |
60 FileSystem* FileSystem::AsFileSystem() { | 87 PPB_FileSystem_API* FileSystem::AsPPB_FileSystem_API() { |
61 return this; | 88 return this; |
62 } | 89 } |
63 | 90 |
64 namespace { | 91 int32_t FileSystem::Open(int64_t expected_size, |
65 | 92 PP_CompletionCallback callback) { |
66 PP_Resource Create(PP_Instance instance, PP_FileSystemType_Dev type) { | 93 if (current_open_callback_.func) |
67 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); | |
68 if (!dispatcher) | |
69 return PP_ERROR_BADARGUMENT; | |
70 | |
71 HostResource result; | |
72 dispatcher->Send(new PpapiHostMsg_PPBFileSystem_Create( | |
73 INTERFACE_ID_PPB_FILE_SYSTEM, instance, type, &result)); | |
74 if (result.is_null()) | |
75 return 0; | |
76 | |
77 linked_ptr<FileSystem> object(new FileSystem(result, type)); | |
78 return PluginResourceTracker::GetInstance()->AddResource(object); | |
79 } | |
80 | |
81 PP_Bool IsFileSystem(PP_Resource resource) { | |
82 FileSystem* object = PluginResource::GetAs<FileSystem>(resource); | |
83 return BoolToPPBool(!!object); | |
84 } | |
85 | |
86 int32_t Open(PP_Resource file_system, | |
87 int64_t expected_size, | |
88 struct PP_CompletionCallback callback) { | |
89 FileSystem* object = PluginResource::GetAs<FileSystem>(file_system); | |
90 if (!object) | |
91 return PP_ERROR_BADRESOURCE; | |
92 | |
93 Dispatcher* dispatcher = PluginDispatcher::GetForInstance(object->instance()); | |
94 if (!dispatcher) | |
95 return PP_ERROR_BADARGUMENT; | |
96 | |
97 if (object->current_open_callback_.func) | |
98 return PP_ERROR_INPROGRESS; | 94 return PP_ERROR_INPROGRESS; |
99 else if (object->called_open_) | 95 if (called_open_) |
100 return PP_ERROR_FAILED; | 96 return PP_ERROR_FAILED; |
101 | 97 |
102 object->current_open_callback_ = callback; | 98 current_open_callback_ = callback; |
103 object->called_open_ = true; | 99 called_open_ = true; |
104 | 100 GetDispatcher()->Send(new PpapiHostMsg_PPBFileSystem_Open( |
105 dispatcher->Send(new PpapiHostMsg_PPBFileSystem_Open( | 101 INTERFACE_ID_PPB_FILE_SYSTEM, host_resource(), expected_size)); |
106 INTERFACE_ID_PPB_FILE_SYSTEM, object->host_resource(), expected_size)); | |
107 return PP_OK_COMPLETIONPENDING; | 102 return PP_OK_COMPLETIONPENDING; |
108 } | 103 } |
109 | 104 |
110 PP_FileSystemType_Dev GetType(PP_Resource resource) { | 105 PP_FileSystemType_Dev FileSystem::GetType() { |
111 FileSystem* object = PluginResource::GetAs<FileSystem>(resource); | 106 return type_; |
112 if (!object) | |
113 return PP_FILESYSTEMTYPE_INVALID; | |
114 return object->type_; | |
115 } | 107 } |
116 | 108 |
117 const PPB_FileSystem_Dev file_system_interface = { | 109 void FileSystem::OpenComplete(int32_t result) { |
118 &Create, | 110 PP_RunAndClearCompletionCallback(¤t_open_callback_, result); |
119 &IsFileSystem, | |
120 &Open, | |
121 &GetType | |
122 }; | |
123 | |
124 InterfaceProxy* CreateFileSystemProxy(Dispatcher* dispatcher, | |
125 const void* target_interface) { | |
126 return new PPB_FileSystem_Proxy(dispatcher, target_interface); | |
127 } | 111 } |
128 | 112 |
129 } // namespace | |
130 | |
131 PPB_FileSystem_Proxy::PPB_FileSystem_Proxy(Dispatcher* dispatcher, | 113 PPB_FileSystem_Proxy::PPB_FileSystem_Proxy(Dispatcher* dispatcher, |
132 const void* target_interface) | 114 const void* target_interface) |
133 : InterfaceProxy(dispatcher, target_interface), | 115 : InterfaceProxy(dispatcher, target_interface), |
134 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 116 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
135 } | 117 } |
136 | 118 |
137 PPB_FileSystem_Proxy::~PPB_FileSystem_Proxy() { | 119 PPB_FileSystem_Proxy::~PPB_FileSystem_Proxy() { |
138 } | 120 } |
139 | 121 |
140 const InterfaceProxy::Info* PPB_FileSystem_Proxy::GetInfo() { | 122 const InterfaceProxy::Info* PPB_FileSystem_Proxy::GetInfo() { |
141 static const Info info = { | 123 static const Info info = { |
142 &file_system_interface, | 124 ::ppapi::thunk::GetPPB_FileSystem_Thunk(), |
143 PPB_FILESYSTEM_DEV_INTERFACE, | 125 PPB_FILESYSTEM_DEV_INTERFACE, |
144 INTERFACE_ID_PPB_FILE_SYSTEM, | 126 INTERFACE_ID_PPB_FILE_SYSTEM, |
145 false, | 127 false, |
146 &CreateFileSystemProxy, | 128 &CreateFileSystemProxy, |
147 }; | 129 }; |
148 return &info; | 130 return &info; |
149 } | 131 } |
150 | 132 |
| 133 // static |
| 134 PP_Resource PPB_FileSystem_Proxy::CreateProxyResource( |
| 135 PP_Instance instance, |
| 136 PP_FileSystemType_Dev type) { |
| 137 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); |
| 138 if (!dispatcher) |
| 139 return PP_ERROR_BADARGUMENT; |
| 140 |
| 141 HostResource result; |
| 142 dispatcher->Send(new PpapiHostMsg_PPBFileSystem_Create( |
| 143 INTERFACE_ID_PPB_FILE_SYSTEM, instance, type, &result)); |
| 144 if (result.is_null()) |
| 145 return 0; |
| 146 |
| 147 linked_ptr<FileSystem> object(new FileSystem(result, type)); |
| 148 return PluginResourceTracker::GetInstance()->AddResource(object); |
| 149 } |
| 150 |
151 bool PPB_FileSystem_Proxy::OnMessageReceived(const IPC::Message& msg) { | 151 bool PPB_FileSystem_Proxy::OnMessageReceived(const IPC::Message& msg) { |
152 bool handled = true; | 152 bool handled = true; |
153 IPC_BEGIN_MESSAGE_MAP(PPB_FileSystem_Proxy, msg) | 153 IPC_BEGIN_MESSAGE_MAP(PPB_FileSystem_Proxy, msg) |
154 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileSystem_Create, OnMsgCreate) | 154 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileSystem_Create, OnMsgCreate) |
155 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileSystem_Open, OnMsgOpen) | 155 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileSystem_Open, OnMsgOpen) |
156 IPC_MESSAGE_HANDLER(PpapiMsg_PPBFileSystem_OpenComplete, OnMsgOpenComplete) | 156 IPC_MESSAGE_HANDLER(PpapiMsg_PPBFileSystem_OpenComplete, OnMsgOpenComplete) |
157 IPC_MESSAGE_UNHANDLED(handled = false) | 157 IPC_MESSAGE_UNHANDLED(handled = false) |
158 IPC_END_MESSAGE_MAP() | 158 IPC_END_MESSAGE_MAP() |
159 return handled; | 159 return handled; |
160 } | 160 } |
161 | 161 |
162 void PPB_FileSystem_Proxy::OnMsgCreate(PP_Instance instance, | 162 void PPB_FileSystem_Proxy::OnMsgCreate(PP_Instance instance, |
163 int type, | 163 int type, |
164 HostResource* result) { | 164 HostResource* result) { |
165 PP_Resource resource = ppb_file_system_target()->Create( | 165 EnterFunctionNoLock<ResourceCreationAPI> enter(instance, true); |
| 166 if (enter.failed()) |
| 167 return; |
| 168 PP_Resource resource = enter.functions()->CreateFileSystem( |
166 instance, static_cast<PP_FileSystemType_Dev>(type)); | 169 instance, static_cast<PP_FileSystemType_Dev>(type)); |
167 if (!resource) | 170 if (!resource) |
168 return; // CreateInfo default constructor initializes to 0. | 171 return; // CreateInfo default constructor initializes to 0. |
169 result->SetHostResource(instance, resource); | 172 result->SetHostResource(instance, resource); |
170 } | 173 } |
171 | 174 |
172 void PPB_FileSystem_Proxy::OnMsgOpen(const HostResource& host_resource, | 175 void PPB_FileSystem_Proxy::OnMsgOpen(const HostResource& host_resource, |
173 int64_t expected_size) { | 176 int64_t expected_size) { |
| 177 EnterHostFromHostResource<PPB_FileSystem_API> enter(host_resource); |
| 178 if (enter.failed()) |
| 179 return; |
| 180 |
174 CompletionCallback callback = callback_factory_.NewCallback( | 181 CompletionCallback callback = callback_factory_.NewCallback( |
175 &PPB_FileSystem_Proxy::OpenCompleteInHost, host_resource); | 182 &PPB_FileSystem_Proxy::OpenCompleteInHost, host_resource); |
176 | 183 int32_t result = enter.object()->Open(expected_size, |
177 int32_t result = ppb_file_system_target()->Open( | 184 callback.pp_completion_callback()); |
178 host_resource.host_resource(), expected_size, | |
179 callback.pp_completion_callback()); | |
180 if (result != PP_OK_COMPLETIONPENDING) | 185 if (result != PP_OK_COMPLETIONPENDING) |
181 callback.Run(result); | 186 callback.Run(result); |
182 } | 187 } |
183 | 188 |
184 // Called in the plugin to handle the open callback. | 189 // Called in the plugin to handle the open callback. |
185 void PPB_FileSystem_Proxy::OnMsgOpenComplete(const HostResource& filesystem, | 190 void PPB_FileSystem_Proxy::OnMsgOpenComplete(const HostResource& host_resource, |
186 int32_t result) { | 191 int32_t result) { |
187 FileSystem* object = PluginResource::GetAs<FileSystem>( | 192 EnterPluginFromHostResource<PPB_FileSystem_API> enter(host_resource); |
188 PluginResourceTracker::GetInstance()->PluginResourceForHostResource( | 193 if (enter.succeeded()) |
189 filesystem)); | 194 static_cast<FileSystem*>(enter.object())->OpenComplete(result); |
190 if (!object || !object->current_open_callback_.func) | |
191 return; | |
192 PP_RunAndClearCompletionCallback(&object->current_open_callback_, result); | |
193 } | 195 } |
194 | 196 |
195 void PPB_FileSystem_Proxy::OpenCompleteInHost( | 197 void PPB_FileSystem_Proxy::OpenCompleteInHost( |
196 int32_t result, | 198 int32_t result, |
197 const HostResource& host_resource) { | 199 const HostResource& host_resource) { |
198 dispatcher()->Send(new PpapiMsg_PPBFileSystem_OpenComplete( | 200 dispatcher()->Send(new PpapiMsg_PPBFileSystem_OpenComplete( |
199 INTERFACE_ID_PPB_FILE_SYSTEM, host_resource, result)); | 201 INTERFACE_ID_PPB_FILE_SYSTEM, host_resource, result)); |
200 } | 202 } |
201 | 203 |
202 } // namespace proxy | 204 } // namespace proxy |
203 } // namespace pp | 205 } // namespace pp |
OLD | NEW |