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.
5 #ifndef CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_REQUEST_MANAGER_H_
6 #define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_REQUEST_MANAGER_H_
12 #include "base/callback.h"
13 #include "base/files/file.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/observer_list.h"
17 #include "base/time/time.h"
18 #include "base/timer/timer.h"
19 #include "chrome/browser/chromeos/file_system_provider/notification_manager_interface.h"
20 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_info.h"
21 #include "chrome/browser/chromeos/file_system_provider/request_value.h"
24 namespace file_system_provider
{
26 // Request type, passed to RequestManager::CreateRequest. For logging purposes.
47 // Converts a request type to human-readable format.
48 std::string
RequestTypeToString(RequestType type
);
50 // Manages requests between the service, async utils and the providing
52 class RequestManager
{
54 // Handles requests. Each request implementation must implement
56 class HandlerInterface
{
58 virtual ~HandlerInterface() {}
60 // Called when the request is created. Executes the request implementation.
61 // Returns false in case of a execution failure.
62 virtual bool Execute(int request_id
) = 0;
64 // Success callback invoked by the providing extension in response to
65 // Execute(). It may be called more than once, until |has_more| is set to
67 virtual void OnSuccess(int request_id
,
68 scoped_ptr
<RequestValue
> result
,
71 // Error callback invoked by the providing extension in response to
72 // Execute(). It can be called at most once. It can be also called if the
73 // request is aborted due to a timeout.
74 virtual void OnError(int request_id
,
75 scoped_ptr
<RequestValue
> result
,
76 base::File::Error error
) = 0;
79 // Observes activities in the request manager.
82 virtual ~Observer() {}
84 // Called when the request is created.
85 virtual void OnRequestCreated(int request_id
, RequestType type
) = 0;
87 // Called when the request is destroyed.
88 virtual void OnRequestDestroyed(int request_id
) = 0;
90 // Called when the request is executed.
91 virtual void OnRequestExecuted(int request_id
) = 0;
93 // Called when the request is fulfilled with a success.
94 virtual void OnRequestFulfilled(int request_id
,
95 const RequestValue
& result
,
98 // Called when the request is rejected with an error.
99 virtual void OnRequestRejected(int request_id
,
100 const RequestValue
& result
,
101 base::File::Error error
) = 0;
103 // Called when the request is timeouted.
104 virtual void OnRequestTimeouted(int request_id
) = 0;
107 explicit RequestManager(NotificationManagerInterface
* notification_manager
);
108 virtual ~RequestManager();
110 // Creates a request and returns its request id (greater than 0). Returns 0 in
111 // case of an error (eg. too many requests). The |type| argument indicates
112 // what kind of request it is.
113 int CreateRequest(RequestType type
, scoped_ptr
<HandlerInterface
> handler
);
115 // Handles successful response for the |request_id|. If |has_more| is false,
116 // then the request is disposed, after handling the |response|. On success,
117 // returns base::File::FILE_OK. Otherwise returns an error code. |response|
119 base::File::Error
FulfillRequest(int request_id
,
120 scoped_ptr
<RequestValue
> response
,
123 // Handles error response for the |request_id|. If handling the error
124 // succeeds, theen returns base::File::FILE_OK. Otherwise returns an error
125 // code. Always disposes the request. |response| must not be NULL.
126 base::File::Error
RejectRequest(int request_id
,
127 scoped_ptr
<RequestValue
> response
,
128 base::File::Error error
);
130 // Sets a custom timeout for tests. The new timeout value will be applied to
132 void SetTimeoutForTesting(const base::TimeDelta
& timeout
);
134 // Gets list of active request ids.
135 std::vector
<int> GetActiveRequestIds() const;
137 // Adds and removes observers.
138 void AddObserver(Observer
* observer
);
139 void RemoveObserver(Observer
* observer
);
146 // Timer for discarding the request during a timeout.
147 base::OneShotTimer
<RequestManager
> timeout_timer
;
149 // Handler tied to this request.
150 scoped_ptr
<HandlerInterface
> handler
;
153 DISALLOW_COPY_AND_ASSIGN(Request
);
156 typedef std::map
<int, Request
*> RequestMap
;
158 // Destroys the request with the passed |request_id|.
159 void DestroyRequest(int request_id
);
161 // Called when a request with |request_id| timeouts.
162 void OnRequestTimeout(int request_id
);
164 // Called when an user either aborts the unresponsive request or lets it
166 void OnUnresponsiveNotificationResult(
168 NotificationManagerInterface::NotificationResult result
);
170 // Resets the timeout timer for the specified request.
171 void ResetTimer(int request_id
);
173 RequestMap requests_
;
174 NotificationManagerInterface
* notification_manager_
; // Not owned.
176 base::TimeDelta timeout_
;
177 ObserverList
<Observer
> observers_
;
178 base::WeakPtrFactory
<RequestManager
> weak_ptr_factory_
;
180 DISALLOW_COPY_AND_ASSIGN(RequestManager
);
183 } // namespace file_system_provider
184 } // namespace chromeos
186 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_REQUEST_MANAGER_H_