OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 |
| 5 #include "chrome/browser/chromeos/file_system_provider/request_manager.h" |
| 6 |
| 7 #include "base/values.h" |
| 8 |
| 9 namespace chromeos { |
| 10 namespace file_system_provider { |
| 11 |
| 12 RequestManager::RequestManager() : next_id_(1) {} |
| 13 |
| 14 RequestManager::~RequestManager() {} |
| 15 |
| 16 int RequestManager::CreateRequest(const ProvidedFileSystem& file_system, |
| 17 const SuccessCallback& success_callback, |
| 18 const ErrorCallback& error_callback) { |
| 19 // The request id is unique per request manager, so per service, thereof |
| 20 // per profile. |
| 21 int request_id = next_id_++; |
| 22 |
| 23 // If cycled the int, then signal an error. |
| 24 if (requests_.find(request_id) != requests_.end()) |
| 25 return 0; |
| 26 |
| 27 Request request; |
| 28 request.file_system = file_system; |
| 29 request.success_callback = success_callback; |
| 30 request.error_callback = error_callback; |
| 31 requests_[request_id] = request; |
| 32 |
| 33 return request_id; |
| 34 } |
| 35 |
| 36 bool RequestManager::FulfillRequest(const ProvidedFileSystem& file_system, |
| 37 int request_id, |
| 38 scoped_ptr<base::DictionaryValue> response, |
| 39 bool has_next) { |
| 40 RequestMap::iterator request_it = requests_.find(request_id); |
| 41 |
| 42 if (request_it == requests_.end()) |
| 43 return false; |
| 44 |
| 45 // Check if the request belongs to the same provided file system. |
| 46 if (request_it->second.file_system.file_system_id() != |
| 47 file_system.file_system_id()) { |
| 48 return false; |
| 49 } |
| 50 |
| 51 if (!request_it->second.success_callback.is_null()) |
| 52 request_it->second.success_callback.Run(response.Pass(), has_next); |
| 53 if (!has_next) |
| 54 requests_.erase(request_it); |
| 55 |
| 56 return true; |
| 57 } |
| 58 |
| 59 bool RequestManager::RejectRequest(const ProvidedFileSystem& file_system, |
| 60 int request_id, |
| 61 base::File::Error error) { |
| 62 RequestMap::iterator request_it = requests_.find(request_id); |
| 63 |
| 64 if (request_it == requests_.end()) |
| 65 return false; |
| 66 |
| 67 // Check if the request belongs to the same provided file system. |
| 68 if (request_it->second.file_system.file_system_id() != |
| 69 file_system.file_system_id()) { |
| 70 return false; |
| 71 } |
| 72 |
| 73 if (!request_it->second.error_callback.is_null()) |
| 74 request_it->second.error_callback.Run(error); |
| 75 requests_.erase(request_it); |
| 76 |
| 77 return true; |
| 78 } |
| 79 |
| 80 void RequestManager::OnProvidedFileSystemMount( |
| 81 const ProvidedFileSystem& file_system, |
| 82 base::File::Error error) {} |
| 83 |
| 84 void RequestManager::OnProvidedFileSystemUnmount( |
| 85 const ProvidedFileSystem& file_system, |
| 86 base::File::Error error) { |
| 87 // Do not continue on error, since the volume may be still mounted. |
| 88 if (error != base::File::FILE_OK) |
| 89 return; |
| 90 |
| 91 // Remove all requests for this provided file system. |
| 92 RequestMap::iterator it = requests_.begin(); |
| 93 while (it != requests_.begin()) { |
| 94 if (it->second.file_system.file_system_id() == |
| 95 file_system.file_system_id()) { |
| 96 RejectRequest( |
| 97 it->second.file_system, it->first, base::File::FILE_ERROR_ABORT); |
| 98 requests_.erase(it++); |
| 99 } else { |
| 100 it++; |
| 101 } |
| 102 } |
| 103 } |
| 104 |
| 105 } // namespace file_system_provider |
| 106 } // namespace chromeos |
OLD | NEW |