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_PRIVET_HTTP_IMPL_H_ 6 #define CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_IMPL_H_ 7 8 #include <string> 9 10 #include "base/callback.h" 11 #include "base/file_util.h" 12 #include "base/memory/ref_counted.h" 13 #include "base/memory/weak_ptr.h" 14 #include "chrome/browser/local_discovery/privet_http.h" 15 #include "components/cloud_devices/common/cloud_device_description.h" 16 #include "printing/pdf_render_settings.h" 17 18 namespace printing { 19 struct PwgRasterSettings; 20 }; 21 22 namespace local_discovery { 23 24 class PrivetHTTPClient; 25 26 class PrivetInfoOperationImpl : public PrivetJSONOperation, 27 public PrivetURLFetcher::Delegate { 28 public: 29 PrivetInfoOperationImpl(PrivetHTTPClient* privet_client, 30 const PrivetJSONOperation::ResultCallback& callback); 31 virtual ~PrivetInfoOperationImpl(); 32 33 virtual void Start() OVERRIDE; 34 35 virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE; 36 37 virtual void OnError(PrivetURLFetcher* fetcher, 38 PrivetURLFetcher::ErrorType error) OVERRIDE; 39 virtual void OnParsedJson(PrivetURLFetcher* fetcher, 40 const base::DictionaryValue* value, 41 bool has_error) OVERRIDE; 42 43 private: 44 PrivetHTTPClient* privet_client_; 45 PrivetJSONOperation::ResultCallback callback_; 46 scoped_ptr<PrivetURLFetcher> url_fetcher_; 47 }; 48 49 class PrivetRegisterOperationImpl 50 : public PrivetRegisterOperation, 51 public PrivetURLFetcher::Delegate, 52 public base::SupportsWeakPtr<PrivetRegisterOperationImpl> { 53 public: 54 PrivetRegisterOperationImpl(PrivetHTTPClient* privet_client, 55 const std::string& user, 56 PrivetRegisterOperation::Delegate* delegate); 57 virtual ~PrivetRegisterOperationImpl(); 58 59 virtual void Start() OVERRIDE; 60 virtual void Cancel() OVERRIDE; 61 virtual void CompleteRegistration() OVERRIDE; 62 63 virtual void OnError(PrivetURLFetcher* fetcher, 64 PrivetURLFetcher::ErrorType error) OVERRIDE; 65 66 virtual void OnParsedJson(PrivetURLFetcher* fetcher, 67 const base::DictionaryValue* value, 68 bool has_error) OVERRIDE; 69 70 virtual void OnNeedPrivetToken( 71 PrivetURLFetcher* fetcher, 72 const PrivetURLFetcher::TokenCallback& callback) OVERRIDE; 73 74 virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE; 75 private: 76 class Cancelation : public PrivetURLFetcher::Delegate { 77 public: 78 Cancelation(PrivetHTTPClient* privet_client, const std::string& user); 79 virtual ~Cancelation(); 80 81 virtual void OnError(PrivetURLFetcher* fetcher, 82 PrivetURLFetcher::ErrorType error) OVERRIDE; 83 84 virtual void OnParsedJson(PrivetURLFetcher* fetcher, 85 const base::DictionaryValue* value, 86 bool has_error) OVERRIDE; 87 88 void Cleanup(); 89 90 private: 91 scoped_ptr<PrivetURLFetcher> url_fetcher_; 92 }; 93 94 // Arguments is JSON value from request. 95 typedef base::Callback<void(const base::DictionaryValue&)> 96 ResponseHandler; 97 98 void StartInfoOperation(); 99 void OnPrivetInfoDone(const base::DictionaryValue* value); 100 101 void StartResponse(const base::DictionaryValue& value); 102 void GetClaimTokenResponse(const base::DictionaryValue& value); 103 void CompleteResponse(const base::DictionaryValue& value); 104 105 void SendRequest(const std::string& action); 106 107 std::string user_; 108 std::string current_action_; 109 scoped_ptr<PrivetURLFetcher> url_fetcher_; 110 PrivetRegisterOperation::Delegate* delegate_; 111 PrivetHTTPClient* privet_client_; 112 ResponseHandler next_response_handler_; 113 // Required to ensure destroying completed register operations doesn't cause 114 // extraneous cancelations. 115 bool ongoing_; 116 117 scoped_ptr<PrivetJSONOperation> info_operation_; 118 std::string expected_id_; 119 }; 120 121 class PrivetJSONOperationImpl : public PrivetJSONOperation, 122 public PrivetURLFetcher::Delegate { 123 public: 124 PrivetJSONOperationImpl(PrivetHTTPClient* privet_client, 125 const std::string& path, 126 const std::string& query_params, 127 const PrivetJSONOperation::ResultCallback& callback); 128 virtual ~PrivetJSONOperationImpl(); 129 virtual void Start() OVERRIDE; 130 131 virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE; 132 133 virtual void OnError(PrivetURLFetcher* fetcher, 134 PrivetURLFetcher::ErrorType error) OVERRIDE; 135 virtual void OnParsedJson(PrivetURLFetcher* fetcher, 136 const base::DictionaryValue* value, 137 bool has_error) OVERRIDE; 138 virtual void OnNeedPrivetToken( 139 PrivetURLFetcher* fetcher, 140 const PrivetURLFetcher::TokenCallback& callback) OVERRIDE; 141 142 private: 143 PrivetHTTPClient* privet_client_; 144 std::string path_; 145 std::string query_params_; 146 PrivetJSONOperation::ResultCallback callback_; 147 148 scoped_ptr<PrivetURLFetcher> url_fetcher_; 149 }; 150 151 class PrivetDataReadOperationImpl : public PrivetDataReadOperation, 152 public PrivetURLFetcher::Delegate { 153 public: 154 PrivetDataReadOperationImpl( 155 PrivetHTTPClient* privet_client, 156 const std::string& path, 157 const std::string& query_params, 158 const PrivetDataReadOperation::ResultCallback& callback); 159 virtual ~PrivetDataReadOperationImpl(); 160 161 virtual void Start() OVERRIDE; 162 163 virtual void SetDataRange(int range_start, int range_end) OVERRIDE; 164 165 virtual void SaveDataToFile() OVERRIDE; 166 167 virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE; 168 169 virtual void OnError(PrivetURLFetcher* fetcher, 170 PrivetURLFetcher::ErrorType error) OVERRIDE; 171 virtual void OnParsedJson(PrivetURLFetcher* fetcher, 172 const base::DictionaryValue* value, 173 bool has_error) OVERRIDE; 174 virtual void OnNeedPrivetToken( 175 PrivetURLFetcher* fetcher, 176 const PrivetURLFetcher::TokenCallback& callback) OVERRIDE; 177 virtual bool OnRawData(PrivetURLFetcher* fetcher, 178 bool is_file, 179 const std::string& data_str, 180 const base::FilePath& file_path) OVERRIDE; 181 182 private: 183 PrivetHTTPClient* privet_client_; 184 std::string path_; 185 std::string query_params_; 186 int range_start_; 187 int range_end_; 188 PrivetDataReadOperation::ResultCallback callback_; 189 190 bool has_range_; 191 bool save_to_file_; 192 193 scoped_ptr<PrivetURLFetcher> url_fetcher_; 194 }; 195 196 class PrivetLocalPrintOperationImpl 197 : public PrivetLocalPrintOperation, 198 public PrivetURLFetcher::Delegate { 199 public: 200 PrivetLocalPrintOperationImpl(PrivetHTTPClient* privet_client, 201 PrivetLocalPrintOperation::Delegate* delegate); 202 203 virtual ~PrivetLocalPrintOperationImpl(); 204 virtual void Start() OVERRIDE; 205 206 virtual void SetData(base::RefCountedBytes* data) OVERRIDE; 207 208 virtual void SetCapabilities(const std::string& capabilities) OVERRIDE; 209 210 virtual void SetTicket(const std::string& ticket) OVERRIDE; 211 212 virtual void SetUsername(const std::string& user) OVERRIDE; 213 214 virtual void SetJobname(const std::string& jobname) OVERRIDE; 215 216 virtual void SetOffline(bool offline) OVERRIDE; 217 218 virtual void SetPageSize(const gfx::Size& page_size) OVERRIDE; 219 220 virtual void SetPWGRasterConverterForTesting( 221 scoped_ptr<PWGRasterConverter> pwg_raster_converter) OVERRIDE; 222 223 virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE; 224 225 virtual void OnError(PrivetURLFetcher* fetcher, 226 PrivetURLFetcher::ErrorType error) OVERRIDE; 227 virtual void OnParsedJson(PrivetURLFetcher* fetcher, 228 const base::DictionaryValue* value, 229 bool has_error) OVERRIDE; 230 virtual void OnNeedPrivetToken( 231 PrivetURLFetcher* fetcher, 232 const PrivetURLFetcher::TokenCallback& callback) OVERRIDE; 233 234 private: 235 typedef base::Callback<void(bool, const base::DictionaryValue* value)> 236 ResponseCallback; 237 238 void StartInitialRequest(); 239 void DoCreatejob(); 240 void DoSubmitdoc(); 241 242 void StartConvertToPWG(); 243 void StartPrinting(); 244 245 void OnPrivetInfoDone(const base::DictionaryValue* value); 246 void OnSubmitdocResponse(bool has_error, 247 const base::DictionaryValue* value); 248 void OnCreatejobResponse(bool has_error, 249 const base::DictionaryValue* value); 250 void OnPWGRasterConverted(bool success, const base::FilePath& pwg_file_path); 251 void FillPwgRasterSettings(printing::PwgRasterSettings* transfrom_settings); 252 253 PrivetHTTPClient* privet_client_; 254 PrivetLocalPrintOperation::Delegate* delegate_; 255 256 ResponseCallback current_response_; 257 258 cloud_devices::CloudDeviceDescription ticket_; 259 cloud_devices::CloudDeviceDescription capabilities_; 260 261 scoped_refptr<base::RefCountedBytes> data_; 262 base::FilePath pwg_file_path_; 263 264 bool use_pdf_; 265 bool has_extended_workflow_; 266 bool started_; 267 bool offline_; 268 gfx::Size page_size_; 269 int dpi_; 270 271 std::string user_; 272 std::string jobname_; 273 274 std::string jobid_; 275 276 int invalid_job_retries_; 277 278 scoped_ptr<PrivetURLFetcher> url_fetcher_; 279 scoped_ptr<PrivetJSONOperation> info_operation_; 280 scoped_ptr<PWGRasterConverter> pwg_raster_converter_; 281 282 base::WeakPtrFactory<PrivetLocalPrintOperationImpl> weak_factory_; 283 }; 284 285 class PrivetHTTPClientImpl : public PrivetHTTPClient { 286 public: 287 PrivetHTTPClientImpl( 288 const std::string& name, 289 const net::HostPortPair& host_port, 290 net::URLRequestContextGetter* request_context); 291 virtual ~PrivetHTTPClientImpl(); 292 293 // PrivetHTTPClient implementation. 294 virtual const std::string& GetName() OVERRIDE; 295 virtual scoped_ptr<PrivetJSONOperation> CreateInfoOperation( 296 const PrivetJSONOperation::ResultCallback& callback) OVERRIDE; 297 virtual scoped_ptr<PrivetURLFetcher> CreateURLFetcher( 298 const GURL& url, 299 net::URLFetcher::RequestType request_type, 300 PrivetURLFetcher::Delegate* delegate) OVERRIDE; 301 virtual void RefreshPrivetToken( 302 const PrivetURLFetcher::TokenCallback& token_callback) OVERRIDE; 303 304 private: 305 typedef std::vector<PrivetURLFetcher::TokenCallback> TokenCallbackVector; 306 307 void OnPrivetInfoDone(const base::DictionaryValue* value); 308 309 std::string name_; 310 scoped_refptr<net::URLRequestContextGetter> request_context_; 311 net::HostPortPair host_port_; 312 313 scoped_ptr<PrivetJSONOperation> info_operation_; 314 TokenCallbackVector token_callbacks_; 315 316 DISALLOW_COPY_AND_ASSIGN(PrivetHTTPClientImpl); 317 }; 318 319 class PrivetV1HTTPClientImpl : public PrivetV1HTTPClient { 320 public: 321 explicit PrivetV1HTTPClientImpl(scoped_ptr<PrivetHTTPClient> info_client); 322 virtual ~PrivetV1HTTPClientImpl(); 323 324 virtual const std::string& GetName() OVERRIDE; 325 virtual scoped_ptr<PrivetJSONOperation> CreateInfoOperation( 326 const PrivetJSONOperation::ResultCallback& callback) OVERRIDE; 327 virtual scoped_ptr<PrivetRegisterOperation> CreateRegisterOperation( 328 const std::string& user, 329 PrivetRegisterOperation::Delegate* delegate) OVERRIDE; 330 virtual scoped_ptr<PrivetJSONOperation> CreateCapabilitiesOperation( 331 const PrivetJSONOperation::ResultCallback& callback) OVERRIDE; 332 virtual scoped_ptr<PrivetLocalPrintOperation> CreateLocalPrintOperation( 333 PrivetLocalPrintOperation::Delegate* delegate) OVERRIDE; 334 virtual scoped_ptr<PrivetJSONOperation> CreateStorageListOperation( 335 const std::string& path, 336 const PrivetJSONOperation::ResultCallback& callback) OVERRIDE; 337 virtual scoped_ptr<PrivetDataReadOperation> CreateStorageReadOperation( 338 const std::string& path, 339 const PrivetDataReadOperation::ResultCallback& callback) OVERRIDE; 340 341 private: info_client()342 PrivetHTTPClient* info_client() { return info_client_.get(); } 343 344 scoped_ptr<PrivetHTTPClient> info_client_; 345 346 DISALLOW_COPY_AND_ASSIGN(PrivetV1HTTPClientImpl); 347 }; 348 349 class PrivetV3HTTPClientImpl : public PrivetV3HTTPClient { 350 public: 351 explicit PrivetV3HTTPClientImpl(scoped_ptr<PrivetHTTPClient> info_client); 352 virtual ~PrivetV3HTTPClientImpl(); 353 354 virtual const std::string& GetName() OVERRIDE; 355 virtual scoped_ptr<PrivetJSONOperation> CreateInfoOperation( 356 const PrivetJSONOperation::ResultCallback& callback) OVERRIDE; 357 358 private: info_client()359 PrivetHTTPClient* info_client() { return info_client_.get(); } 360 361 scoped_ptr<PrivetHTTPClient> info_client_; 362 363 DISALLOW_COPY_AND_ASSIGN(PrivetV3HTTPClientImpl); 364 }; 365 366 } // namespace local_discovery 367 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_IMPL_H_ 368