OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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. |
| 4 #ifndef FILESYSTEM_PROVIDER_RESOURCE_H |
| 5 #define FILESYSTEM_PROVIDER_RESOURCE_H |
| 6 |
| 7 #include <chrono> |
| 8 #include <map> |
| 9 |
| 10 #include "ppapi/proxy/connection.h" |
| 11 #include "ppapi/proxy/plugin_resource.h" |
| 12 #include "ppapi/proxy/ppapi_proxy_export.h" |
| 13 #include "ppapi/shared_impl/resource.h" |
| 14 #include "ppapi/thunk/ppb_filesystem_provider_api.h" |
| 15 |
| 16 namespace ppapi { |
| 17 namespace proxy { |
| 18 |
| 19 namespace filesystem_provider_internal{ |
| 20 struct OperationTranslator; |
| 21 } // namespace |
| 22 class PPAPI_PROXY_EXPORT FilesystemProviderResource |
| 23 :public PluginResource, |
| 24 public NON_EXPORTED_BASE(thunk::PPB_FilesystemProvider_API) { |
| 25 private: |
| 26 // Waiting room for shared memory |
| 27 // This is used when sending read file responses to the browser |
| 28 class ShmChannelSynchronizer{ |
| 29 typedef base::Callback<bool(scoped_ptr<base::ListValue>, const char* data, |
| 30 uint32_t data_size)> |
| 31 FlushResponseCallback; |
| 32 public: |
| 33 ShmChannelSynchronizer( uint32_t size, FlushResponseCallback callback ); |
| 34 ~ShmChannelSynchronizer(); |
| 35 // This will flush send the re |
| 36 bool EnqueueResponse(scoped_ptr<base::ListValue> response, |
| 37 const char* data, uint32_t data_size); |
| 38 bool PushNextResponse(); |
| 39 bool AckLastPushedResponse(); |
| 40 private: |
| 41 uint32_t size(); |
| 42 uint32_t max_size; |
| 43 ScopedVector<base::ListValue> replies; |
| 44 bool ready; |
| 45 FlushResponseCallback callback; |
| 46 }; |
| 47 // All the information we need about the filesystem is stored here |
| 48 struct ProvidedFilesystemInfo{ |
| 49 ProvidedFilesystemInfo(); |
| 50 ~ProvidedFilesystemInfo(); |
| 51 std::string filesystem_id; |
| 52 std::string display_name; |
| 53 bool writable; |
| 54 int32_t opened_files_limit; |
| 55 }; |
| 56 // Shared memory container |
| 57 struct ShmBuffer { |
| 58 ShmBuffer(uint32_t read_size, |
| 59 uint32_t write_size, scoped_ptr<base::SharedMemory> shm); |
| 60 ~ShmBuffer(); |
| 61 uint32_t read_size; |
| 62 uint32_t write_size; |
| 63 scoped_ptr<base::SharedMemory> shm; |
| 64 }; |
| 65 // An interface used for statistics and ill intended |
| 66 // notifications from the plugin implementation.Basically this will filter out |
| 67 // operation responses that don't have an operation request from the browser. |
| 68 class RequestManager{ |
| 69 private: |
| 70 // Current Requests typedefs |
| 71 typedef std::pair<PP_OperationType_Dev, |
| 72 std::chrono::time_point<std::chrono::system_clock>> |
| 73 OperationStartTimePair; |
| 74 typedef std::map<int32_t, OperationStartTimePair > |
| 75 ListOfRequests; |
| 76 typedef ListOfRequests::iterator ListOfRequestsIterator; |
| 77 |
| 78 public: |
| 79 RequestManager(); |
| 80 ~RequestManager(); |
| 81 // Adds a request |
| 82 void AddRequest( int32_t request_id, PP_OperationType_Dev operation ); |
| 83 // Removes a request. This is successful only if the request previously |
| 84 // existed. The out_time_span contains the time in seconds of the operation |
| 85 bool RemoveRequest( int32_t request_id, double* out_time_span); |
| 86 |
| 87 private: |
| 88 ListOfRequests requests_; |
| 89 }; |
| 90 public: |
| 91 FilesystemProviderResource( |
| 92 Connection connection, |
| 93 PP_Instance instance ); |
| 94 virtual ~FilesystemProviderResource(); |
| 95 // PluginResource overrides: |
| 96 virtual thunk::PPB_FilesystemProvider_API* |
| 97 AsPPB_FilesystemProvider_API() override; |
| 98 void OnReplyReceived( |
| 99 const ResourceMessageReplyParams ¶ms, |
| 100 const IPC::Message &msg) override; |
| 101 // PPB_FilesystemProvider_API overrides |
| 102 // The below functions will be called from the instance module |
| 103 // to notify the browser or to request certain actions from the browser |
| 104 virtual int32_t Mount( |
| 105 struct PP_Var filesystem_id, |
| 106 struct PP_Var display_name, |
| 107 PP_Bool writable, |
| 108 int32_t opened_files_limit, |
| 109 PP_ProviderError_Dev* error, |
| 110 scoped_refptr<TrackedCallback> callback) override; |
| 111 virtual int32_t Unmount( |
| 112 struct PP_Var filesystemId, |
| 113 PP_ProviderError_Dev* error, |
| 114 scoped_refptr<TrackedCallback> callback) override; |
| 115 |
| 116 virtual int32_t SendSuccessResponse( |
| 117 PP_OperationType_Dev operation_type, |
| 118 int32_t request_id) override; |
| 119 virtual int32_t SendErrorResponse( |
| 120 PP_OperationType_Dev operation_type, |
| 121 PP_ProviderError_Dev error, |
| 122 int32_t request_id) override; |
| 123 |
| 124 virtual int32_t SendMetadataSuccessResponse( |
| 125 const struct PP_EntryMetadata_Dev* metadata, |
| 126 int32_t request_id) override; |
| 127 virtual int32_t SendReadDirectorySuccessResponse( |
| 128 uint32_t array_size, |
| 129 const struct PP_EntryMetadata_Dev entries[], |
| 130 PP_Bool has_more, |
| 131 int32_t request_id) override; |
| 132 virtual int32_t SendReadFileSuccessResponse( |
| 133 uint32_t data_size, |
| 134 const void* data, |
| 135 PP_Bool has_more, |
| 136 int32_t request_id) override; |
| 137 virtual int32_t GetNextRequest( |
| 138 PP_FilesystemRequest *request, |
| 139 scoped_refptr<TrackedCallback> callback)override; |
| 140 virtual int32_t FreeWriteRequestBuffer(const void *buffer)override; |
| 141 |
| 142 |
| 143 private: |
| 144 // Message Handlers for the browser messages |
| 145 void OnPluginMsgMountReply( |
| 146 const ResourceMessageReplyParams& params, |
| 147 PP_ProviderError_Dev error); |
| 148 void OnPluginMsgUnmountReply( |
| 149 const ResourceMessageReplyParams& params, |
| 150 PP_ProviderError_Dev error); |
| 151 // Handles the file system operations |
| 152 // requested by the browser |
| 153 void OnPluginMsgOperationRequest( |
| 154 const ResourceMessageParams& params, |
| 155 const PP_OperationType_Dev& operation, |
| 156 const base::ListValue& operationArgs); |
| 157 void OnPluginMsgBuffersReady(const ResourceMessageParams& params, |
| 158 uint32_t buffer_length, uint32_t write_buffer_size); |
| 159 void OnPluginMsgReadAck( |
| 160 const ResourceMessageReplyParams& params); |
| 161 |
| 162 // Remember a request in the provided container address |
| 163 bool WriteRequest(); |
| 164 // Send Read response to browser |
| 165 bool FlushReadResponse(scoped_ptr<base::ListValue> response, |
| 166 const char *data, |
| 167 uint32_t data_size); |
| 168 // The filesystem information |
| 169 ProvidedFilesystemInfo filesystem_info_; |
| 170 // Flag for the state of the filesystem |
| 171 bool mounted_; |
| 172 // User callbacks |
| 173 scoped_refptr<TrackedCallback> mount_callback_; |
| 174 scoped_refptr<TrackedCallback> unmount_callback_; |
| 175 scoped_refptr<TrackedCallback> get_next_request_callback_; |
| 176 // State for the above callbacks |
| 177 PP_ProviderError_Dev* mount_callback_var_; |
| 178 PP_ProviderError_Dev* unmount_callback_var_; |
| 179 struct PP_FilesystemRequest* get_next_request_callback_var_; |
| 180 // Keep received requests until consumed |
| 181 // by calling GetNextRequest |
| 182 ScopedVector<filesystem_provider_internal::OperationTranslator> |
| 183 received_requests_; |
| 184 // Received write requests that are buffered from when GetNextRequest |
| 185 // is called until FreeWriteRequestBuffer is called. |
| 186 ScopedVector<filesystem_provider_internal::OperationTranslator> |
| 187 received_write_requests_; |
| 188 // Shared memory buffer |
| 189 scoped_ptr<ShmBuffer> read_write_buffer_; |
| 190 // A RequestManager that controls access to the shared memory write |
| 191 scoped_ptr<FilesystemProviderResource::RequestManager> request_manager_; |
| 192 // Cacher for replys that content for the shared mem buffer |
| 193 scoped_ptr<ShmChannelSynchronizer> read_channel_controller_; |
| 194 DISALLOW_COPY_AND_ASSIGN( FilesystemProviderResource ); |
| 195 }; |
| 196 |
| 197 namespace filesystem_provider_internal{ |
| 198 |
| 199 struct OperationTranslator{ |
| 200 virtual ~OperationTranslator(); |
| 201 virtual scoped_ptr<PP_FilesystemRequest> ToFilesystemRequest()=0; |
| 202 }; |
| 203 struct AbortOperationTranslator : public OperationTranslator{ |
| 204 virtual ~AbortOperationTranslator()override{} |
| 205 virtual scoped_ptr<PP_FilesystemRequest> ToFilesystemRequest()override; |
| 206 static |
| 207 scoped_ptr< AbortOperationTranslator > PopulateFromRequest( |
| 208 const base::ListValue& request ); |
| 209 // The identifier of the file system related to this operation. |
| 210 std::string file_system_id; |
| 211 // The unique identifier of this request. |
| 212 int request_id; |
| 213 // An ID of the request to be aborted. |
| 214 int operation_request_id; |
| 215 private: |
| 216 AbortOperationTranslator() {} |
| 217 DISALLOW_COPY_AND_ASSIGN(AbortOperationTranslator); |
| 218 }; |
| 219 |
| 220 struct CloseFileOperationTranslator : public OperationTranslator{ |
| 221 ~CloseFileOperationTranslator()override{} |
| 222 virtual scoped_ptr<PP_FilesystemRequest> ToFilesystemRequest()override; |
| 223 // Creates a CloseFileRequestedOptions object from a base::Value, or NULL on |
| 224 // failure. |
| 225 static |
| 226 scoped_ptr<CloseFileOperationTranslator> PopulateFromRequest( |
| 227 const base::ListValue& request); |
| 228 // The identifier of the file system related to this operation. |
| 229 std::string file_system_id; |
| 230 // The unique identifier of this request. |
| 231 int request_id; |
| 232 // A request ID used to open the file. |
| 233 int open_request_id; |
| 234 private: |
| 235 CloseFileOperationTranslator(){} |
| 236 DISALLOW_COPY_AND_ASSIGN(CloseFileOperationTranslator); |
| 237 }; |
| 238 |
| 239 struct CopyEntryOperationTranslator : public OperationTranslator{ |
| 240 ~CopyEntryOperationTranslator() override {} |
| 241 virtual scoped_ptr<PP_FilesystemRequest> ToFilesystemRequest() override; |
| 242 static scoped_ptr<CopyEntryOperationTranslator> PopulateFromRequest( |
| 243 const base::ListValue& request); |
| 244 // The identifier of the file system related to this operation. |
| 245 std::string file_system_id; |
| 246 // The unique identifier of this request. |
| 247 int request_id; |
| 248 // The source path of the entry to be copied. |
| 249 std::string source_path; |
| 250 // The destination path for the copy operation. |
| 251 std::string target_path; |
| 252 private: |
| 253 CopyEntryOperationTranslator() {} |
| 254 DISALLOW_COPY_AND_ASSIGN(CopyEntryOperationTranslator); |
| 255 }; |
| 256 |
| 257 struct CreateDirectoryOperationTranslator : public OperationTranslator { |
| 258 ~CreateDirectoryOperationTranslator()override{} |
| 259 virtual scoped_ptr<PP_FilesystemRequest> ToFilesystemRequest()override; |
| 260 static scoped_ptr<CreateDirectoryOperationTranslator> PopulateFromRequest( |
| 261 const base::ListValue& request ); |
| 262 // The identifier of the file system related to this operation. |
| 263 std::string file_system_id; |
| 264 // The unique identifier of this request. |
| 265 int request_id; |
| 266 // The path of the directory to be created. |
| 267 std::string directory_path; |
| 268 // Whether the operation is recursive (for directories only). |
| 269 bool recursive; |
| 270 private: |
| 271 CreateDirectoryOperationTranslator() {} |
| 272 DISALLOW_COPY_AND_ASSIGN(CreateDirectoryOperationTranslator); |
| 273 }; |
| 274 |
| 275 struct CreateFileOperationTranslator : public OperationTranslator{ |
| 276 ~CreateFileOperationTranslator() override {} |
| 277 virtual scoped_ptr<PP_FilesystemRequest> ToFilesystemRequest()override; |
| 278 static scoped_ptr<CreateFileOperationTranslator> PopulateFromRequest( |
| 279 const base::ListValue &request ); |
| 280 // The identifier of the file system related to this operation. |
| 281 std::string file_system_id; |
| 282 // The unique identifier of this request. |
| 283 int request_id; |
| 284 // The path of the file to be created. |
| 285 std::string file_path; |
| 286 private: |
| 287 CreateFileOperationTranslator() {} |
| 288 DISALLOW_COPY_AND_ASSIGN(CreateFileOperationTranslator); |
| 289 }; |
| 290 |
| 291 struct DeleteEntryOperationTranslator : public OperationTranslator{ |
| 292 ~DeleteEntryOperationTranslator()override{} |
| 293 virtual scoped_ptr<PP_FilesystemRequest> ToFilesystemRequest()override; |
| 294 static scoped_ptr<DeleteEntryOperationTranslator> PopulateFromRequest( |
| 295 const base::ListValue& request ); |
| 296 // The identifier of the file system related to this operation. |
| 297 std::string file_system_id; |
| 298 // The unique identifier of this request. |
| 299 int request_id; |
| 300 // The path of the entry to be deleted. |
| 301 std::string entry_path; |
| 302 // Whether the operation is recursive (for directories only). |
| 303 bool recursive; |
| 304 private: |
| 305 DeleteEntryOperationTranslator(){} |
| 306 DISALLOW_COPY_AND_ASSIGN(DeleteEntryOperationTranslator); |
| 307 }; |
| 308 |
| 309 struct GetMetadataOperationTranslator:public OperationTranslator{ |
| 310 ~GetMetadataOperationTranslator()override{} |
| 311 virtual scoped_ptr<PP_FilesystemRequest> ToFilesystemRequest()override; |
| 312 static scoped_ptr<GetMetadataOperationTranslator> PopulateFromRequest( |
| 313 const base::ListValue& request ); |
| 314 std::string file_system_id; |
| 315 // The unique identifier of this request. |
| 316 int request_id; |
| 317 // The path of the entry to fetch metadata about. |
| 318 std::string entry_path; |
| 319 // Set to <code>true</code> if the thumbnail is requested. |
| 320 bool thumbnail; |
| 321 private: |
| 322 GetMetadataOperationTranslator() {} |
| 323 DISALLOW_COPY_AND_ASSIGN( GetMetadataOperationTranslator ); |
| 324 }; |
| 325 |
| 326 struct MoveOperationTranslator : public OperationTranslator { |
| 327 ~MoveOperationTranslator()override{} |
| 328 virtual scoped_ptr<PP_FilesystemRequest> ToFilesystemRequest()override; |
| 329 static scoped_ptr<MoveOperationTranslator> PopulateFromRequest( |
| 330 const base::ListValue& request ); |
| 331 // The identifier of the file system related to this operation. |
| 332 std::string file_system_id; |
| 333 // The unique identifier of this request. |
| 334 int request_id; |
| 335 // The source path of the entry to be moved into a new place. |
| 336 std::string source_path; |
| 337 // The destination path for the copy operation. |
| 338 std::string target_path; |
| 339 private: |
| 340 MoveOperationTranslator() {} |
| 341 DISALLOW_COPY_AND_ASSIGN(MoveOperationTranslator); |
| 342 }; |
| 343 |
| 344 struct OpenFileOperationTranslator : public OperationTranslator { |
| 345 ~OpenFileOperationTranslator() override {} |
| 346 virtual scoped_ptr<PP_FilesystemRequest> ToFilesystemRequest()override; |
| 347 static scoped_ptr<OpenFileOperationTranslator> PopulateFromRequest( |
| 348 const base::ListValue& request); |
| 349 // The identifier of the file system related to this operation. |
| 350 std::string file_system_id; |
| 351 // A request ID which will be used by consecutive read/write and close |
| 352 // requests. |
| 353 int request_id; |
| 354 // The path of the file to be opened. |
| 355 std::string file_path; |
| 356 // Whether the file will be used for reading or writing. |
| 357 PP_OpenFileMode_Dev mode; |
| 358 private: |
| 359 OpenFileOperationTranslator() {} |
| 360 DISALLOW_COPY_AND_ASSIGN(OpenFileOperationTranslator); |
| 361 }; |
| 362 |
| 363 struct ReadDirectoryOperationTranslator : public OperationTranslator { |
| 364 ~ReadDirectoryOperationTranslator() override {} |
| 365 virtual scoped_ptr<PP_FilesystemRequest> ToFilesystemRequest() override; |
| 366 static scoped_ptr<ReadDirectoryOperationTranslator> PopulateFromRequest( |
| 367 const base::ListValue& value); |
| 368 // The identifier of the file system related to this operation. |
| 369 std::string file_system_id; |
| 370 // The unique identifier of this request. |
| 371 int request_id; |
| 372 // The path of the directory which contents are requested. |
| 373 std::string directory_path; |
| 374 private: |
| 375 ReadDirectoryOperationTranslator() {} |
| 376 DISALLOW_COPY_AND_ASSIGN(ReadDirectoryOperationTranslator); |
| 377 }; |
| 378 |
| 379 struct ReadFileOperationTranslator : public OperationTranslator { |
| 380 ~ReadFileOperationTranslator() override {} |
| 381 virtual scoped_ptr<PP_FilesystemRequest> ToFilesystemRequest() override; |
| 382 static scoped_ptr<ReadFileOperationTranslator> PopulateFromRequest( |
| 383 const base::ListValue& value); |
| 384 // The identifier of the file system related to this operation. |
| 385 std::string file_system_id; |
| 386 // The unique identifier of this request. |
| 387 int request_id; |
| 388 // A request ID used to open the file. |
| 389 int open_request_id; |
| 390 // Position in the file (in bytes) to start reading from. |
| 391 double offset; |
| 392 // Number of bytes to be returned. |
| 393 double length; |
| 394 private: |
| 395 ReadFileOperationTranslator() {} |
| 396 DISALLOW_COPY_AND_ASSIGN(ReadFileOperationTranslator); |
| 397 }; |
| 398 |
| 399 struct TruncateOperationTranslator : public OperationTranslator { |
| 400 ~TruncateOperationTranslator() override {} |
| 401 virtual scoped_ptr<PP_FilesystemRequest> ToFilesystemRequest() override; |
| 402 static scoped_ptr<TruncateOperationTranslator> PopulateFromRequest( |
| 403 const base::ListValue& request); |
| 404 // The identifier of the file system related to this operation. |
| 405 std::string file_system_id; |
| 406 // The unique identifier of this request. |
| 407 int request_id; |
| 408 // The path of the file to be truncated. |
| 409 std::string file_path; |
| 410 // Number of bytes to be retained after the operation completes. |
| 411 double length; |
| 412 private: |
| 413 TruncateOperationTranslator() {} |
| 414 DISALLOW_COPY_AND_ASSIGN(TruncateOperationTranslator); |
| 415 }; |
| 416 |
| 417 struct UnmountOperationTranslator : public OperationTranslator { |
| 418 ~UnmountOperationTranslator() override {} |
| 419 virtual scoped_ptr<PP_FilesystemRequest> ToFilesystemRequest() override; |
| 420 static scoped_ptr<UnmountOperationTranslator> PopulateFromRequest( |
| 421 const base::ListValue& request); |
| 422 // The identifier of the file system to be unmounted. |
| 423 std::string file_system_id; |
| 424 int request_id; |
| 425 private: |
| 426 UnmountOperationTranslator() {} |
| 427 DISALLOW_COPY_AND_ASSIGN(UnmountOperationTranslator); |
| 428 }; |
| 429 |
| 430 struct WriteFileOperationTranslator : public OperationTranslator { |
| 431 ~WriteFileOperationTranslator() override {} |
| 432 virtual scoped_ptr<PP_FilesystemRequest> ToFilesystemRequest() override; |
| 433 static scoped_ptr<WriteFileOperationTranslator> PopulateFromRequest( |
| 434 const base::ListValue& request, |
| 435 char* memory_address); |
| 436 // The identifier of the file system related to this operation. |
| 437 std::string file_system_id; |
| 438 // The unique identifier of this request. |
| 439 int request_id; |
| 440 // A request ID used to open the file. |
| 441 int open_request_id; |
| 442 // Position in the file (in bytes) to start writing the bytes from. |
| 443 double offset; |
| 444 // The size of the buffer |
| 445 double size; |
| 446 // Buffer for the file chunk |
| 447 std::vector<char> data; |
| 448 private: |
| 449 WriteFileOperationTranslator() {} |
| 450 DISALLOW_COPY_AND_ASSIGN(WriteFileOperationTranslator); |
| 451 }; |
| 452 |
| 453 struct PluginToBrowserTranslator{ |
| 454 ~PluginToBrowserTranslator() {} |
| 455 static scoped_ptr<base::ListValue> GenerateMountRequest( |
| 456 std::string filesystem_id, |
| 457 std::string display_name, |
| 458 bool writable, |
| 459 int32_t opened_files_limit); |
| 460 |
| 461 static scoped_ptr<base::ListValue> GenerateSuccessResponse( |
| 462 PP_OperationType_Dev operation_type, |
| 463 const std::string& filesystem_id, |
| 464 int32_t request_id, |
| 465 double time_span); |
| 466 |
| 467 static scoped_ptr<base::ListValue> GenerateFailureResponse( |
| 468 PP_OperationType_Dev operation_type, |
| 469 PP_ProviderError_Dev error, |
| 470 const std::string& filesystem_id, |
| 471 int32_t request_id, |
| 472 double time_span); |
| 473 static scoped_ptr<base::ListValue> GenerateMetadataResponse( |
| 474 const PP_EntryMetadata_Dev *metadata, |
| 475 int32_t request_id, |
| 476 const std::string& filesystem_id, |
| 477 double time_span); |
| 478 static scoped_ptr<base::ListValue> GenerateReadDirectoryResponse( |
| 479 uint32_t array_size, |
| 480 const PP_EntryMetadata_Dev entries[], |
| 481 PP_Bool has_more, int32_t request_id, const std::string& filesystem_id, |
| 482 double time_span); |
| 483 static scoped_ptr<base::ListValue> GenerateReadFileSuccessResponse( |
| 484 uint32_t data_size, bool has_more,int32_t request_id, |
| 485 double time_span, const std::string& filesystem_id); |
| 486 private: |
| 487 PluginToBrowserTranslator(){} |
| 488 DISALLOW_COPY_AND_ASSIGN(PluginToBrowserTranslator); |
| 489 |
| 490 }; |
| 491 |
| 492 }// namespace |
| 493 }// namespace proxy |
| 494 }// namespace ppapi |
| 495 |
| 496 #endif // FILESYSTEM_PROVIDER_RESOURCE_H |
| 497 |
OLD | NEW |