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