Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / chromeos / file_system_provider / request_manager.h
blobccc87d83be672d94c45ada069e71e04861f16acb
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/provided_file_system_info.h"
21 #include "chrome/browser/chromeos/file_system_provider/request_value.h"
23 namespace chromeos {
24 namespace file_system_provider {
26 // Request type, passed to RequestManager::CreateRequest. For logging purposes.
27 enum RequestType {
28 REQUEST_UNMOUNT,
29 GET_METADATA,
30 READ_DIRECTORY,
31 OPEN_FILE,
32 CLOSE_FILE,
33 READ_FILE,
34 CREATE_DIRECTORY,
35 DELETE_ENTRY,
36 CREATE_FILE,
37 COPY_ENTRY,
38 MOVE_ENTRY,
39 TRUNCATE,
40 WRITE_FILE,
41 ABORT,
42 ADD_WATCHER,
43 REMOVE_WATCHER,
44 TESTING
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
51 // extensions.
52 class RequestManager {
53 public:
54 // Handles requests. Each request implementation must implement
55 // this interface.
56 class HandlerInterface {
57 public:
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
66 // false.
67 virtual void OnSuccess(int request_id,
68 scoped_ptr<RequestValue> result,
69 bool has_more) = 0;
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.
80 class Observer {
81 public:
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,
96 bool has_more) = 0;
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|
118 // must not be NULL.
119 base::File::Error FulfillRequest(int request_id,
120 scoped_ptr<RequestValue> response,
121 bool has_more);
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
131 // new requests
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);
141 private:
142 struct Request {
143 Request();
144 ~Request();
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;
152 private:
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
165 // continue.
166 void OnUnresponsiveNotificationResult(
167 int request_id,
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.
175 int next_id_;
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_