OLD | NEW |
(Empty) | |
| 1 // Copyright 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_CHROME_TO_MOBILE_COMMON_CLOUD_PRINT_REQUEST_H_ |
| 6 #define CHROME_BROWSER_CHROME_TO_MOBILE_COMMON_CLOUD_PRINT_REQUEST_H_ |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "base/threading/non_thread_safe.h" |
| 11 #include "net/url_request/url_fetcher.h" |
| 12 #include "net/url_request/url_fetcher_delegate.h" |
| 13 |
| 14 class GURL; |
| 15 |
| 16 namespace net { |
| 17 class URLRequestContextGetter; |
| 18 } // namespace net |
| 19 |
| 20 class OAuth2TokenService; |
| 21 |
| 22 namespace chrome_to_mobile { |
| 23 |
| 24 class CloudPrintRequestFactory; |
| 25 |
| 26 // Class for google cloud print requests authenticated by oauth2 refresh tokens |
| 27 // managed by an |OAuth2TokenService|. |
| 28 // |
| 29 // It handles the common process that applies to all cloud print server |
| 30 // requests, such as cloud print header settings, common parameter generation |
| 31 // and oauth2 access token fetching. |
| 32 // |
| 33 // To use this class, call |CloudPrintRequest::CreateAndStart()| to create an |
| 34 // instance and start it with (1) request data, (2) additional settings, and (3) |
| 35 // a pointer to the object to be notified when the request has been loaded, |
| 36 // which should inherit from |CloudPrintRequest::Delegate|. |
| 37 // |
| 38 // When the request is completed, |
| 39 // |CloudPrintRequest::Delegate::OnRequestComplete()| will be called with a |
| 40 // pointer to the |CloudPrintRequest|. From that point until the original |
| 41 // |CloudPrintRequest| instance is destroyed, accessor methods can be used to |
| 42 // check the status of the request. |
| 43 // |
| 44 // If the |CloudPrintRequest| is destroyed before the callback happens, the |
| 45 // request will be canceled and no callback will occur. |
| 46 // |
| 47 // The |CloudPrintRequest| instance can be created on any thread; |
| 48 // |CloudPrintRequest::Delegate::OnRequestComplete()| will be called back on |
| 49 // the same thread where the instance was craeted. |
| 50 class CloudPrintRequest : public base::NonThreadSafe { |
| 51 public: |
| 52 class Delegate { |
| 53 public: |
| 54 Delegate(); |
| 55 ~Delegate(); |
| 56 virtual void OnRequestComplete(CloudPrintRequest* source) = 0; |
| 57 }; |
| 58 |
| 59 // Struct that contains request related information. |
| 60 struct Settings { |
| 61 Settings(const std::string& cloud_print_client_id, |
| 62 net::URLRequestContextGetter* request_context_getter, |
| 63 OAuth2TokenService* oauth2_token_service); |
| 64 Settings(); |
| 65 |
| 66 std::string cloud_print_client_id; |
| 67 net::URLRequestContextGetter* request_context_getter; |
| 68 OAuth2TokenService* oauth2_token_service; |
| 69 }; |
| 70 |
| 71 CloudPrintRequest(); |
| 72 virtual ~CloudPrintRequest(); |
| 73 |
| 74 // Creates a |CloudPrintRequest| and starts the request. A request that |
| 75 // fetches oauth2 access token using |settings.oauth2_token_service| is sent |
| 76 // before this method returns. |
| 77 // |
| 78 // Note for testing purpose, |CloudPrintRequestFactory| can be registered (by |
| 79 // calling |SetFactory()|) to create |CloudPrintRequest|s. This method creates |
| 80 // a |CloudPrintRequestImpl| if no factory registered, otherwise the |
| 81 // registered factory is used to create requests. |
| 82 static CloudPrintRequest* CreateAndStart( |
| 83 const GURL& url, |
| 84 const std::string& additional_header, |
| 85 const net::URLFetcher::RequestType& request_type, |
| 86 const std::string& post_data_mime_type, |
| 87 const std::string& post_data, |
| 88 const Settings& setting, |
| 89 Delegate* delegate); |
| 90 |
| 91 // Convenient methods for GET and Post Requests. |
| 92 static CloudPrintRequest* CreateAndStartGetRequest(const GURL& request_url, |
| 93 const Settings& settings, |
| 94 Delegate* delegate); |
| 95 static CloudPrintRequest* CreateAndStartPostRequest( |
| 96 const GURL& request_url, |
| 97 const std::string& post_data_mime_type, |
| 98 const std::string& post_data, |
| 99 const Settings& settings, |
| 100 Delegate* delegate); |
| 101 |
| 102 // APIs to access the request status. |
| 103 // Returns true if it fails to get an oauth accss token to start the cloud |
| 104 // print request. |
| 105 virtual bool HasOAuth2AccessTokenFailure() const = 0; |
| 106 // Returns true if it has received authentication error from google cloud |
| 107 // print server. |
| 108 virtual bool HasCloudPrintAuthError() const = 0; |
| 109 // Returns the mime type if a response has been received; an empty string |
| 110 // otherwise. |
| 111 virtual std::string GetResponseMimeType() const = 0; |
| 112 // Returns the response data as a string and sets |*success| true if a cloud |
| 113 // response has been received with success response status and response code |
| 114 // 200. |
| 115 virtual std::string GetResponseData(bool* success) const = 0; |
| 116 |
| 117 // Helper methods used in testing to set up testing |
| 118 // |CloudPrintRequestFactory|. |
| 119 static void SetFactory(CloudPrintRequestFactory* factory); |
| 120 static CloudPrintRequestFactory* GetFactory(); |
| 121 |
| 122 private: |
| 123 DISALLOW_COPY_AND_ASSIGN(CloudPrintRequest); |
| 124 }; |
| 125 |
| 126 } // namespace chrome_to_mobile |
| 127 |
| 128 #endif // CHROME_BROWSER_CHROME_TO_MOBILE_COMMON_CLOUD_PRINT_REQUEST_H_ |
OLD | NEW |