OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/chromeos/file_system_provider/request_manager.h" | 5 #include "chrome/browser/chromeos/file_system_provider/request_manager.h" |
6 | 6 |
7 #include "base/files/file.h" | 7 #include "base/files/file.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "base/values.h" | |
10 | 9 |
11 namespace chromeos { | 10 namespace chromeos { |
12 namespace file_system_provider { | 11 namespace file_system_provider { |
13 | 12 |
14 namespace { | 13 namespace { |
15 | 14 |
16 // Timeout in seconds, before a request is considered as stale and hence | 15 // Timeout in seconds, before a request is considered as stale and hence |
17 // aborted. | 16 // aborted. |
18 const int kDefaultTimeout = 10; | 17 const int kDefaultTimeout = 10; |
19 | 18 |
(...skipping 10 matching lines...) Expand all Loading... |
30 while (it != requests_.end()) { | 29 while (it != requests_.end()) { |
31 const int request_id = it->first; | 30 const int request_id = it->first; |
32 ++it; | 31 ++it; |
33 RejectRequest(request_id, base::File::FILE_ERROR_ABORT); | 32 RejectRequest(request_id, base::File::FILE_ERROR_ABORT); |
34 } | 33 } |
35 | 34 |
36 DCHECK_EQ(0u, requests_.size()); | 35 DCHECK_EQ(0u, requests_.size()); |
37 STLDeleteValues(&requests_); | 36 STLDeleteValues(&requests_); |
38 } | 37 } |
39 | 38 |
40 int RequestManager::CreateRequest(const SuccessCallback& success_callback, | 39 int RequestManager::CreateRequest(scoped_ptr<HandlerInterface> handler) { |
41 const ErrorCallback& error_callback) { | |
42 // The request id is unique per request manager, so per service, thereof | 40 // The request id is unique per request manager, so per service, thereof |
43 // per profile. | 41 // per profile. |
44 int request_id = next_id_++; | 42 int request_id = next_id_++; |
45 | 43 |
46 // If cycled the int, then signal an error. | 44 // If cycled the int, then signal an error. |
47 if (requests_.find(request_id) != requests_.end()) | 45 if (requests_.find(request_id) != requests_.end()) |
48 return 0; | 46 return 0; |
49 | 47 |
50 Request* request = new Request; | 48 Request* request = new Request; |
51 request->success_callback = success_callback; | 49 request->handler = handler.Pass(); |
52 request->error_callback = error_callback; | |
53 request->timeout_timer.Start(FROM_HERE, | 50 request->timeout_timer.Start(FROM_HERE, |
54 timeout_, | 51 timeout_, |
55 base::Bind(&RequestManager::OnRequestTimeout, | 52 base::Bind(&RequestManager::OnRequestTimeout, |
56 weak_ptr_factory_.GetWeakPtr(), | 53 weak_ptr_factory_.GetWeakPtr(), |
57 request_id)); | 54 request_id)); |
58 requests_[request_id] = request; | 55 requests_[request_id] = request; |
59 | 56 |
| 57 // Execute the request implementation. In case of an execution failure, |
| 58 // unregister and return 0. This may often happen, eg. if the providing |
| 59 // extension is not listening for the request event being sent. |
| 60 // In such case, there is no reason we should abort as soon as possible. |
| 61 if (!request->handler->Execute(request_id)) { |
| 62 delete request; |
| 63 requests_.erase(request_id); |
| 64 return 0; |
| 65 } |
| 66 |
60 return request_id; | 67 return request_id; |
61 } | 68 } |
62 | 69 |
63 bool RequestManager::FulfillRequest(int request_id, | 70 bool RequestManager::FulfillRequest(int request_id, |
64 scoped_ptr<base::DictionaryValue> response, | 71 scoped_ptr<RequestValue> response, |
65 bool has_next) { | 72 bool has_next) { |
66 RequestMap::iterator request_it = requests_.find(request_id); | 73 RequestMap::iterator request_it = requests_.find(request_id); |
67 | 74 |
68 if (request_it == requests_.end()) | 75 if (request_it == requests_.end()) |
69 return false; | 76 return false; |
70 | 77 |
71 if (!request_it->second->success_callback.is_null()) | 78 request_it->second->handler->OnSuccess(request_id, response.Pass(), has_next); |
72 request_it->second->success_callback.Run(response.Pass(), has_next); | |
73 if (!has_next) { | 79 if (!has_next) { |
74 delete request_it->second; | 80 delete request_it->second; |
75 requests_.erase(request_it); | 81 requests_.erase(request_it); |
76 } else { | 82 } else { |
77 request_it->second->timeout_timer.Reset(); | 83 request_it->second->timeout_timer.Reset(); |
78 } | 84 } |
79 | 85 |
80 return true; | 86 return true; |
81 } | 87 } |
82 | 88 |
83 bool RequestManager::RejectRequest(int request_id, base::File::Error error) { | 89 bool RequestManager::RejectRequest(int request_id, base::File::Error error) { |
84 RequestMap::iterator request_it = requests_.find(request_id); | 90 RequestMap::iterator request_it = requests_.find(request_id); |
85 | 91 |
86 if (request_it == requests_.end()) | 92 if (request_it == requests_.end()) |
87 return false; | 93 return false; |
88 | 94 |
89 if (!request_it->second->error_callback.is_null()) | 95 request_it->second->handler->OnError(request_id, error); |
90 request_it->second->error_callback.Run(error); | |
91 delete request_it->second; | 96 delete request_it->second; |
92 requests_.erase(request_it); | 97 requests_.erase(request_it); |
93 | 98 |
94 return true; | 99 return true; |
95 } | 100 } |
96 | 101 |
97 void RequestManager::SetTimeoutForTests(const base::TimeDelta& timeout) { | 102 void RequestManager::SetTimeoutForTests(const base::TimeDelta& timeout) { |
98 timeout_ = timeout; | 103 timeout_ = timeout; |
99 } | 104 } |
100 | 105 |
101 void RequestManager::OnRequestTimeout(int request_id) { | 106 void RequestManager::OnRequestTimeout(int request_id) { |
102 RejectRequest(request_id, base::File::FILE_ERROR_ABORT); | 107 RejectRequest(request_id, base::File::FILE_ERROR_ABORT); |
103 } | 108 } |
104 | 109 |
105 RequestManager::Request::Request() {} | 110 RequestManager::Request::Request() {} |
106 | 111 |
107 RequestManager::Request::~Request() {} | 112 RequestManager::Request::~Request() {} |
108 | 113 |
109 } // namespace file_system_provider | 114 } // namespace file_system_provider |
110 } // namespace chromeos | 115 } // namespace chromeos |
OLD | NEW |