OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_chooser_proxy.h" | 5 #include "ppapi/proxy/ppb_file_chooser_proxy.h" |
6 | 6 |
7 #include <queue> | 7 #include <queue> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "ppapi/c/dev/ppb_file_chooser_dev.h" | 10 #include "ppapi/c/dev/ppb_file_chooser_dev.h" |
11 #include "ppapi/c/pp_errors.h" | 11 #include "ppapi/c/pp_errors.h" |
12 #include "ppapi/c/private/ppb_proxy_private.h" | 12 #include "ppapi/c/private/ppb_proxy_private.h" |
13 #include "ppapi/c/trusted/ppb_file_chooser_trusted.h" | 13 #include "ppapi/c/trusted/ppb_file_chooser_trusted.h" |
14 #include "ppapi/proxy/enter_proxy.h" | 14 #include "ppapi/proxy/enter_proxy.h" |
15 #include "ppapi/proxy/host_dispatcher.h" | 15 #include "ppapi/proxy/host_dispatcher.h" |
16 #include "ppapi/proxy/plugin_dispatcher.h" | 16 #include "ppapi/proxy/plugin_dispatcher.h" |
17 #include "ppapi/proxy/ppapi_messages.h" | 17 #include "ppapi/proxy/ppapi_messages.h" |
18 #include "ppapi/proxy/ppb_file_ref_proxy.h" | 18 #include "ppapi/proxy/ppb_file_ref_proxy.h" |
19 #include "ppapi/proxy/serialized_var.h" | 19 #include "ppapi/proxy/serialized_var.h" |
| 20 #include "ppapi/shared_impl/array_writer.h" |
20 #include "ppapi/shared_impl/ppapi_globals.h" | 21 #include "ppapi/shared_impl/ppapi_globals.h" |
21 #include "ppapi/shared_impl/resource_tracker.h" | 22 #include "ppapi/shared_impl/resource_tracker.h" |
22 #include "ppapi/shared_impl/tracked_callback.h" | 23 #include "ppapi/shared_impl/tracked_callback.h" |
23 #include "ppapi/shared_impl/var.h" | 24 #include "ppapi/shared_impl/var.h" |
24 #include "ppapi/thunk/resource_creation_api.h" | 25 #include "ppapi/thunk/resource_creation_api.h" |
25 #include "ppapi/thunk/thunk.h" | 26 #include "ppapi/thunk/thunk.h" |
26 | 27 |
27 using ppapi::thunk::PPB_FileChooser_API; | 28 using ppapi::thunk::PPB_FileChooser_API; |
28 | 29 |
29 namespace ppapi { | 30 namespace ppapi { |
30 namespace proxy { | 31 namespace proxy { |
31 | 32 |
32 namespace { | 33 namespace { |
33 InterfaceProxy* CreateFileChooserProxy(Dispatcher* dispatcher) { | 34 InterfaceProxy* CreateFileChooserProxy(Dispatcher* dispatcher) { |
34 return new PPB_FileChooser_Proxy(dispatcher); | 35 return new PPB_FileChooser_Proxy(dispatcher); |
35 } | 36 } |
36 | 37 |
37 class FileChooser : public Resource, | 38 class FileChooser : public Resource, |
38 public PPB_FileChooser_API { | 39 public PPB_FileChooser_API { |
39 public: | 40 public: |
40 FileChooser(const HostResource& resource); | 41 FileChooser(const HostResource& resource); |
41 virtual ~FileChooser(); | 42 virtual ~FileChooser(); |
42 | 43 |
43 // Resource overrides. | 44 // Resource overrides. |
44 virtual PPB_FileChooser_API* AsPPB_FileChooser_API() OVERRIDE; | 45 virtual PPB_FileChooser_API* AsPPB_FileChooser_API() OVERRIDE; |
45 | 46 |
46 // PPB_FileChooser_API implementation. | 47 // PPB_FileChooser_API implementation. |
47 virtual int32_t Show(const PP_CompletionCallback& callback) OVERRIDE; | 48 virtual int32_t Show(const PP_ArrayOutput& output, |
| 49 const PP_CompletionCallback& callback) OVERRIDE; |
| 50 virtual int32_t ShowWithoutUserGesture( |
| 51 PP_Bool save_as, |
| 52 PP_Var suggested_file_name, |
| 53 const PP_ArrayOutput& output, |
| 54 const PP_CompletionCallback& callback); |
| 55 virtual int32_t Show0_5(const PP_CompletionCallback& callback) OVERRIDE; |
48 virtual PP_Resource GetNextChosenFile() OVERRIDE; | 56 virtual PP_Resource GetNextChosenFile() OVERRIDE; |
49 virtual int32_t ShowWithoutUserGesture( | 57 virtual int32_t ShowWithoutUserGesture0_5( |
50 bool save_as, | 58 PP_Bool save_as, |
51 const char* suggested_file_name, | 59 PP_Var suggested_file_name, |
52 const PP_CompletionCallback& callback) OVERRIDE; | 60 const PP_CompletionCallback& callback) OVERRIDE; |
53 | 61 |
54 // Handles the choose complete notification from the host. | 62 // Handles the choose complete notification from the host. |
55 void ChooseComplete( | 63 void ChooseComplete( |
56 int32_t result_code, | 64 int32_t result_code, |
57 const std::vector<PPB_FileRef_CreateInfo>& chosen_files); | 65 const std::vector<PPB_FileRef_CreateInfo>& chosen_files); |
58 | 66 |
59 private: | 67 private: |
60 int32_t Show(bool require_user_gesture, | 68 int32_t Show(bool require_user_gesture, |
61 bool save_as, | 69 PP_Bool save_as, |
62 const char* suggested_file_name, | 70 PP_Var suggested_file_name, |
63 const PP_CompletionCallback& callback); | 71 const PP_CompletionCallback& callback); |
64 | 72 |
| 73 // When using v0.6 of the API, contains the array output info. |
| 74 ArrayWriter output_; |
| 75 |
65 scoped_refptr<TrackedCallback> current_show_callback_; | 76 scoped_refptr<TrackedCallback> current_show_callback_; |
66 | 77 |
67 // All files returned by the current show callback that haven't yet been | 78 // When using v0.5 of the API, contains all files returned by the current |
68 // given to the plugin. The plugin will repeatedly call us to get the next | 79 // show callback that haven't yet been given to the plugin. The plugin will |
69 // file, and we'll vend those out of this queue, removing them when ownership | 80 // repeatedly call us to get the next file, and we'll vend those out of this |
70 // has transferred to the plugin. | 81 // queue, removing them when ownership has transferred to the plugin. |
71 std::queue<PP_Resource> file_queue_; | 82 std::queue<PP_Resource> file_queue_; |
72 | 83 |
73 DISALLOW_COPY_AND_ASSIGN(FileChooser); | 84 DISALLOW_COPY_AND_ASSIGN(FileChooser); |
74 }; | 85 }; |
75 | 86 |
76 FileChooser::FileChooser(const HostResource& resource) | 87 FileChooser::FileChooser(const HostResource& resource) |
77 : Resource(OBJECT_IS_PROXY, resource) { | 88 : Resource(OBJECT_IS_PROXY, resource) { |
78 } | 89 } |
79 | 90 |
80 FileChooser::~FileChooser() { | 91 FileChooser::~FileChooser() { |
81 // Any existing files we haven't transferred ownership to the plugin need | 92 // Any existing files we haven't transferred ownership to the plugin need |
82 // to be freed. | 93 // to be freed. |
83 ResourceTracker* tracker = PpapiGlobals::Get()->GetResourceTracker(); | 94 ResourceTracker* tracker = PpapiGlobals::Get()->GetResourceTracker(); |
84 while (!file_queue_.empty()) { | 95 while (!file_queue_.empty()) { |
85 tracker->ReleaseResource(file_queue_.front()); | 96 tracker->ReleaseResource(file_queue_.front()); |
86 file_queue_.pop(); | 97 file_queue_.pop(); |
87 } | 98 } |
88 } | 99 } |
89 | 100 |
90 PPB_FileChooser_API* FileChooser::AsPPB_FileChooser_API() { | 101 PPB_FileChooser_API* FileChooser::AsPPB_FileChooser_API() { |
91 return this; | 102 return this; |
92 } | 103 } |
93 | 104 |
94 int32_t FileChooser::Show(const PP_CompletionCallback& callback) { | 105 int32_t FileChooser::Show(const PP_ArrayOutput& output, |
95 return Show(true, false, NULL, callback); | 106 const PP_CompletionCallback& callback) { |
| 107 int32_t result = Show(true, PP_FALSE, PP_MakeUndefined(), callback); |
| 108 if (result == PP_OK_COMPLETIONPENDING) |
| 109 output_.set_pp_array_output(output); |
| 110 return result; |
96 } | 111 } |
97 | 112 |
98 int32_t FileChooser::ShowWithoutUserGesture( | 113 int32_t FileChooser::ShowWithoutUserGesture( |
99 bool save_as, | 114 PP_Bool save_as, |
100 const char* suggested_file_name, | 115 PP_Var suggested_file_name, |
| 116 const PP_ArrayOutput& output, |
| 117 const PP_CompletionCallback& callback) { |
| 118 int32_t result = Show(false, save_as, PP_MakeUndefined(), callback); |
| 119 if (result == PP_OK_COMPLETIONPENDING) |
| 120 output_.set_pp_array_output(output); |
| 121 return result; |
| 122 } |
| 123 |
| 124 int32_t FileChooser::Show0_5(const PP_CompletionCallback& callback) { |
| 125 return Show(true, PP_FALSE, PP_MakeUndefined(), callback); |
| 126 } |
| 127 |
| 128 int32_t FileChooser::ShowWithoutUserGesture0_5( |
| 129 PP_Bool save_as, |
| 130 PP_Var suggested_file_name, |
101 const PP_CompletionCallback& callback) { | 131 const PP_CompletionCallback& callback) { |
102 return Show(false, save_as, suggested_file_name, callback); | 132 return Show(false, save_as, suggested_file_name, callback); |
103 } | 133 } |
104 | 134 |
105 int32_t FileChooser::Show(bool require_user_gesture, | 135 int32_t FileChooser::Show(bool require_user_gesture, |
106 bool save_as, | 136 PP_Bool save_as, |
107 const char* suggested_file_name, | 137 PP_Var suggested_file_name, |
108 const PP_CompletionCallback& callback) { | 138 const PP_CompletionCallback& callback) { |
109 if (!callback.func) | 139 if (!callback.func) |
110 return PP_ERROR_BLOCKS_MAIN_THREAD; | 140 return PP_ERROR_BLOCKS_MAIN_THREAD; |
111 | 141 |
112 if (TrackedCallback::IsPending(current_show_callback_)) | 142 if (TrackedCallback::IsPending(current_show_callback_)) |
113 return PP_ERROR_INPROGRESS; // Can't show more than once. | 143 return PP_ERROR_INPROGRESS; // Can't show more than once. |
114 | 144 |
115 current_show_callback_ = new TrackedCallback(this, callback); | 145 current_show_callback_ = new TrackedCallback(this, callback); |
116 PluginDispatcher::GetForResource(this)->Send( | 146 PluginDispatcher* dispatcher = PluginDispatcher::GetForResource(this); |
| 147 dispatcher->Send( |
117 new PpapiHostMsg_PPBFileChooser_Show( | 148 new PpapiHostMsg_PPBFileChooser_Show( |
118 API_ID_PPB_FILE_CHOOSER, | 149 API_ID_PPB_FILE_CHOOSER, |
119 host_resource(), | 150 host_resource(), |
120 save_as, | 151 save_as, |
121 suggested_file_name ? suggested_file_name : "", | 152 SerializedVarSendInput(dispatcher, suggested_file_name), |
122 require_user_gesture)); | 153 require_user_gesture)); |
123 return PP_OK_COMPLETIONPENDING; | 154 return PP_OK_COMPLETIONPENDING; |
124 } | 155 } |
125 | 156 |
126 PP_Resource FileChooser::GetNextChosenFile() { | 157 PP_Resource FileChooser::GetNextChosenFile() { |
127 if (file_queue_.empty()) | 158 if (file_queue_.empty()) |
128 return 0; | 159 return 0; |
129 | 160 |
130 // Return the next resource in the queue. These resource have already been | 161 // Return the next resource in the queue. These resource have already been |
131 // addrefed (they're currently owned by the FileChooser) and returning them | 162 // addrefed (they're currently owned by the FileChooser) and returning them |
132 // transfers ownership of that reference to the plugin. | 163 // transfers ownership of that reference to the plugin. |
133 PP_Resource next = file_queue_.front(); | 164 PP_Resource next = file_queue_.front(); |
134 file_queue_.pop(); | 165 file_queue_.pop(); |
135 return next; | 166 return next; |
136 } | 167 } |
137 | 168 |
138 void FileChooser::ChooseComplete( | 169 void FileChooser::ChooseComplete( |
139 int32_t result_code, | 170 int32_t result_code, |
140 const std::vector<PPB_FileRef_CreateInfo>& chosen_files) { | 171 const std::vector<PPB_FileRef_CreateInfo>& chosen_files) { |
141 // Convert each of the passed in file infos to resources. These will be owned | 172 if (output_.is_valid()) { |
142 // by the FileChooser object until they're passed to the plugin. | 173 // Using v0.6 of the API with the output array. |
143 DCHECK(file_queue_.empty()); | 174 std::vector<PP_Resource> files; |
144 for (size_t i = 0; i < chosen_files.size(); i++) | 175 for (size_t i = 0; i < chosen_files.size(); i++) |
145 file_queue_.push(PPB_FileRef_Proxy::DeserializeFileRef(chosen_files[i])); | 176 files.push_back(PPB_FileRef_Proxy::DeserializeFileRef(chosen_files[i])); |
| 177 output_.StoreResourceVector(files); |
| 178 } else { |
| 179 // Convert each of the passed in file infos to resources. These will be |
| 180 // owned by the FileChooser object until they're passed to the plugin. |
| 181 DCHECK(file_queue_.empty()); |
| 182 for (size_t i = 0; i < chosen_files.size(); i++) { |
| 183 file_queue_.push(PPB_FileRef_Proxy::DeserializeFileRef( |
| 184 chosen_files[i])); |
| 185 } |
| 186 } |
146 | 187 |
147 // Notify the plugin of the new data. | 188 // Notify the plugin of the new data. |
148 TrackedCallback::ClearAndRun(¤t_show_callback_, result_code); | 189 TrackedCallback::ClearAndRun(¤t_show_callback_, result_code); |
149 // DANGER: May delete |this|! | 190 // DANGER: May delete |this|! |
150 } | 191 } |
151 | 192 |
152 } // namespace | 193 } // namespace |
153 | 194 |
154 PPB_FileChooser_Proxy::PPB_FileChooser_Proxy(Dispatcher* dispatcher) | 195 PPB_FileChooser_Proxy::PPB_FileChooser_Proxy(Dispatcher* dispatcher) |
155 : InterfaceProxy(dispatcher), | 196 : InterfaceProxy(dispatcher), |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 if (enter.succeeded()) { | 257 if (enter.succeeded()) { |
217 result->SetHostResource(instance, enter.functions()->CreateFileChooser( | 258 result->SetHostResource(instance, enter.functions()->CreateFileChooser( |
218 instance, | 259 instance, |
219 static_cast<PP_FileChooserMode_Dev>(mode), | 260 static_cast<PP_FileChooserMode_Dev>(mode), |
220 accept_mime_types.c_str())); | 261 accept_mime_types.c_str())); |
221 } | 262 } |
222 } | 263 } |
223 | 264 |
224 void PPB_FileChooser_Proxy::OnMsgShow( | 265 void PPB_FileChooser_Proxy::OnMsgShow( |
225 const HostResource& chooser, | 266 const HostResource& chooser, |
226 bool save_as, | 267 PP_Bool save_as, |
227 std::string suggested_file_name, | 268 SerializedVarReceiveInput suggested_file_name, |
228 bool require_user_gesture) { | 269 bool require_user_gesture) { |
| 270 scoped_refptr<RefCountedArrayOutputAdapter<PP_Resource> > output( |
| 271 new RefCountedArrayOutputAdapter<PP_Resource>); |
229 EnterHostFromHostResourceForceCallback<PPB_FileChooser_API> enter( | 272 EnterHostFromHostResourceForceCallback<PPB_FileChooser_API> enter( |
230 chooser, callback_factory_, &PPB_FileChooser_Proxy::OnShowCallback, | 273 chooser, |
231 chooser); | 274 callback_factory_.NewOptionalCallback( |
| 275 &PPB_FileChooser_Proxy::OnShowCallback, output, chooser)); |
232 if (enter.succeeded()) { | 276 if (enter.succeeded()) { |
233 if (require_user_gesture) { | 277 if (require_user_gesture) { |
234 enter.SetResult(enter.object()->Show(enter.callback())); | 278 enter.SetResult(enter.object()->Show(output->pp_array_output(), |
| 279 enter.callback())); |
235 } else { | 280 } else { |
236 enter.SetResult(enter.object()->ShowWithoutUserGesture( | 281 enter.SetResult(enter.object()->ShowWithoutUserGesture( |
237 save_as, | 282 save_as, |
238 suggested_file_name.c_str(), | 283 suggested_file_name.Get(dispatcher()), |
| 284 output->pp_array_output(), |
239 enter.callback())); | 285 enter.callback())); |
240 } | 286 } |
241 } | 287 } |
242 } | 288 } |
243 | 289 |
244 void PPB_FileChooser_Proxy::OnMsgChooseComplete( | 290 void PPB_FileChooser_Proxy::OnMsgChooseComplete( |
245 const HostResource& chooser, | 291 const HostResource& chooser, |
246 int32_t result_code, | 292 int32_t result_code, |
247 const std::vector<PPB_FileRef_CreateInfo>& chosen_files) { | 293 const std::vector<PPB_FileRef_CreateInfo>& chosen_files) { |
248 EnterPluginFromHostResource<PPB_FileChooser_API> enter(chooser); | 294 EnterPluginFromHostResource<PPB_FileChooser_API> enter(chooser); |
249 if (enter.succeeded()) { | 295 if (enter.succeeded()) { |
250 static_cast<FileChooser*>(enter.object())->ChooseComplete( | 296 static_cast<FileChooser*>(enter.object())->ChooseComplete( |
251 result_code, chosen_files); | 297 result_code, chosen_files); |
252 } | 298 } |
253 } | 299 } |
254 | 300 |
255 void PPB_FileChooser_Proxy::OnShowCallback(int32_t result, | 301 void PPB_FileChooser_Proxy::OnShowCallback( |
256 const HostResource& chooser) { | 302 int32_t result, |
| 303 scoped_refptr<RefCountedArrayOutputAdapter<PP_Resource> > |
| 304 output, |
| 305 HostResource chooser) { |
257 EnterHostFromHostResource<PPB_FileChooser_API> enter(chooser); | 306 EnterHostFromHostResource<PPB_FileChooser_API> enter(chooser); |
258 | 307 |
259 std::vector<PPB_FileRef_CreateInfo> files; | 308 std::vector<PPB_FileRef_CreateInfo> files; |
260 if (enter.succeeded() && result == PP_OK) { | 309 if (enter.succeeded() && result == PP_OK) { |
261 PPB_FileRef_Proxy* file_ref_proxy = static_cast<PPB_FileRef_Proxy*>( | 310 PPB_FileRef_Proxy* file_ref_proxy = static_cast<PPB_FileRef_Proxy*>( |
262 dispatcher()->GetInterfaceProxy(API_ID_PPB_FILE_REF)); | 311 dispatcher()->GetInterfaceProxy(API_ID_PPB_FILE_REF)); |
263 | 312 |
264 // Convert the returned files to the serialized info. | 313 // Convert the returned files to the serialized info. |
265 while (PP_Resource cur_file_resource = | 314 ResourceTracker* tracker = PpapiGlobals::Get()->GetResourceTracker(); |
266 enter.object()->GetNextChosenFile()) { | 315 for (size_t i = 0; i < output->output().size(); i++) { |
267 PPB_FileRef_CreateInfo cur_create_info; | 316 PPB_FileRef_CreateInfo cur_create_info; |
268 file_ref_proxy->SerializeFileRef(cur_file_resource, &cur_create_info); | 317 file_ref_proxy->SerializeFileRef(output->output()[i], &cur_create_info); |
269 files.push_back(cur_create_info); | 318 files.push_back(cur_create_info); |
| 319 |
| 320 // Done with this resource, caller gave us a ref. |
| 321 tracker->ReleaseResource(output->output()[i]); |
270 } | 322 } |
271 } | 323 } |
272 | 324 |
273 dispatcher()->Send(new PpapiMsg_PPBFileChooser_ChooseComplete( | 325 dispatcher()->Send(new PpapiMsg_PPBFileChooser_ChooseComplete( |
274 API_ID_PPB_FILE_CHOOSER, chooser, result, files)); | 326 API_ID_PPB_FILE_CHOOSER, chooser, result, files)); |
275 } | 327 } |
276 | 328 |
277 } // namespace proxy | 329 } // namespace proxy |
278 } // namespace ppapi | 330 } // namespace ppapi |
OLD | NEW |