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/request_value.h"
25 namespace file_system_provider
{
27 // Request type, passed to RequestManager::CreateRequest. For logging purposes.
51 // Manages requests between the service, async utils and the providing
53 class RequestManager
{
55 // Handles requests. Each request implementation must implement
57 class HandlerInterface
{
59 virtual ~HandlerInterface() {}
61 // Called when the request is created. Executes the request implementation.
62 // Returns false in case of a execution failure.
63 virtual bool Execute(int request_id
) = 0;
65 // Success callback invoked by the providing extension in response to
66 // Execute(). It may be called more than once, until |has_more| is set to
68 virtual void OnSuccess(int request_id
,
69 scoped_ptr
<RequestValue
> result
,
72 // Error callback invoked by the providing extension in response to
73 // Execute(). It can be called at most once. It can be also called if the
74 // request is aborted due to a timeout.
75 virtual void OnError(int request_id
,
76 scoped_ptr
<RequestValue
> result
,
77 base::File::Error error
) = 0;
80 // Observes activities in the request manager.
83 virtual ~Observer() {}
85 // Called when the request is created.
86 virtual void OnRequestCreated(int request_id
, RequestType type
) = 0;
88 // Called when the request is destroyed.
89 virtual void OnRequestDestroyed(int request_id
) = 0;
91 // Called when the request is executed.
92 virtual void OnRequestExecuted(int request_id
) = 0;
94 // Called when the request is fulfilled with a success.
95 virtual void OnRequestFulfilled(int request_id
,
96 const RequestValue
& result
,
99 // Called when the request is rejected with an error.
100 virtual void OnRequestRejected(int request_id
,
101 const RequestValue
& result
,
102 base::File::Error error
) = 0;
104 // Called when the request is timeouted.
105 virtual void OnRequestTimeouted(int request_id
) = 0;
108 // Creates a request manager for |profile| and |extension_id|. Note, that
109 // there may be multiple instances of request managers per extension.
110 RequestManager(Profile
* profile
,
111 const std::string
& extension_id
,
112 NotificationManagerInterface
* notification_manager
);
113 virtual ~RequestManager();
115 // Creates a request and returns its request id (greater than 0). Returns 0 in
116 // case of an error (eg. too many requests). The |type| argument indicates
117 // what kind of request it is.
118 int CreateRequest(RequestType type
, scoped_ptr
<HandlerInterface
> handler
);
120 // Handles successful response for the |request_id|. If |has_more| is false,
121 // then the request is disposed, after handling the |response|. On success,
122 // returns base::File::FILE_OK. Otherwise returns an error code. |response|
124 base::File::Error
FulfillRequest(int request_id
,
125 scoped_ptr
<RequestValue
> response
,
128 // Handles error response for the |request_id|. If handling the error
129 // succeeds, theen returns base::File::FILE_OK. Otherwise returns an error
130 // code. Always disposes the request. |response| must not be NULL.
131 base::File::Error
RejectRequest(int request_id
,
132 scoped_ptr
<RequestValue
> response
,
133 base::File::Error error
);
135 // Sets a custom timeout for tests. The new timeout value will be applied to
137 void SetTimeoutForTesting(const base::TimeDelta
& timeout
);
139 // Gets list of active request ids.
140 std::vector
<int> GetActiveRequestIds() const;
142 // Adds and removes observers.
143 void AddObserver(Observer
* observer
);
144 void RemoveObserver(Observer
* observer
);
151 // Timer for discarding the request during a timeout.
152 base::OneShotTimer
<RequestManager
> timeout_timer
;
154 // Handler tied to this request.
155 scoped_ptr
<HandlerInterface
> handler
;
158 DISALLOW_COPY_AND_ASSIGN(Request
);
161 typedef std::map
<int, Request
*> RequestMap
;
163 // Destroys the request with the passed |request_id|.
164 void DestroyRequest(int request_id
);
166 // Called when a request with |request_id| timeouts.
167 void OnRequestTimeout(int request_id
);
169 // Called when an user either aborts the unresponsive request or lets it
171 void OnUnresponsiveNotificationResult(
173 NotificationManagerInterface::NotificationResult result
);
175 // Resets the timeout timer for the specified request.
176 void ResetTimer(int request_id
);
178 // Checks whether there is an ongoing interaction between the providing
179 // extension and user.
180 bool IsInteractingWithUser() const;
182 Profile
* profile_
; // Not owned.
183 std::string extension_id_
;
184 RequestMap requests_
;
185 NotificationManagerInterface
* notification_manager_
; // Not owned.
187 base::TimeDelta timeout_
;
188 base::ObserverList
<Observer
> observers_
;
189 base::WeakPtrFactory
<RequestManager
> weak_ptr_factory_
;
191 DISALLOW_COPY_AND_ASSIGN(RequestManager
);
194 } // namespace file_system_provider
195 } // namespace chromeos
197 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_REQUEST_MANAGER_H_