• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_REQUEST_MANAGER_H_
6 #define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_REQUEST_MANAGER_H_
7 
8 #include <map>
9 #include <string>
10 
11 #include "base/callback.h"
12 #include "base/files/file.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/observer_list.h"
16 #include "base/time/time.h"
17 #include "base/timer/timer.h"
18 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_info.h"
19 #include "chrome/browser/chromeos/file_system_provider/request_value.h"
20 
21 namespace chromeos {
22 namespace file_system_provider {
23 
24 // Request type, passed to RequestManager::CreateRequest. For logging purposes.
25 enum RequestType {
26   REQUEST_UNMOUNT,
27   GET_METADATA,
28   READ_DIRECTORY,
29   OPEN_FILE,
30   CLOSE_FILE,
31   READ_FILE,
32   TESTING
33 };
34 
35 // Converts a request type to human-readable format.
36 std::string RequestTypeToString(RequestType type);
37 
38 // Manages requests between the service, async utils and the providing
39 // extensions.
40 class RequestManager {
41  public:
42   // Handles requests. Each request implementation must implement
43   // this interface.
44   class HandlerInterface {
45    public:
~HandlerInterface()46     virtual ~HandlerInterface() {}
47 
48     // Called when the request is created. Executes the request implementation.
49     // Returns false in case of a execution failure.
50     virtual bool Execute(int request_id) = 0;
51 
52     // Success callback invoked by the providing extension in response to
53     // Execute(). It may be called more than once, until |has_more| is set to
54     // false.
55     virtual void OnSuccess(int request_id,
56                            scoped_ptr<RequestValue> result,
57                            bool has_more) = 0;
58 
59     // Error callback invoked by the providing extension in response to
60     // Execute(). It can be called at most once. It can be also called if the
61     // request is aborted due to a timeout.
62     virtual void OnError(int request_id, base::File::Error error) = 0;
63   };
64 
65   // Observes activities in the request manager.
66   class Observer {
67    public:
~Observer()68     virtual ~Observer() {}
69 
70     // Called when the request is created.
71     virtual void OnRequestCreated(int request_id, RequestType type) = 0;
72 
73     // Called when the request is destroyed.
74     virtual void OnRequestDestroyed(int request_id) = 0;
75 
76     // Called when the request is executed.
77     virtual void OnRequestExecuted(int request_id) = 0;
78 
79     // Called when the request is fulfilled with a success.
80     virtual void OnRequestFulfilled(int request_id, bool has_more) = 0;
81 
82     // Called when the request is rejected with an error.
83     virtual void OnRequestRejected(int request_id, base::File::Error error) = 0;
84 
85     // Called when the request is timeouted.
86     virtual void OnRequestTimeouted(int request_id) = 0;
87   };
88 
89   RequestManager();
90   virtual ~RequestManager();
91 
92   // Creates a request and returns its request id (greater than 0). Returns 0 in
93   // case of an error (eg. too many requests). The |type| argument indicates
94   // what kind of request it is.
95   int CreateRequest(RequestType type, scoped_ptr<HandlerInterface> handler);
96 
97   // Handles successful response for the |request_id|. If |has_more| is false,
98   // then the request is disposed, after handling the |response|. On error,
99   // returns false, and the request is disposed.
100   bool FulfillRequest(int request_id,
101                       scoped_ptr<RequestValue> response,
102                       bool has_more);
103 
104   // Handles error response for the |request_id|. If handling the error fails,
105   // returns false. Always disposes the request.
106   bool RejectRequest(int request_id, base::File::Error error);
107 
108   // Sets a custom timeout for tests. The new timeout value will be applied to
109   // new requests
110   void SetTimeoutForTesting(const base::TimeDelta& timeout);
111 
112   // Gets number of active requests for logging purposes.
113   // TODO(mtomasz): Introduce a logger class to gather more information
114   size_t GetActiveRequestsForLogging() const;
115 
116   // Adds and removes observers.
117   void AddObserver(Observer* observer);
118   void RemoveObserver(Observer* observer);
119 
120  private:
121   struct Request {
122     Request();
123     ~Request();
124 
125     // Timer for discarding the request during a timeout.
126     base::OneShotTimer<RequestManager> timeout_timer;
127 
128     // Handler tied to this request.
129     scoped_ptr<HandlerInterface> handler;
130 
131    private:
132     DISALLOW_COPY_AND_ASSIGN(Request);
133   };
134 
135   typedef std::map<int, Request*> RequestMap;
136 
137   // Destroys the request with the passed |request_id|.
138   void DestroyRequest(int request_id);
139 
140   // Called when a request with |request_id| timeouts.
141   void OnRequestTimeout(int request_id);
142 
143   RequestMap requests_;
144   int next_id_;
145   base::TimeDelta timeout_;
146   base::WeakPtrFactory<RequestManager> weak_ptr_factory_;
147   ObserverList<Observer> observers_;
148 
149   DISALLOW_COPY_AND_ASSIGN(RequestManager);
150 };
151 
152 }  // namespace file_system_provider
153 }  // namespace chromeos
154 
155 #endif  // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_REQUEST_MANAGER_H_
156