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/values.h" | 8 #include "base/values.h" |
8 | 9 |
9 namespace chromeos { | 10 namespace chromeos { |
10 namespace file_system_provider { | 11 namespace file_system_provider { |
11 | 12 |
12 RequestManager::RequestManager() : next_id_(1) {} | 13 namespace { |
14 | |
15 // Timeout in milliseconds, before a request is considered as stale and hence | |
16 // aborted. | |
17 const int kDefaultTimeout = 10000; // 10 seconds. | |
18 | |
19 } // namespace | |
20 | |
21 RequestManager::RequestManager() : next_id_(1), timeout_(kTimeout) {} | |
hashimoto
2014/04/18 05:28:09
Does this line compile?
mtomasz
2014/04/18 05:56:18
Oops. Done.
| |
13 | 22 |
14 RequestManager::~RequestManager() { | 23 RequestManager::~RequestManager() { |
15 // Abort all of the active requests. | 24 // Abort all of the active requests. |
16 RequestMap::iterator it = requests_.begin(); | 25 RequestMap::iterator it = requests_.begin(); |
17 while (it != requests_.end()) { | 26 while (it != requests_.end()) { |
18 const int request_id = it->first; | 27 const int request_id = it->first; |
19 ++it; | 28 ++it; |
20 RejectRequest(request_id, base::File::FILE_ERROR_ABORT); | 29 RejectRequest(request_id, base::File::FILE_ERROR_ABORT); |
21 } | 30 } |
31 | |
32 DCHECK_EQ(0u, requests_.size()); | |
22 } | 33 } |
23 | 34 |
24 int RequestManager::CreateRequest(const SuccessCallback& success_callback, | 35 int RequestManager::CreateRequest(const SuccessCallback& success_callback, |
25 const ErrorCallback& error_callback) { | 36 const ErrorCallback& error_callback) { |
26 // The request id is unique per request manager, so per service, thereof | 37 // The request id is unique per request manager, so per service, thereof |
27 // per profile. | 38 // per profile. |
28 int request_id = next_id_++; | 39 int request_id = next_id_++; |
29 | 40 |
30 // If cycled the int, then signal an error. | 41 // If cycled the int, then signal an error. |
31 if (requests_.find(request_id) != requests_.end()) | 42 if (requests_.find(request_id) != requests_.end()) |
32 return 0; | 43 return 0; |
33 | 44 |
34 Request request; | 45 Request* request = new Request; |
35 request.success_callback = success_callback; | 46 request->success_callback = success_callback; |
36 request.error_callback = error_callback; | 47 request->error_callback = error_callback; |
48 request->timeout_timer.Start( | |
49 FROM_HERE, | |
50 base::TimeDelta::FromMilliseconds(timeout_), | |
51 base::Bind( | |
52 &RequestManager::OnRequestTimeout, | |
53 base::Unretained(this), // The timer is outlived by RequestManager. | |
hashimoto
2014/04/18 05:28:09
Why don't you use WeakPtrFactory?
mtomasz
2014/04/18 05:56:18
That would be completely redundant. Timers are own
hashimoto
2014/04/18 06:08:00
I'd use WeakPtr because there is a period during t
mtomasz
2014/04/18 06:19:51
Makes sense to me. Let's go with weak ptrs for sim
| |
54 request_id)); | |
37 requests_[request_id] = request; | 55 requests_[request_id] = request; |
38 | 56 |
39 return request_id; | 57 return request_id; |
40 } | 58 } |
41 | 59 |
42 bool RequestManager::FulfillRequest(int request_id, | 60 bool RequestManager::FulfillRequest(int request_id, |
43 scoped_ptr<base::DictionaryValue> response, | 61 scoped_ptr<base::DictionaryValue> response, |
44 bool has_next) { | 62 bool has_next) { |
45 RequestMap::iterator request_it = requests_.find(request_id); | 63 RequestMap::iterator request_it = requests_.find(request_id); |
46 | 64 |
47 if (request_it == requests_.end()) | 65 if (request_it == requests_.end()) |
48 return false; | 66 return false; |
49 | 67 |
50 if (!request_it->second.success_callback.is_null()) | 68 if (!request_it->second->success_callback.is_null()) |
51 request_it->second.success_callback.Run(response.Pass(), has_next); | 69 request_it->second->success_callback.Run(response.Pass(), has_next); |
52 if (!has_next) | 70 if (!has_next) { |
71 delete request_it->second; | |
53 requests_.erase(request_it); | 72 requests_.erase(request_it); |
73 } else { | |
74 request_it->second->timeout_timer.Reset(); | |
75 } | |
54 | 76 |
55 return true; | 77 return true; |
56 } | 78 } |
57 | 79 |
58 bool RequestManager::RejectRequest(int request_id, base::File::Error error) { | 80 bool RequestManager::RejectRequest(int request_id, base::File::Error error) { |
59 RequestMap::iterator request_it = requests_.find(request_id); | 81 RequestMap::iterator request_it = requests_.find(request_id); |
60 | 82 |
61 if (request_it == requests_.end()) | 83 if (request_it == requests_.end()) |
62 return false; | 84 return false; |
63 | 85 |
64 if (!request_it->second.error_callback.is_null()) | 86 if (!request_it->second->error_callback.is_null()) |
65 request_it->second.error_callback.Run(error); | 87 request_it->second->error_callback.Run(error); |
88 delete request_it->second; | |
66 requests_.erase(request_it); | 89 requests_.erase(request_it); |
67 | 90 |
68 return true; | 91 return true; |
69 } | 92 } |
70 | 93 |
94 void RequestManager::SetTimeoutForTests(int timeout) { timeout_ = timeout; } | |
95 | |
96 void RequestManager::OnRequestTimeout(int request_id) { | |
97 RejectRequest(request_id, base::File::FILE_ERROR_ABORT); | |
98 } | |
99 | |
71 RequestManager::Request::Request() {} | 100 RequestManager::Request::Request() {} |
72 | 101 |
73 RequestManager::Request::~Request() {} | 102 RequestManager::Request::~Request() {} |
74 | 103 |
75 } // namespace file_system_provider | 104 } // namespace file_system_provider |
76 } // namespace chromeos | 105 } // namespace chromeos |
OLD | NEW |