Index: chrome/browser/chromeos/file_system_provider/request_manager.cc |
diff --git a/chrome/browser/chromeos/file_system_provider/request_manager.cc b/chrome/browser/chromeos/file_system_provider/request_manager.cc |
index d8aa9bb14d4a07304459f5a32b913aeee3bc4fac..ae2c4a485bed5cb1f207a9e7ca096f33f447b301 100644 |
--- a/chrome/browser/chromeos/file_system_provider/request_manager.cc |
+++ b/chrome/browser/chromeos/file_system_provider/request_manager.cc |
@@ -4,12 +4,25 @@ |
#include "chrome/browser/chromeos/file_system_provider/request_manager.h" |
+#include "base/files/file.h" |
+#include "base/stl_util.h" |
#include "base/values.h" |
namespace chromeos { |
namespace file_system_provider { |
-RequestManager::RequestManager() : next_id_(1) {} |
+namespace { |
+ |
+// Timeout in seconds, before a request is considered as stale and hence |
+// aborted. |
+const int kDefaultTimeout = 10; |
+ |
+} // namespace |
+ |
+RequestManager::RequestManager() |
+ : next_id_(1), |
+ timeout_(base::TimeDelta::FromSeconds(kDefaultTimeout)), |
+ weak_ptr_factory_(this) {} |
RequestManager::~RequestManager() { |
// Abort all of the active requests. |
@@ -19,6 +32,9 @@ RequestManager::~RequestManager() { |
++it; |
RejectRequest(request_id, base::File::FILE_ERROR_ABORT); |
} |
+ |
+ DCHECK_EQ(0u, requests_.size()); |
+ STLDeleteValues(&requests_); |
} |
int RequestManager::CreateRequest(const SuccessCallback& success_callback, |
@@ -31,9 +47,14 @@ int RequestManager::CreateRequest(const SuccessCallback& success_callback, |
if (requests_.find(request_id) != requests_.end()) |
return 0; |
- Request request; |
- request.success_callback = success_callback; |
- request.error_callback = error_callback; |
+ Request* request = new Request; |
+ request->success_callback = success_callback; |
+ request->error_callback = error_callback; |
+ request->timeout_timer.Start(FROM_HERE, |
+ timeout_, |
+ base::Bind(&RequestManager::OnRequestTimeout, |
+ weak_ptr_factory_.GetWeakPtr(), |
+ request_id)); |
requests_[request_id] = request; |
return request_id; |
@@ -47,10 +68,14 @@ bool RequestManager::FulfillRequest(int request_id, |
if (request_it == requests_.end()) |
return false; |
- if (!request_it->second.success_callback.is_null()) |
- request_it->second.success_callback.Run(response.Pass(), has_next); |
- if (!has_next) |
+ if (!request_it->second->success_callback.is_null()) |
+ request_it->second->success_callback.Run(response.Pass(), has_next); |
+ if (!has_next) { |
+ delete request_it->second; |
requests_.erase(request_it); |
+ } else { |
+ request_it->second->timeout_timer.Reset(); |
+ } |
return true; |
} |
@@ -61,13 +86,22 @@ bool RequestManager::RejectRequest(int request_id, base::File::Error error) { |
if (request_it == requests_.end()) |
return false; |
- if (!request_it->second.error_callback.is_null()) |
- request_it->second.error_callback.Run(error); |
+ if (!request_it->second->error_callback.is_null()) |
+ request_it->second->error_callback.Run(error); |
+ delete request_it->second; |
requests_.erase(request_it); |
return true; |
} |
+void RequestManager::SetTimeoutForTests(const base::TimeDelta& timeout) { |
+ timeout_ = timeout; |
+} |
+ |
+void RequestManager::OnRequestTimeout(int request_id) { |
+ RejectRequest(request_id, base::File::FILE_ERROR_ABORT); |
+} |
+ |
RequestManager::Request::Request() {} |
RequestManager::Request::~Request() {} |