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