Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Side by Side Diff: chrome/browser/google_apis/base_operations.h

Issue 11421170: google_apis: Clean up base_operations.h (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: polish Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/browser/google_apis/base_operations.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 //
5 // This file provides base classes used to implement operations for Google APIs.
4 6
5 #ifndef CHROME_BROWSER_GOOGLE_APIS_BASE_OPERATIONS_H_ 7 #ifndef CHROME_BROWSER_GOOGLE_APIS_BASE_OPERATIONS_H_
6 #define CHROME_BROWSER_GOOGLE_APIS_BASE_OPERATIONS_H_ 8 #define CHROME_BROWSER_GOOGLE_APIS_BASE_OPERATIONS_H_
7 9
8 #include <string> 10 #include <string>
9 #include <vector> 11 #include <vector>
10 12
11 #include "base/callback.h" 13 #include "base/callback.h"
12 #include "base/memory/weak_ptr.h" 14 #include "base/memory/weak_ptr.h"
13 #include "chrome/browser/google_apis/gdata_errorcode.h" 15 #include "chrome/browser/google_apis/gdata_errorcode.h"
14 #include "chrome/browser/google_apis/operation_registry.h" 16 #include "chrome/browser/google_apis/operation_registry.h"
15 #include "google_apis/gaia/oauth2_access_token_consumer.h" 17 #include "google_apis/gaia/oauth2_access_token_consumer.h"
16 #include "googleurl/src/gurl.h" 18 #include "googleurl/src/gurl.h"
17 #include "net/url_request/url_fetcher.h" 19 #include "net/url_request/url_fetcher.h"
18 #include "net/url_request/url_fetcher_delegate.h" 20 #include "net/url_request/url_fetcher_delegate.h"
19 21
20 class OAuth2AccessTokenFetcher; 22 class OAuth2AccessTokenFetcher;
21 23
22 namespace base { 24 namespace base {
23 class Value; 25 class Value;
24 } // namespace base 26 } // namespace base
25 27
26 namespace google_apis { 28 namespace google_apis {
27 29
28 //======================= AuthenticatedOperationInterface ====================== 30 //======================= AuthenticatedOperationInterface ======================
29 31
30 // An interface for implementing an operation used by DriveServiceInterface. 32 // An interface class for implementing an operation which requires OAuth2
33 // authentication.
31 class AuthenticatedOperationInterface { 34 class AuthenticatedOperationInterface {
32 public: 35 public:
33 // Callback to DriveServiceInterface upon for re-authentication. 36 // Callback to DriveServiceInterface upon for re-authentication.
34 typedef base::Callback<void(AuthenticatedOperationInterface* operation)> 37 typedef base::Callback<void(AuthenticatedOperationInterface* operation)>
35 ReAuthenticateCallback; 38 ReAuthenticateCallback;
36 39
37 virtual ~AuthenticatedOperationInterface() {} 40 virtual ~AuthenticatedOperationInterface() {}
38 41
39 // Starts the actual operation after obtaining an authentication token 42 // Starts the actual operation after obtaining an authentication token
40 // |auth_token|. User-Agent header will be set to |custom_user_agent| if 43 // |auth_token|. User-Agent header will be set to |custom_user_agent| if
41 // the value is not empty. 44 // the value is not empty.
42 virtual void Start(const std::string& auth_token, 45 virtual void Start(const std::string& auth_token,
43 const std::string& custom_user_agent) = 0; 46 const std::string& custom_user_agent) = 0;
44 47
45 // Invoked when the authentication failed with an error code |code|. 48 // Invoked when the authentication failed with an error code |code|.
46 virtual void OnAuthFailed(GDataErrorCode code) = 0; 49 virtual void OnAuthFailed(GDataErrorCode code) = 0;
47 50
48 // Sets the callback to DriveServiceInterface when the operation restarts due 51 // Sets the callback to DriveServiceInterface when the operation restarts due
49 // to an authentication failure. 52 // to an authentication failure.
53 // This function should be called before Start().
54 // TODO(satorux): Make it a parameter of Start(). crbug.com/163535.
50 virtual void SetReAuthenticateCallback( 55 virtual void SetReAuthenticateCallback(
51 const ReAuthenticateCallback& callback) = 0; 56 const ReAuthenticateCallback& callback) = 0;
52 57
53 // Gets a weak pointer to this operation object. Since operations may be 58 // Gets a weak pointer to this operation object. Since operations may be
54 // deleted when it is canceled by user action, for posting asynchronous tasks 59 // deleted when it is canceled by user action, for posting asynchronous tasks
55 // on the authentication operation object, weak pointers have to be used. 60 // on the authentication operation object, weak pointers have to be used.
56 // TODO(kinaba): crbug.com/134814 use more clean life time management than 61 // TODO(kinaba): crbug.com/134814 use more clean life time management than
57 // using weak pointers, while deprecating OperationRegistry. 62 // using weak pointers, while deprecating OperationRegistry.
58 virtual base::WeakPtr<AuthenticatedOperationInterface> GetWeakPtr() = 0; 63 virtual base::WeakPtr<AuthenticatedOperationInterface> GetWeakPtr() = 0;
59 }; 64 };
60 65
61 //============================ UrlFetchOperationBase =========================== 66 //============================ UrlFetchOperationBase ===========================
62 67
63 // Callback type for getting the content from URLFetcher::GetResponseAsString(). 68 // Callback type for getting the content from URLFetcher::GetResponseAsString().
64 typedef base::Callback<void( 69 typedef base::Callback<void(
65 GDataErrorCode error, 70 GDataErrorCode error,
66 scoped_ptr<std::string> content)> GetContentCallback; 71 scoped_ptr<std::string> content)> GetContentCallback;
67 72
68 // Base class for operations that are fetching URLs. 73 // Base class for operations that are fetching URLs.
69 class UrlFetchOperationBase : public AuthenticatedOperationInterface, 74 class UrlFetchOperationBase : public AuthenticatedOperationInterface,
70 public OperationRegistry::Operation, 75 public OperationRegistry::Operation,
71 public net::URLFetcherDelegate { 76 public net::URLFetcherDelegate {
72 public: 77 public:
73 // Overridden from AuthenticatedOperationInterface. 78 // AuthenticatedOperationInterface overrides.
74 virtual void Start(const std::string& auth_token, 79 virtual void Start(const std::string& auth_token,
75 const std::string& custom_user_agent) OVERRIDE; 80 const std::string& custom_user_agent) OVERRIDE;
76
77 // Overridden from AuthenticatedOperationInterface.
78 virtual void SetReAuthenticateCallback( 81 virtual void SetReAuthenticateCallback(
79 const ReAuthenticateCallback& callback) OVERRIDE; 82 const ReAuthenticateCallback& callback) OVERRIDE;
80
81 // Overridden from AuthenticatedOperationInterface.
82 virtual base::WeakPtr<AuthenticatedOperationInterface> GetWeakPtr() OVERRIDE; 83 virtual base::WeakPtr<AuthenticatedOperationInterface> GetWeakPtr() OVERRIDE;
83 84
84 protected: 85 protected:
85 explicit UrlFetchOperationBase(OperationRegistry* registry); 86 explicit UrlFetchOperationBase(OperationRegistry* registry);
87 // Use this constructor when you need to implement operations that take a
88 // drive file path (ex. for downloading and uploading).
89 // TODO(satorux): Remove the drive file path hack. crbug.com/163296
86 UrlFetchOperationBase(OperationRegistry* registry, 90 UrlFetchOperationBase(OperationRegistry* registry,
87 OperationType type, 91 OperationType type,
88 const FilePath& path); 92 const FilePath& drive_file_path);
89 virtual ~UrlFetchOperationBase(); 93 virtual ~UrlFetchOperationBase();
90 94
91 // Gets URL for the request. 95 // Gets URL for the request.
92 virtual GURL GetURL() const = 0; 96 virtual GURL GetURL() const = 0;
97
93 // Returns the request type. A derived class should override this method 98 // Returns the request type. A derived class should override this method
94 // for a request type other than HTTP GET. 99 // for a request type other than HTTP GET.
95 virtual net::URLFetcher::RequestType GetRequestType() const; 100 virtual net::URLFetcher::RequestType GetRequestType() const;
101
96 // Returns the extra HTTP headers for the request. A derived class should 102 // Returns the extra HTTP headers for the request. A derived class should
97 // override this method to specify any extra headers needed for the request. 103 // override this method to specify any extra headers needed for the request.
98 virtual std::vector<std::string> GetExtraRequestHeaders() const; 104 virtual std::vector<std::string> GetExtraRequestHeaders() const;
105
99 // Used by a derived class to add any content data to the request. 106 // Used by a derived class to add any content data to the request.
100 // Returns true if |upload_content_type| and |upload_content| are updated 107 // Returns true if |upload_content_type| and |upload_content| are updated
101 // with the content type and data for the request. 108 // with the content type and data for the request.
102 virtual bool GetContentData(std::string* upload_content_type, 109 virtual bool GetContentData(std::string* upload_content_type,
103 std::string* upload_content); 110 std::string* upload_content);
104 111
105 // Invoked by OnURLFetchComplete when the operation completes without an 112 // Invoked by OnURLFetchComplete when the operation completes without an
106 // authentication error. Must be implemented by a derived class. 113 // authentication error. Must be implemented by a derived class.
107 virtual void ProcessURLFetchResults(const net::URLFetcher* source) = 0; 114 virtual void ProcessURLFetchResults(const net::URLFetcher* source) = 0;
108 115
109 // Invoked when it needs to notify the status. Chunked operations that 116 // Invoked when it needs to notify the status. Chunked operations that
110 // constructs a logically single operation from multiple physical operations 117 // constructs a logically single operation from multiple physical operations
111 // should notify resume/suspend instead of start/finish. 118 // should notify resume/suspend instead of start/finish.
112 virtual void NotifyStartToOperationRegistry(); 119 virtual void NotifyStartToOperationRegistry();
113 virtual void NotifySuccessToOperationRegistry(); 120 virtual void NotifySuccessToOperationRegistry();
114 121
115 // Invoked by this base class upon an authentication error or cancel by 122 // Invoked by this base class upon an authentication error or cancel by
116 // an user operation. Must be implemented by a derived class. 123 // an user operation. Must be implemented by a derived class.
117 virtual void RunCallbackOnPrematureFailure(GDataErrorCode code) = 0; 124 virtual void RunCallbackOnPrematureFailure(GDataErrorCode code) = 0;
118 125
119 // Implement OperationRegistry::Operation
120 virtual void DoCancel() OVERRIDE;
121
122 // Overridden from URLFetcherDelegate.
123 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
124
125 // Overridden from AuthenticatedOperationInterface.
126 virtual void OnAuthFailed(GDataErrorCode code) OVERRIDE;
127
128 // Invoked when ProcessURLFetchResults() is completed. 126 // Invoked when ProcessURLFetchResults() is completed.
129 void OnProcessURLFetchResultsComplete(bool result); 127 void OnProcessURLFetchResultsComplete(bool result);
130 128
131 // Returns an appropriate GDataErrorCode based on the HTTP response code and 129 // Returns an appropriate GDataErrorCode based on the HTTP response code and
132 // the status of the URLFetcher. 130 // the status of the URLFetcher.
133 GDataErrorCode GetErrorCode(const net::URLFetcher* source) const; 131 static GDataErrorCode GetErrorCode(const net::URLFetcher* source);
134 132
135 std::string GetResponseHeadersAsString( 133 // The following members are used by DownloadFileOperation.
136 const net::URLFetcher* url_fetcher); 134 // TODO(satorux): Make them private.
135 bool save_temp_file_;
136 FilePath output_file_path_;
137
138 private:
139 // OperationRegistry::Operation overrides.
140 virtual void DoCancel() OVERRIDE;
141
142 // URLFetcherDelegate overrides.
143 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
144
145 // AuthenticatedOperationInterface overrides.
146 virtual void OnAuthFailed(GDataErrorCode code) OVERRIDE;
137 147
138 ReAuthenticateCallback re_authenticate_callback_; 148 ReAuthenticateCallback re_authenticate_callback_;
139 int re_authenticate_count_; 149 int re_authenticate_count_;
140 bool save_temp_file_;
141 FilePath output_file_path_;
142 scoped_ptr<net::URLFetcher> url_fetcher_; 150 scoped_ptr<net::URLFetcher> url_fetcher_;
143 bool started_; 151 bool started_;
144 152
145 // WeakPtrFactory bound to the UI thread. 153 // WeakPtrFactory bound to the UI thread.
146 // Note: This should remain the last member so it'll be destroyed and 154 // Note: This should remain the last member so it'll be destroyed and
147 // invalidate its weak pointers before any other members are destroyed. 155 // invalidate its weak pointers before any other members are destroyed.
148 base::WeakPtrFactory<UrlFetchOperationBase> weak_ptr_factory_; 156 base::WeakPtrFactory<UrlFetchOperationBase> weak_ptr_factory_;
149 }; 157 };
150 158
151 //============================ EntryActionOperation ============================ 159 //============================ EntryActionOperation ============================
152 160
153 // Callback type for Delete/Move DocumentServiceInterface calls. 161 // Callback type for Delete/Move DocumentServiceInterface calls.
154 typedef base::Callback<void(GDataErrorCode error)> EntryActionCallback; 162 typedef base::Callback<void(GDataErrorCode error)> EntryActionCallback;
155 163
156 // This class performs a simple action over a given entry (document/file). 164 // This class performs a simple action over a given entry (document/file).
157 // It is meant to be used for operations that return no JSON blobs. 165 // It is meant to be used for operations that return no JSON blobs.
158 class EntryActionOperation : public UrlFetchOperationBase { 166 class EntryActionOperation : public UrlFetchOperationBase {
159 public: 167 public:
160 EntryActionOperation(OperationRegistry* registry, 168 EntryActionOperation(OperationRegistry* registry,
161 const EntryActionCallback& callback); 169 const EntryActionCallback& callback);
162 virtual ~EntryActionOperation(); 170 virtual ~EntryActionOperation();
163 171
164 protected: 172 protected:
165 // Overridden from UrlFetchOperationBase. 173 // Overridden from UrlFetchOperationBase.
166 virtual void ProcessURLFetchResults(const net::URLFetcher* source) OVERRIDE; 174 virtual void ProcessURLFetchResults(const net::URLFetcher* source) OVERRIDE;
167 virtual void RunCallbackOnPrematureFailure(GDataErrorCode code) OVERRIDE; 175 virtual void RunCallbackOnPrematureFailure(GDataErrorCode code) OVERRIDE;
168 176
169 private: 177 private:
170 EntryActionCallback callback_; 178 const EntryActionCallback callback_;
171 179
172 DISALLOW_COPY_AND_ASSIGN(EntryActionOperation); 180 DISALLOW_COPY_AND_ASSIGN(EntryActionOperation);
173 }; 181 };
174 182
175 //============================== GetDataOperation ============================== 183 //============================== GetDataOperation ==============================
176 184
177 // Callback type for DocumentServiceInterface::GetDocuments. 185 // Callback type for DocumentServiceInterface::GetDocuments.
178 // Note: feed_data argument should be passed using base::Passed(&feed_data), not 186 // Note: feed_data argument should be passed using base::Passed(&feed_data), not
179 // feed_data.Pass(). 187 // feed_data.Pass().
180 typedef base::Callback<void(GDataErrorCode error, 188 typedef base::Callback<void(GDataErrorCode error,
181 scoped_ptr<base::Value> feed_data)> GetDataCallback; 189 scoped_ptr<base::Value> feed_data)> GetDataCallback;
182 190
183 // This class performs the operation for fetching and parsing JSON data content. 191 // This class performs the operation for fetching and converting the fetched
192 // content into a base::Value.
184 class GetDataOperation : public UrlFetchOperationBase { 193 class GetDataOperation : public UrlFetchOperationBase {
185 public: 194 public:
186 GetDataOperation(OperationRegistry* registry, 195 GetDataOperation(OperationRegistry* registry,
187 const GetDataCallback& callback); 196 const GetDataCallback& callback);
188 virtual ~GetDataOperation(); 197 virtual ~GetDataOperation();
189 198
190 // Parse GData JSON response. 199 // Parses JSON response. A derived class should override this function if
200 // the input data is not JSON (ex. XML).
191 virtual void ParseResponse(GDataErrorCode fetch_error_code, 201 virtual void ParseResponse(GDataErrorCode fetch_error_code,
192 const std::string& data); 202 const std::string& data);
193 203
194 protected: 204 // UrlFetchOperationBase overrides.
195 // Overridden from UrlFetchOperationBase.
196 virtual void ProcessURLFetchResults(const net::URLFetcher* source) OVERRIDE; 205 virtual void ProcessURLFetchResults(const net::URLFetcher* source) OVERRIDE;
197 virtual void RunCallbackOnPrematureFailure( 206
198 GDataErrorCode fetch_error_code) OVERRIDE; 207 // Runs |callback_| with the given parameters, if |callback_| is not null.
208 // TODO(satorux): Remove this by making |callback_| mandatory.
199 void RunCallback(GDataErrorCode fetch_error_code, 209 void RunCallback(GDataErrorCode fetch_error_code,
200 scoped_ptr<base::Value> value); 210 scoped_ptr<base::Value> value);
201 211
202 private: 212 private:
213 // UrlFetchOperationBase overrides.
214 virtual void RunCallbackOnPrematureFailure(
215 GDataErrorCode fetch_error_code) OVERRIDE;
216
203 // Called when ParseJsonOnBlockingPool() is completed. 217 // Called when ParseJsonOnBlockingPool() is completed.
204 void OnDataParsed(google_apis::GDataErrorCode fetch_error_code, 218 void OnDataParsed(google_apis::GDataErrorCode fetch_error_code,
205 scoped_ptr<base::Value> value); 219 scoped_ptr<base::Value> value);
206 220
207 GetDataCallback callback_; 221 const GetDataCallback callback_;
208 222
209 // Note: This should remain the last member so it'll be destroyed and 223 // Note: This should remain the last member so it'll be destroyed and
210 // invalidate its weak pointers before any other members are destroyed. 224 // invalidate its weak pointers before any other members are destroyed.
211 base::WeakPtrFactory<GetDataOperation> weak_ptr_factory_; 225 base::WeakPtrFactory<GetDataOperation> weak_ptr_factory_;
212 DISALLOW_COPY_AND_ASSIGN(GetDataOperation); 226 DISALLOW_COPY_AND_ASSIGN(GetDataOperation);
213 }; 227 };
214 228
215 } // namespace google_apis 229 } // namespace google_apis
216 230
217 #endif // CHROME_BROWSER_GOOGLE_APIS_BASE_OPERATIONS_H_ 231 #endif // CHROME_BROWSER_GOOGLE_APIS_BASE_OPERATIONS_H_
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/google_apis/base_operations.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698