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" |
| 8 #include "base/stl_util.h" |
7 #include "base/values.h" | 9 #include "base/values.h" |
8 | 10 |
9 namespace chromeos { | 11 namespace chromeos { |
10 namespace file_system_provider { | 12 namespace file_system_provider { |
11 | 13 |
12 RequestManager::RequestManager() : next_id_(1) {} | 14 namespace { |
| 15 |
| 16 // Timeout in seconds, before a request is considered as stale and hence |
| 17 // aborted. |
| 18 const int kDefaultTimeout = 10; |
| 19 |
| 20 } // namespace |
| 21 |
| 22 RequestManager::RequestManager() |
| 23 : next_id_(1), |
| 24 timeout_(base::TimeDelta::FromSeconds(kDefaultTimeout)), |
| 25 weak_ptr_factory_(this) {} |
13 | 26 |
14 RequestManager::~RequestManager() { | 27 RequestManager::~RequestManager() { |
15 // Abort all of the active requests. | 28 // Abort all of the active requests. |
16 RequestMap::iterator it = requests_.begin(); | 29 RequestMap::iterator it = requests_.begin(); |
17 while (it != requests_.end()) { | 30 while (it != requests_.end()) { |
18 const int request_id = it->first; | 31 const int request_id = it->first; |
19 ++it; | 32 ++it; |
20 RejectRequest(request_id, base::File::FILE_ERROR_ABORT); | 33 RejectRequest(request_id, base::File::FILE_ERROR_ABORT); |
21 } | 34 } |
| 35 |
| 36 DCHECK_EQ(0u, requests_.size()); |
| 37 STLDeleteValues(&requests_); |
22 } | 38 } |
23 | 39 |
24 int RequestManager::CreateRequest(const SuccessCallback& success_callback, | 40 int RequestManager::CreateRequest(const SuccessCallback& success_callback, |
25 const ErrorCallback& error_callback) { | 41 const ErrorCallback& error_callback) { |
26 // The request id is unique per request manager, so per service, thereof | 42 // The request id is unique per request manager, so per service, thereof |
27 // per profile. | 43 // per profile. |
28 int request_id = next_id_++; | 44 int request_id = next_id_++; |
29 | 45 |
30 // If cycled the int, then signal an error. | 46 // If cycled the int, then signal an error. |
31 if (requests_.find(request_id) != requests_.end()) | 47 if (requests_.find(request_id) != requests_.end()) |
32 return 0; | 48 return 0; |
33 | 49 |
34 Request request; | 50 Request* request = new Request; |
35 request.success_callback = success_callback; | 51 request->success_callback = success_callback; |
36 request.error_callback = error_callback; | 52 request->error_callback = error_callback; |
| 53 request->timeout_timer.Start(FROM_HERE, |
| 54 timeout_, |
| 55 base::Bind(&RequestManager::OnRequestTimeout, |
| 56 weak_ptr_factory_.GetWeakPtr(), |
| 57 request_id)); |
37 requests_[request_id] = request; | 58 requests_[request_id] = request; |
38 | 59 |
39 return request_id; | 60 return request_id; |
40 } | 61 } |
41 | 62 |
42 bool RequestManager::FulfillRequest(int request_id, | 63 bool RequestManager::FulfillRequest(int request_id, |
43 scoped_ptr<base::DictionaryValue> response, | 64 scoped_ptr<base::DictionaryValue> response, |
44 bool has_next) { | 65 bool has_next) { |
45 RequestMap::iterator request_it = requests_.find(request_id); | 66 RequestMap::iterator request_it = requests_.find(request_id); |
46 | 67 |
47 if (request_it == requests_.end()) | 68 if (request_it == requests_.end()) |
48 return false; | 69 return false; |
49 | 70 |
50 if (!request_it->second.success_callback.is_null()) | 71 if (!request_it->second->success_callback.is_null()) |
51 request_it->second.success_callback.Run(response.Pass(), has_next); | 72 request_it->second->success_callback.Run(response.Pass(), has_next); |
52 if (!has_next) | 73 if (!has_next) { |
| 74 delete request_it->second; |
53 requests_.erase(request_it); | 75 requests_.erase(request_it); |
| 76 } else { |
| 77 request_it->second->timeout_timer.Reset(); |
| 78 } |
54 | 79 |
55 return true; | 80 return true; |
56 } | 81 } |
57 | 82 |
58 bool RequestManager::RejectRequest(int request_id, base::File::Error error) { | 83 bool RequestManager::RejectRequest(int request_id, base::File::Error error) { |
59 RequestMap::iterator request_it = requests_.find(request_id); | 84 RequestMap::iterator request_it = requests_.find(request_id); |
60 | 85 |
61 if (request_it == requests_.end()) | 86 if (request_it == requests_.end()) |
62 return false; | 87 return false; |
63 | 88 |
64 if (!request_it->second.error_callback.is_null()) | 89 if (!request_it->second->error_callback.is_null()) |
65 request_it->second.error_callback.Run(error); | 90 request_it->second->error_callback.Run(error); |
| 91 delete request_it->second; |
66 requests_.erase(request_it); | 92 requests_.erase(request_it); |
67 | 93 |
68 return true; | 94 return true; |
69 } | 95 } |
70 | 96 |
| 97 void RequestManager::SetTimeoutForTests(const base::TimeDelta& timeout) { |
| 98 timeout_ = timeout; |
| 99 } |
| 100 |
| 101 void RequestManager::OnRequestTimeout(int request_id) { |
| 102 RejectRequest(request_id, base::File::FILE_ERROR_ABORT); |
| 103 } |
| 104 |
71 RequestManager::Request::Request() {} | 105 RequestManager::Request::Request() {} |
72 | 106 |
73 RequestManager::Request::~Request() {} | 107 RequestManager::Request::~Request() {} |
74 | 108 |
75 } // namespace file_system_provider | 109 } // namespace file_system_provider |
76 } // namespace chromeos | 110 } // namespace chromeos |
OLD | NEW |