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_
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/time/time.h"
16 #include "base/timer/timer.h"
17 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_info.h"
18 #include "chrome/browser/chromeos/file_system_provider/request_value.h"
21 namespace file_system_provider
{
23 // Manages requests between the service, async utils and the providing
25 class RequestManager
{
27 class HandlerInterface
{
29 virtual ~HandlerInterface() {}
31 // Called when the request is created. Executes the request implementation.
32 // Returns false in case of a execution failure.
33 virtual bool Execute(int request_id
) = 0;
35 // Success callback invoked by the providing extension in response to
36 // Execute(). It may be called more than once, until |has_next| is set to
38 virtual void OnSuccess(int request_id
,
39 scoped_ptr
<RequestValue
> result
,
42 // Error callback invoked by the providing extension in response to
43 // Execute(). It can be called at most once. It can be also called if the
44 // request is aborted due to a timeout.
45 virtual void OnError(int request_id
, base::File::Error error
) = 0;
49 virtual ~RequestManager();
51 // Creates a request and returns its request id (greater than 0). Returns 0 in
52 // case of an error (eg. too many requests).
53 int CreateRequest(scoped_ptr
<HandlerInterface
> handler
);
55 // Handles successful response for the |request_id|. If |has_next| is false,
56 // then the request is disposed, after handling the |response|. On error,
57 // returns false, and the request is disposed.
58 bool FulfillRequest(int request_id
,
59 scoped_ptr
<RequestValue
> response
,
62 // Handles error response for the |request_id|. If handling the error fails,
63 // returns false. Always disposes the request.
64 bool RejectRequest(int request_id
, base::File::Error error
);
66 // Sets a custom timeout for tests. The new timeout value will be applied to
68 void SetTimeoutForTests(const base::TimeDelta
& timeout
);
75 // Timer for discarding the request during a timeout.
76 base::OneShotTimer
<RequestManager
> timeout_timer
;
78 // Handler tied to this request.
79 scoped_ptr
<HandlerInterface
> handler
;
82 DISALLOW_COPY_AND_ASSIGN(Request
);
85 typedef std::map
<int, Request
*> RequestMap
;
87 // Called when a request with |request_id| timeouts.
88 void OnRequestTimeout(int request_id
);
92 base::TimeDelta timeout_
;
93 base::WeakPtrFactory
<RequestManager
> weak_ptr_factory_
;
95 DISALLOW_COPY_AND_ASSIGN(RequestManager
);
98 } // namespace file_system_provider
99 } // namespace chromeos
101 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_REQUEST_MANAGER_H_