privet_http_impl.h revision 1e9bf3e0803691d0a228da41fc608347b6db4340
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/memory/weak_ptr.h" 12#include "chrome/browser/local_discovery/privet_http.h" 13 14namespace local_discovery { 15 16class PrivetHTTPClientImpl; 17 18class PrivetInfoOperationImpl : public PrivetInfoOperation, 19 public PrivetURLFetcher::Delegate { 20 public: 21 PrivetInfoOperationImpl(PrivetHTTPClientImpl* privet_client, 22 PrivetInfoOperation::Delegate* delegate); 23 virtual ~PrivetInfoOperationImpl(); 24 25 virtual void Start() OVERRIDE; 26 27 virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE; 28 29 virtual void OnError(PrivetURLFetcher* fetcher, 30 PrivetURLFetcher::ErrorType error) OVERRIDE; 31 virtual void OnParsedJson(PrivetURLFetcher* fetcher, 32 const base::DictionaryValue* value, 33 bool has_error) OVERRIDE; 34 35 private: 36 PrivetHTTPClientImpl* privet_client_; 37 PrivetInfoOperation::Delegate* delegate_; 38 scoped_ptr<PrivetURLFetcher> url_fetcher_; 39}; 40 41class PrivetRegisterOperationImpl 42 : public PrivetRegisterOperation, 43 public PrivetURLFetcher::Delegate, 44 public PrivetInfoOperation::Delegate, 45 public base::SupportsWeakPtr<PrivetRegisterOperationImpl> { 46 public: 47 PrivetRegisterOperationImpl(PrivetHTTPClientImpl* privet_client, 48 const std::string& user, 49 PrivetRegisterOperation::Delegate* delegate); 50 virtual ~PrivetRegisterOperationImpl(); 51 52 virtual void Start() OVERRIDE; 53 virtual void Cancel() OVERRIDE; 54 virtual void CompleteRegistration() OVERRIDE; 55 56 virtual void OnError(PrivetURLFetcher* fetcher, 57 PrivetURLFetcher::ErrorType error) OVERRIDE; 58 59 virtual void OnParsedJson(PrivetURLFetcher* fetcher, 60 const base::DictionaryValue* value, 61 bool has_error) OVERRIDE; 62 63 virtual void OnPrivetInfoDone(PrivetInfoOperation* operation, 64 int http_code, 65 const base::DictionaryValue* value) OVERRIDE; 66 67 void GetTokenFromInfoCall(PrivetInfoOperation* operation, 68 int http_code, 69 const base::DictionaryValue* value); 70 71 void VerifyIDFromInfoCall(PrivetInfoOperation* operation, 72 int http_code, 73 const base::DictionaryValue* value); 74 75 virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE; 76 private: 77 class Cancelation : public PrivetURLFetcher::Delegate { 78 public: 79 Cancelation(PrivetHTTPClientImpl* privet_client, 80 const std::string& user); 81 virtual ~Cancelation(); 82 83 virtual void OnError(PrivetURLFetcher* fetcher, 84 PrivetURLFetcher::ErrorType error) OVERRIDE; 85 86 virtual void OnParsedJson(PrivetURLFetcher* fetcher, 87 const base::DictionaryValue* value, 88 bool has_error) OVERRIDE; 89 90 void Cleanup(); 91 92 private: 93 scoped_ptr<PrivetURLFetcher> url_fetcher_; 94 }; 95 96 // Arguments is JSON value from request. 97 typedef base::Callback<void(const base::DictionaryValue&)> 98 ResponseHandler; 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 StartInfoOperation(); 105 106 void SendRequest(const std::string& action); 107 108 std::string user_; 109 std::string current_action_; 110 scoped_ptr<PrivetURLFetcher> url_fetcher_; 111 PrivetRegisterOperation::Delegate* delegate_; 112 PrivetHTTPClientImpl* privet_client_; 113 ResponseHandler next_response_handler_; 114 // Required to ensure destroying completed register operations doesn't cause 115 // extraneous cancelations. 116 bool ongoing_; 117 // Whether the current /privet/info call is for the final confirmation or 118 // for initial token getting. 119 bool info_for_confirmation_; 120 scoped_ptr<PrivetInfoOperation> info_operation_; 121 std::string expected_id_; 122}; 123 124// TODO(noamsml): Factor out some of this code into a PrivetBaseOperation 125class PrivetCapabilitiesOperationImpl : public PrivetCapabilitiesOperation, 126 public PrivetURLFetcher::Delegate, 127 public PrivetInfoOperation::Delegate { 128 public: 129 PrivetCapabilitiesOperationImpl( 130 PrivetHTTPClientImpl* privet_client, 131 PrivetCapabilitiesOperation::Delegate* delegate); 132 virtual ~PrivetCapabilitiesOperationImpl(); 133 virtual void Start() OVERRIDE; 134 135 virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE; 136 137 virtual void OnError(PrivetURLFetcher* fetcher, 138 PrivetURLFetcher::ErrorType error) OVERRIDE; 139 virtual void OnParsedJson(PrivetURLFetcher* fetcher, 140 const base::DictionaryValue* value, 141 bool has_error) OVERRIDE; 142 143 virtual void OnPrivetInfoDone(PrivetInfoOperation* operation, 144 int http_code, 145 const base::DictionaryValue* value) OVERRIDE; 146 private: 147 void StartRequest(); 148 149 PrivetHTTPClientImpl* privet_client_; 150 PrivetCapabilitiesOperation::Delegate* delegate_; 151 152 scoped_ptr<PrivetURLFetcher> url_fetcher_; 153 scoped_ptr<PrivetInfoOperation> info_operation_; 154}; 155 156class PrivetLocalPrintOperationImpl 157 : public PrivetLocalPrintOperation, 158 public PrivetURLFetcher::Delegate, 159 public PrivetInfoOperation::Delegate { 160 public: 161 PrivetLocalPrintOperationImpl( 162 PrivetHTTPClientImpl* privet_client, 163 PrivetLocalPrintOperation::Delegate* delegate); 164 165 virtual ~PrivetLocalPrintOperationImpl(); 166 virtual void Start() OVERRIDE; 167 168 virtual void SendData(const std::string& data) OVERRIDE; 169 170 virtual void SetTicket(const std::string& ticket) OVERRIDE; 171 172 virtual void SetUsername(const std::string& user) OVERRIDE; 173 174 virtual void SetJobname(const std::string& jobname) OVERRIDE; 175 176 virtual void SetOffline(bool offline) OVERRIDE; 177 178 virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE; 179 180 virtual void OnError(PrivetURLFetcher* fetcher, 181 PrivetURLFetcher::ErrorType error) OVERRIDE; 182 virtual void OnParsedJson(PrivetURLFetcher* fetcher, 183 const base::DictionaryValue* value, 184 bool has_error) OVERRIDE; 185 186 virtual void OnPrivetInfoDone(PrivetInfoOperation* operation, 187 int http_code, 188 const base::DictionaryValue* value) OVERRIDE; 189 private: 190 typedef base::Callback<void(const base::DictionaryValue* value)> 191 ResponseCallback; 192 193 void StartCurrentRequest(); 194 195 void StartInitialRequest(); 196 void GetCapabilities(); 197 // TODO(noamsml): void DoCreatejob(); 198 void DoSubmitdoc(); 199 200 void OnCapabilities(const base::DictionaryValue* value); 201 void OnSubmitdocResponse(const base::DictionaryValue* value); 202 203 PrivetHTTPClientImpl* privet_client_; 204 PrivetLocalPrintOperation::Delegate* delegate_; 205 206 base::Closure current_request_; 207 ResponseCallback current_response_; 208 209 std::string ticket_; 210 std::string data_; 211 212 bool use_pdf_; 213 bool has_capabilities_; 214 bool has_extended_workflow_; 215 bool processed_api_list_; 216 bool started_; 217 bool offline_; 218 219 std::string user_; 220 std::string jobname_; 221 222 scoped_ptr<PrivetURLFetcher> url_fetcher_; 223 scoped_ptr<PrivetInfoOperation> info_operation_; 224}; 225 226class PrivetHTTPClientImpl : public PrivetHTTPClient { 227 public: 228 PrivetHTTPClientImpl( 229 const std::string& name, 230 const net::HostPortPair& host_port, 231 net::URLRequestContextGetter* request_context); 232 virtual ~PrivetHTTPClientImpl(); 233 234 virtual const base::DictionaryValue* GetCachedInfo() const OVERRIDE; 235 236 virtual scoped_ptr<PrivetRegisterOperation> CreateRegisterOperation( 237 const std::string& user, 238 PrivetRegisterOperation::Delegate* delegate) OVERRIDE; 239 240 virtual scoped_ptr<PrivetInfoOperation> CreateInfoOperation( 241 PrivetInfoOperation::Delegate* delegate) OVERRIDE; 242 243 virtual scoped_ptr<PrivetCapabilitiesOperation> CreateCapabilitiesOperation( 244 PrivetCapabilitiesOperation::Delegate* delegate) OVERRIDE; 245 246 virtual scoped_ptr<PrivetLocalPrintOperation> CreateLocalPrintOperation( 247 PrivetLocalPrintOperation::Delegate* delegate) OVERRIDE; 248 249 virtual const std::string& GetName() OVERRIDE; 250 251 scoped_ptr<PrivetURLFetcher> CreateURLFetcher( 252 const GURL& url, 253 net::URLFetcher::RequestType request_type, 254 PrivetURLFetcher::Delegate* delegate) const; 255 256 void CacheInfo(const base::DictionaryValue* cached_info); 257 258 bool HasToken() const; 259 260 private: 261 std::string name_; 262 PrivetURLFetcherFactory fetcher_factory_; 263 net::HostPortPair host_port_; 264 scoped_ptr<base::DictionaryValue> cached_info_; 265}; 266 267} // namespace local_discovery 268#endif // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_IMPL_H_ 269