| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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_GDATA_OPERATIONS_BASE_H_ | |
| 6 #define CHROME_BROWSER_CHROMEOS_GDATA_OPERATIONS_BASE_H_ | |
| 7 | |
| 8 #include <string> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/callback.h" | |
| 12 #include "chrome/browser/chromeos/gdata/gdata_errorcode.h" | |
| 13 #include "chrome/browser/chromeos/gdata/operation_registry.h" | |
| 14 #include "google_apis/gaia/oauth2_access_token_consumer.h" | |
| 15 #include "googleurl/src/gurl.h" | |
| 16 #include "net/http/http_response_headers.h" | |
| 17 #include "net/url_request/url_fetcher.h" | |
| 18 #include "net/url_request/url_fetcher_delegate.h" | |
| 19 | |
| 20 class OAuth2AccessTokenFetcher; | |
| 21 | |
| 22 namespace gdata { | |
| 23 | |
| 24 //================================ AuthOperation =============================== | |
| 25 | |
| 26 // Callback type for authentication related DriveServiceInterface calls. | |
| 27 typedef base::Callback<void(GDataErrorCode error, | |
| 28 const std::string& token)> AuthStatusCallback; | |
| 29 | |
| 30 // OAuth2 authorization token retrieval operation. | |
| 31 class AuthOperation : public OperationRegistry::Operation, | |
| 32 public OAuth2AccessTokenConsumer { | |
| 33 public: | |
| 34 AuthOperation(OperationRegistry* registry, | |
| 35 const AuthStatusCallback& callback, | |
| 36 const std::vector<std::string>& scopes, | |
| 37 const std::string& refresh_token); | |
| 38 virtual ~AuthOperation(); | |
| 39 void Start(); | |
| 40 | |
| 41 // Overridden from OAuth2AccessTokenConsumer: | |
| 42 virtual void OnGetTokenSuccess(const std::string& access_token, | |
| 43 const base::Time& expiration_time) OVERRIDE; | |
| 44 virtual void OnGetTokenFailure(const GoogleServiceAuthError& error) OVERRIDE; | |
| 45 | |
| 46 // Overridden from OperationRegistry::Operation | |
| 47 virtual void DoCancel() OVERRIDE; | |
| 48 | |
| 49 private: | |
| 50 std::string refresh_token_; | |
| 51 AuthStatusCallback callback_; | |
| 52 std::vector<std::string> scopes_; | |
| 53 scoped_ptr<OAuth2AccessTokenFetcher> oauth2_access_token_fetcher_; | |
| 54 | |
| 55 DISALLOW_COPY_AND_ASSIGN(AuthOperation); | |
| 56 }; | |
| 57 | |
| 58 //======================= AuthenticatedOperationInterface ====================== | |
| 59 | |
| 60 // An interface for implementing an operation used by DriveServiceInterface. | |
| 61 class AuthenticatedOperationInterface { | |
| 62 public: | |
| 63 // Callback to DriveServiceInterface upon for re-authentication. | |
| 64 typedef base::Callback<void(AuthenticatedOperationInterface* operation)> | |
| 65 ReAuthenticateCallback; | |
| 66 | |
| 67 virtual ~AuthenticatedOperationInterface() {} | |
| 68 | |
| 69 // Starts the actual operation after obtaining an authentication token | |
| 70 // |auth_token|. | |
| 71 virtual void Start(const std::string& auth_token) = 0; | |
| 72 | |
| 73 // Invoked when the authentication failed with an error code |code|. | |
| 74 virtual void OnAuthFailed(GDataErrorCode code) = 0; | |
| 75 | |
| 76 // Sets the callback to DriveServiceInterface when the operation restarts due | |
| 77 // to an authentication failure. | |
| 78 virtual void SetReAuthenticateCallback( | |
| 79 const ReAuthenticateCallback& callback) = 0; | |
| 80 }; | |
| 81 | |
| 82 //============================ UrlFetchOperationBase =========================== | |
| 83 | |
| 84 // Callback type for getting the content from URLFetcher::GetResponseAsString(). | |
| 85 typedef base::Callback<void( | |
| 86 GDataErrorCode error, | |
| 87 scoped_ptr<std::string> content)> GetContentCallback; | |
| 88 | |
| 89 // Base class for operations that are fetching URLs. | |
| 90 class UrlFetchOperationBase : public AuthenticatedOperationInterface, | |
| 91 public OperationRegistry::Operation, | |
| 92 public net::URLFetcherDelegate { | |
| 93 public: | |
| 94 // Overridden from AuthenticatedOperationInterface. | |
| 95 virtual void Start(const std::string& auth_token) OVERRIDE; | |
| 96 | |
| 97 // Overridden from AuthenticatedOperationInterface. | |
| 98 virtual void SetReAuthenticateCallback( | |
| 99 const ReAuthenticateCallback& callback) OVERRIDE; | |
| 100 | |
| 101 protected: | |
| 102 explicit UrlFetchOperationBase(OperationRegistry* registry); | |
| 103 UrlFetchOperationBase(OperationRegistry* registry, | |
| 104 OperationRegistry::OperationType type, | |
| 105 const FilePath& path); | |
| 106 virtual ~UrlFetchOperationBase(); | |
| 107 | |
| 108 // Gets URL for the request. | |
| 109 virtual GURL GetURL() const = 0; | |
| 110 // Returns the request type. A derived class should override this method | |
| 111 // for a request type other than HTTP GET. | |
| 112 virtual net::URLFetcher::RequestType GetRequestType() const; | |
| 113 // Returns the extra HTTP headers for the request. A derived class should | |
| 114 // override this method to specify any extra headers needed for the request. | |
| 115 virtual std::vector<std::string> GetExtraRequestHeaders() const; | |
| 116 // Used by a derived class to add any content data to the request. | |
| 117 // Returns true if |upload_content_type| and |upload_content| are updated | |
| 118 // with the content type and data for the request. | |
| 119 virtual bool GetContentData(std::string* upload_content_type, | |
| 120 std::string* upload_content); | |
| 121 | |
| 122 // Invoked by OnURLFetchComplete when the operation completes without an | |
| 123 // authentication error. Must be implemented by a derived class. | |
| 124 virtual void ProcessURLFetchResults(const net::URLFetcher* source) = 0; | |
| 125 | |
| 126 // Invoked when it needs to notify the status. Chunked operations that | |
| 127 // constructs a logically single operation from multiple physical operations | |
| 128 // should notify resume/suspend instead of start/finish. | |
| 129 virtual void NotifyStartToOperationRegistry(); | |
| 130 virtual void NotifySuccessToOperationRegistry(); | |
| 131 | |
| 132 // Invoked by this base class upon an authentication error or cancel by | |
| 133 // an user operation. Must be implemented by a derived class. | |
| 134 virtual void RunCallbackOnPrematureFailure(GDataErrorCode code) = 0; | |
| 135 | |
| 136 // Implement OperationRegistry::Operation | |
| 137 virtual void DoCancel() OVERRIDE; | |
| 138 | |
| 139 // Overridden from URLFetcherDelegate. | |
| 140 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
| 141 | |
| 142 // Overridden from AuthenticatedOperationInterface. | |
| 143 virtual void OnAuthFailed(GDataErrorCode code) OVERRIDE; | |
| 144 | |
| 145 // Invoked when ProcessURLFetchResults() is completed. | |
| 146 void OnProcessURLFetchResultsComplete(bool result); | |
| 147 | |
| 148 // Returns an appropriate GDataErrorCode based on the HTTP response code and | |
| 149 // the status of the URLFetcher. | |
| 150 GDataErrorCode GetErrorCode(const net::URLFetcher* source) const; | |
| 151 | |
| 152 std::string GetResponseHeadersAsString( | |
| 153 const net::URLFetcher* url_fetcher); | |
| 154 | |
| 155 ReAuthenticateCallback re_authenticate_callback_; | |
| 156 int re_authenticate_count_; | |
| 157 bool save_temp_file_; | |
| 158 FilePath output_file_path_; | |
| 159 scoped_ptr<net::URLFetcher> url_fetcher_; | |
| 160 bool started_; | |
| 161 }; | |
| 162 | |
| 163 //============================ EntryActionOperation ============================ | |
| 164 | |
| 165 // Callback type for Delete/Move DocumentServiceInterface calls. | |
| 166 typedef base::Callback<void(GDataErrorCode error, | |
| 167 const GURL& document_url)> EntryActionCallback; | |
| 168 | |
| 169 // This class performs a simple action over a given entry (document/file). | |
| 170 // It is meant to be used for operations that return no JSON blobs. | |
| 171 class EntryActionOperation : public UrlFetchOperationBase { | |
| 172 public: | |
| 173 EntryActionOperation(OperationRegistry* registry, | |
| 174 const EntryActionCallback& callback, | |
| 175 const GURL& document_url); | |
| 176 virtual ~EntryActionOperation(); | |
| 177 | |
| 178 protected: | |
| 179 // Overridden from UrlFetchOperationBase. | |
| 180 virtual void ProcessURLFetchResults(const net::URLFetcher* source) OVERRIDE; | |
| 181 virtual void RunCallbackOnPrematureFailure(GDataErrorCode code) OVERRIDE; | |
| 182 | |
| 183 const GURL& document_url() const { return document_url_; } | |
| 184 | |
| 185 private: | |
| 186 EntryActionCallback callback_; | |
| 187 GURL document_url_; | |
| 188 | |
| 189 DISALLOW_COPY_AND_ASSIGN(EntryActionOperation); | |
| 190 }; | |
| 191 | |
| 192 //============================== GetDataOperation ============================== | |
| 193 | |
| 194 // Callback type for DocumentServiceInterface::GetDocuments. | |
| 195 // Note: feed_data argument should be passed using base::Passed(&feed_data), not | |
| 196 // feed_data.Pass(). | |
| 197 typedef base::Callback<void(GDataErrorCode error, | |
| 198 scoped_ptr<base::Value> feed_data)> GetDataCallback; | |
| 199 | |
| 200 // This class performs the operation for fetching and parsing JSON data content. | |
| 201 class GetDataOperation : public UrlFetchOperationBase { | |
| 202 public: | |
| 203 GetDataOperation(OperationRegistry* registry, | |
| 204 const GetDataCallback& callback); | |
| 205 virtual ~GetDataOperation(); | |
| 206 | |
| 207 // Parse GData JSON response. | |
| 208 virtual void ParseResponse(GDataErrorCode fetch_error_code, | |
| 209 const std::string& data); | |
| 210 | |
| 211 protected: | |
| 212 // Overridden from UrlFetchOperationBase. | |
| 213 virtual void ProcessURLFetchResults(const net::URLFetcher* source) OVERRIDE; | |
| 214 virtual void RunCallbackOnPrematureFailure( | |
| 215 GDataErrorCode fetch_error_code) OVERRIDE; | |
| 216 void RunCallback(GDataErrorCode fetch_error_code, | |
| 217 scoped_ptr<base::Value> value); | |
| 218 | |
| 219 private: | |
| 220 // Called when ParseJsonOnBlockingPool() is completed. | |
| 221 void OnDataParsed(gdata::GDataErrorCode fetch_error_code, | |
| 222 scoped_ptr<base::Value>* value); | |
| 223 | |
| 224 GetDataCallback callback_; | |
| 225 | |
| 226 // Note: This should remain the last member so it'll be destroyed and | |
| 227 // invalidate its weak pointers before any other members are destroyed. | |
| 228 base::WeakPtrFactory<GetDataOperation> weak_ptr_factory_; | |
| 229 DISALLOW_COPY_AND_ASSIGN(GetDataOperation); | |
| 230 }; | |
| 231 | |
| 232 } // namespace gdata | |
| 233 | |
| 234 #endif // CHROME_BROWSER_CHROMEOS_GDATA_OPERATIONS_BASE_H_ | |
| OLD | NEW |