OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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_LOCAL_DISCOVERY_CLOUD_PRINT_BASE_API_FLOW_H_ |
| 6 #define CHROME_BROWSER_LOCAL_DISCOVERY_CLOUD_PRINT_BASE_API_FLOW_H_ |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "chrome/browser/local_discovery/privet_constants.h" |
| 11 #include "chrome/browser/local_discovery/privet_http.h" |
| 12 #include "google_apis/gaia/oauth2_token_service.h" |
| 13 #include "net/url_request/url_fetcher.h" |
| 14 #include "net/url_request/url_fetcher_delegate.h" |
| 15 #include "net/url_request/url_request_context_getter.h" |
| 16 |
| 17 namespace local_discovery { |
| 18 |
| 19 // API call flow for communicating with cloud print. |
| 20 class CloudPrintBaseApiFlow : public net::URLFetcherDelegate, |
| 21 public OAuth2TokenService::Consumer { |
| 22 public: |
| 23 // TODO(noamsml): Better error model for this class. |
| 24 enum Status { |
| 25 SUCCESS, |
| 26 ERROR_TOKEN, |
| 27 ERROR_NETWORK, |
| 28 ERROR_HTTP_CODE, |
| 29 ERROR_FROM_SERVER, |
| 30 ERROR_MALFORMED_RESPONSE |
| 31 }; |
| 32 |
| 33 class Delegate { |
| 34 public: |
| 35 virtual ~Delegate() {} |
| 36 |
| 37 virtual void OnCloudPrintAPIFlowError(CloudPrintBaseApiFlow* flow, |
| 38 Status status) = 0; |
| 39 virtual void OnCloudPrintAPIFlowComplete( |
| 40 CloudPrintBaseApiFlow* flow, |
| 41 const base::DictionaryValue* value) = 0; |
| 42 }; |
| 43 |
| 44 // Create an OAuth2-based confirmation. |
| 45 CloudPrintBaseApiFlow(net::URLRequestContextGetter* request_context, |
| 46 OAuth2TokenService* token_service_, |
| 47 const GURL& automated_claim_url, |
| 48 Delegate* delegate); |
| 49 |
| 50 // Create a cookie-based confirmation. |
| 51 CloudPrintBaseApiFlow(net::URLRequestContextGetter* request_context, |
| 52 int user_index, |
| 53 const std::string& xsrf_token, |
| 54 const GURL& automated_claim_url, |
| 55 Delegate* delegate); |
| 56 |
| 57 // Create a cookie-based confirmation with no XSRF token (for requests that |
| 58 // don't need an XSRF token). |
| 59 CloudPrintBaseApiFlow(net::URLRequestContextGetter* request_context, |
| 60 int user_index, |
| 61 const GURL& automated_claim_url, |
| 62 Delegate* delegate); |
| 63 |
| 64 |
| 65 virtual ~CloudPrintBaseApiFlow(); |
| 66 |
| 67 void Start(); |
| 68 |
| 69 |
| 70 // net::URLFetcherDelegate implementation: |
| 71 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 72 |
| 73 // OAuth2TokenService::Consumer implementation: |
| 74 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
| 75 const std::string& access_token, |
| 76 const base::Time& expiration_time) OVERRIDE; |
| 77 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
| 78 const GoogleServiceAuthError& error) OVERRIDE; |
| 79 |
| 80 private: |
| 81 bool UseOAuth2() { return token_service_ != NULL; } |
| 82 |
| 83 void CreateRequest(const GURL& url); |
| 84 |
| 85 scoped_ptr<net::URLFetcher> url_fetcher_; |
| 86 scoped_ptr<OAuth2TokenService::Request> oauth_request_; |
| 87 scoped_refptr<net::URLRequestContextGetter> request_context_; |
| 88 OAuth2TokenService* token_service_; |
| 89 int user_index_; |
| 90 std::string xsrf_token_; |
| 91 GURL url_; |
| 92 Delegate* delegate_; |
| 93 }; |
| 94 |
| 95 } // namespace local_discovery |
| 96 |
| 97 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_CLOUD_PRINT_BASE_API_FLOW_H_ |
OLD | NEW |