Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / file_system_provider / request_manager.h
blob83cd00207a79de29338ffa4e05ac500c6e000471
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_
8 #include <map>
9 #include <string>
10 #include <vector>
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"
22 class Profile;
24 namespace chromeos {
25 namespace file_system_provider {
27 // Request type, passed to RequestManager::CreateRequest. For logging purposes.
28 enum RequestType {
29 REQUEST_UNMOUNT,
30 GET_METADATA,
31 GET_ACTIONS,
32 EXECUTE_ACTION,
33 READ_DIRECTORY,
34 OPEN_FILE,
35 CLOSE_FILE,
36 READ_FILE,
37 CREATE_DIRECTORY,
38 DELETE_ENTRY,
39 CREATE_FILE,
40 COPY_ENTRY,
41 MOVE_ENTRY,
42 TRUNCATE,
43 WRITE_FILE,
44 ABORT,
45 ADD_WATCHER,
46 REMOVE_WATCHER,
47 CONFIGURE,
48 TESTING
51 // Manages requests between the service, async utils and the providing
52 // extensions.
53 class RequestManager {
54 public:
55 // Handles requests. Each request implementation must implement
56 // this interface.
57 class HandlerInterface {
58 public:
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
67 // false.
68 virtual void OnSuccess(int request_id,
69 scoped_ptr<RequestValue> result,
70 bool has_more) = 0;
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.
81 class Observer {
82 public:
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,
97 bool has_more) = 0;
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|
123 // must not be NULL.
124 base::File::Error FulfillRequest(int request_id,
125 scoped_ptr<RequestValue> response,
126 bool has_more);
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
136 // new requests
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);
146 private:
147 struct Request {
148 Request();
149 ~Request();
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;
157 private:
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
170 // continue.
171 void OnUnresponsiveNotificationResult(
172 int request_id,
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.
186 int next_id_;
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_