| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_REQUEST_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_REQUEST_MANAGER_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_REQUEST_MANAGER_H_ | 6 #define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_REQUEST_MANAGER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/files/file.h" | 12 #include "base/files/file.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 16 #include "base/timer/timer.h" | 16 #include "base/timer/timer.h" |
| 17 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_info
.h" | 17 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_info
.h" |
| 18 | 18 #include "chrome/browser/chromeos/file_system_provider/request_value.h" |
| 19 namespace base { | |
| 20 class DictionaryValue; | |
| 21 } // namespace base | |
| 22 | 19 |
| 23 namespace chromeos { | 20 namespace chromeos { |
| 24 namespace file_system_provider { | 21 namespace file_system_provider { |
| 25 | 22 |
| 26 typedef base::Callback<void(scoped_ptr<base::DictionaryValue> result, | |
| 27 bool has_next)> SuccessCallback; | |
| 28 typedef base::Callback<void(base::File::Error)> ErrorCallback; | |
| 29 | |
| 30 // Manages requests between the service, async utils and the providing | 23 // Manages requests between the service, async utils and the providing |
| 31 // extensions. | 24 // extensions. |
| 32 class RequestManager { | 25 class RequestManager { |
| 33 public: | 26 public: |
| 27 class HandlerInterface { |
| 28 public: |
| 29 virtual ~HandlerInterface() {} |
| 30 |
| 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; |
| 34 |
| 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 |
| 37 // false. |
| 38 virtual void OnSuccess(int request_id, |
| 39 scoped_ptr<RequestValue> result, |
| 40 bool has_next) = 0; |
| 41 |
| 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; |
| 46 }; |
| 47 |
| 34 RequestManager(); | 48 RequestManager(); |
| 35 virtual ~RequestManager(); | 49 virtual ~RequestManager(); |
| 36 | 50 |
| 37 // Creates a request and returns its request id (greater than 0). Returns 0 in | 51 // Creates a request and returns its request id (greater than 0). Returns 0 in |
| 38 // case of an error (eg. too many requests). The passed callbacks can be NULL. | 52 // case of an error (eg. too many requests). |
| 39 int CreateRequest(const SuccessCallback& success_callback, | 53 int CreateRequest(scoped_ptr<HandlerInterface> handler); |
| 40 const ErrorCallback& error_callback); | |
| 41 | 54 |
| 42 // Handles successful response for the |request_id|. If |has_next| is false, | 55 // Handles successful response for the |request_id|. If |has_next| is false, |
| 43 // then the request is disposed, after handling the |response|. On error, | 56 // then the request is disposed, after handling the |response|. On error, |
| 44 // returns false, and the request is disposed. | 57 // returns false, and the request is disposed. |
| 45 bool FulfillRequest(int request_id, | 58 bool FulfillRequest(int request_id, |
| 46 scoped_ptr<base::DictionaryValue> response, | 59 scoped_ptr<RequestValue> response, |
| 47 bool has_next); | 60 bool has_next); |
| 48 | 61 |
| 49 // Handles error response for the |request_id|. If handling the error fails, | 62 // Handles error response for the |request_id|. If handling the error fails, |
| 50 // returns false. Always disposes the request. | 63 // returns false. Always disposes the request. |
| 51 bool RejectRequest(int request_id, base::File::Error error); | 64 bool RejectRequest(int request_id, base::File::Error error); |
| 52 | 65 |
| 53 // Sets a custom timeout for tests. The new timeout value will be applied to | 66 // Sets a custom timeout for tests. The new timeout value will be applied to |
| 54 // new requests | 67 // new requests |
| 55 void SetTimeoutForTests(const base::TimeDelta& timeout); | 68 void SetTimeoutForTests(const base::TimeDelta& timeout); |
| 56 | 69 |
| 57 private: | 70 private: |
| 58 struct Request { | 71 struct Request { |
| 59 Request(); | 72 Request(); |
| 60 ~Request(); | 73 ~Request(); |
| 61 | 74 |
| 62 // Timer for discarding the request during a timeout. | 75 // Timer for discarding the request during a timeout. |
| 63 base::OneShotTimer<RequestManager> timeout_timer; | 76 base::OneShotTimer<RequestManager> timeout_timer; |
| 64 | 77 |
| 65 // Callback to be called on success. | 78 // Handler tied to this request. |
| 66 SuccessCallback success_callback; | 79 scoped_ptr<HandlerInterface> handler; |
| 67 | |
| 68 // Callback to be called on error. | |
| 69 ErrorCallback error_callback; | |
| 70 | 80 |
| 71 private: | 81 private: |
| 72 DISALLOW_COPY_AND_ASSIGN(Request); | 82 DISALLOW_COPY_AND_ASSIGN(Request); |
| 73 }; | 83 }; |
| 74 | 84 |
| 75 typedef std::map<int, Request*> RequestMap; | 85 typedef std::map<int, Request*> RequestMap; |
| 76 | 86 |
| 77 // Called when a request with |request_id| timeouts. | 87 // Called when a request with |request_id| timeouts. |
| 78 void OnRequestTimeout(int request_id); | 88 void OnRequestTimeout(int request_id); |
| 79 | 89 |
| 80 RequestMap requests_; | 90 RequestMap requests_; |
| 81 int next_id_; | 91 int next_id_; |
| 82 base::TimeDelta timeout_; | 92 base::TimeDelta timeout_; |
| 83 base::WeakPtrFactory<RequestManager> weak_ptr_factory_; | 93 base::WeakPtrFactory<RequestManager> weak_ptr_factory_; |
| 84 | 94 |
| 85 DISALLOW_COPY_AND_ASSIGN(RequestManager); | 95 DISALLOW_COPY_AND_ASSIGN(RequestManager); |
| 86 }; | 96 }; |
| 87 | 97 |
| 88 } // namespace file_system_provider | 98 } // namespace file_system_provider |
| 89 } // namespace chromeos | 99 } // namespace chromeos |
| 90 | 100 |
| 91 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_REQUEST_MANAGER_H_ | 101 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_REQUEST_MANAGER_H_ |
| OLD | NEW |