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 CLOUD_PRINT_GCP20_PROTOTYPE_CLOUD_REQUESTER_H_ 6 #define CLOUD_PRINT_GCP20_PROTOTYPE_CLOUD_REQUESTER_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/basictypes.h" 12 #include "base/callback.h" 13 #include "base/memory/weak_ptr.h" 14 #include "base/values.h" 15 #include "cloud_print/gcp20/prototype/cloud_print_request.h" 16 #include "cloud_print/gcp20/prototype/cloud_print_response_parser.h" 17 #include "google_apis/gaia/gaia_oauth_client.h" 18 19 class CloudPrintURLRequestContextGetter; 20 class GURL; 21 class URLRequestContextGetter; 22 23 extern const char kCloudPrintUrl[]; 24 25 // Class for requesting CloudPrint server and parsing responses. 26 class CloudPrintRequester : public base::SupportsWeakPtr<CloudPrintRequester>, 27 public gaia::GaiaOAuthClient::Delegate, 28 public CloudPrintRequest::Delegate { 29 public: 30 class Delegate { 31 public: 32 Delegate() {} 33 virtual ~Delegate() {} 34 35 // Invoked when server respond for registration-start query and response is 36 // successfully parsed. 37 virtual void OnRegistrationStartResponseParsed( 38 const std::string& registration_token, 39 const std::string& complete_invite_url, 40 const std::string& device_id) = 0; 41 42 // Invoked when server respond for registration-getAuthCode query and 43 // response is successfully parsed. 44 virtual void OnGetAuthCodeResponseParsed( 45 const std::string& refresh_token, 46 const std::string& access_token, 47 int access_token_expires_in_seconds) = 0; 48 49 // Invoked when XMPP JID was received and it has to be saved. 50 virtual void OnXmppJidReceived(const std::string& xmpp_jid) = 0; 51 52 // Invoked when access_token was received after UpdateAccesstoken() call. 53 virtual void OnAccesstokenReceviced(const std::string& access_token, 54 int expires_in_seconds) = 0; 55 56 // Invoked when server respond with |"success" = false| or we cannot parse 57 // response. 58 virtual void OnRegistrationError(const std::string& description) = 0; 59 60 // Invoked when network connection cannot be established. 61 virtual void OnNetworkError() = 0; 62 63 // Invoked when server error is received or cannot parse json response. 64 virtual void OnServerError(const std::string& description) = 0; 65 66 // Invoked when authorization failed. 67 virtual void OnAuthError() = 0; 68 69 // Invoked when access_token is needed. 70 virtual std::string GetAccessToken() = 0; 71 72 // Invoked when fetch response was received. 73 virtual void OnPrintJobsAvailable( 74 const std::vector<cloud_print_response_parser::Job>& jobs) = 0; 75 76 // Invoked when printjob is finally downloaded and available for printing. 77 virtual void OnPrintJobDownloaded( 78 const cloud_print_response_parser::Job& job) = 0; 79 80 // Invoked when printjob is marked as done on CloudPrint server. 81 virtual void OnPrintJobDone() = 0; 82 }; 83 84 // Creates and initializes object. 85 CloudPrintRequester(scoped_refptr<base::SingleThreadTaskRunner> task_runner, 86 Delegate* delegate); 87 88 // Destroys the object. 89 virtual ~CloudPrintRequester(); 90 91 // Returns |true| if either |gaia| or |request| is awaiting for response. 92 bool IsBusy() const; 93 94 // Creates query to server for starting registration. 95 void StartRegistration(const std::string& proxy_id, 96 const std::string& device_name, 97 const std::string& user, const std::string& cdd); 98 99 // Creates request for completing registration and receiving refresh token. 100 void CompleteRegistration(); 101 102 // Creates request for fetching printjobs. 103 void FetchPrintJobs(const std::string& device_id); 104 105 // Creates request for updating accesstoken. 106 // TODO(maksymb): Handle expiration of accesstoken. 107 void UpdateAccesstoken(const std::string& refresh_token); 108 109 // Creates chain of requests for requesting printjob. 110 void RequestPrintJob(const cloud_print_response_parser::Job& job); 111 112 // Reports server that printjob has been printed. 113 void SendPrintJobDone(const std::string& job_id); 114 115 private: 116 typedef base::Callback<void(const std::string&)> ParserCallback; 117 118 // CloudPrintRequester::Delegate methods: 119 virtual void OnFetchComplete(const std::string& response) OVERRIDE; 120 virtual void OnFetchError(const std::string& server_api, 121 int server_code, 122 int server_http_code) OVERRIDE; 123 virtual void OnFetchTimeoutReached() OVERRIDE; 124 125 // gaia::GaiaOAuthClient::Delegate methods: 126 virtual void OnGetTokensResponse(const std::string& refresh_token, 127 const std::string& access_token, 128 int expires_in_seconds) OVERRIDE; 129 virtual void OnRefreshTokenResponse(const std::string& access_token, 130 int expires_in_seconds) OVERRIDE; 131 virtual void OnOAuthError() OVERRIDE; 132 virtual void OnNetworkError(int response_code) OVERRIDE; 133 134 // Creates GET request. 135 scoped_ptr<CloudPrintRequest> CreateGet(const GURL& url, 136 const ParserCallback& callback); 137 138 // Creates POST request. 139 scoped_ptr<CloudPrintRequest> CreatePost(const GURL& url, 140 const std::string& content, 141 const std::string& mimetype, 142 const ParserCallback& callback); 143 144 // Deletes all info about current request. 145 void EraseRequest(); 146 147 // Parses register-start server response. 148 void ParseRegisterStart(const std::string& response); 149 150 // Parses register-complete server response. Initializes gaia (OAuth client) 151 // and receives refresh token. 152 void ParseRegisterComplete(const std::string& response); 153 154 // Parses fetch printjobs server response. 155 void ParseFetch(const std::string& response); 156 157 // Invoked after receiving printjob ticket. 158 void ParseGetPrintJobTicket(const std::string& response); 159 160 // Invoked after receiving printjob file. 161 void ParseGetPrintJobData(const std::string& response); 162 163 // Invoked after marking printjob as DONE. 164 void ParsePrintJobDone(const std::string& response); 165 166 // Invoked after marking printjob as IN_PROGRESS. 167 void ParsePrintJobInProgress(const std::string& response); 168 169 // |request| contains |NULL| if no server response is awaiting. Otherwise wait 170 // until callback will be called will be called and close connection. 171 scoped_ptr<CloudPrintRequest> request_; 172 173 // Contains information about current printjob. Information is filled by 174 // CloudPrint server responses. 175 scoped_ptr<cloud_print_response_parser::Job> current_print_job_; 176 177 // CloudPrint context getter. 178 scoped_refptr<net::URLRequestContextGetter> context_getter_; 179 180 // URL for completing registration and receiving OAuth account. 181 std::string polling_url_; 182 183 // OAuth client information (client_id, client_secret, etc). 184 gaia::OAuthClientInfo oauth_client_info_; 185 186 // OAuth client. 187 scoped_ptr<gaia::GaiaOAuthClient> gaia_; 188 189 ParserCallback parser_callback_; 190 191 Delegate* delegate_; 192 193 DISALLOW_COPY_AND_ASSIGN(CloudPrintRequester); 194 }; 195 196 #endif // CLOUD_PRINT_GCP20_PROTOTYPE_CLOUD_REQUESTER_H_ 197 198