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_H_ 6 #define CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_H_ 7 8 #include <string> 9 10 #include "base/callback.h" 11 #include "chrome/browser/local_discovery/privet_url_fetcher.h" 12 #include "net/base/host_port_pair.h" 13 14 namespace local_discovery { 15 16 class PrivetHTTPImpl; 17 18 // Represents a request to /privet/info. Will store a cached response and token 19 // in the PrivetHTTPClient that created. 20 class PrivetInfoOperation { 21 public: 22 class Delegate { 23 public: 24 virtual ~Delegate() {} 25 26 // In case of non-HTTP errors, |http_code| will be -1. 27 virtual void OnPrivetInfoDone(int http_code, 28 const base::DictionaryValue* json_value) = 0; 29 }; 30 31 virtual ~PrivetInfoOperation() {} 32 33 virtual void Start() = 0; 34 }; 35 36 // Represents a full registration flow (/privet/register), normally consisting 37 // of calling the start action, the getClaimToken action, and calling the 38 // complete action. Some intervention from the caller is required to display the 39 // claim URL to the user (noted in OnPrivetRegisterClaimURL). 40 class PrivetRegisterOperation { 41 public: 42 enum FailureReason { 43 FAILURE_NETWORK, 44 FAILURE_HTTP_ERROR, 45 FAILURE_JSON_ERROR, 46 FAILURE_MALFORMED_RESPONSE 47 }; 48 49 class Delegate { 50 public: 51 ~Delegate() {} 52 53 // Called when a user needs to claim the printer by visiting the given URL. 54 virtual void OnPrivetRegisterClaimToken(const std::string& token, 55 const GURL& url) = 0; 56 57 // Called in case of an error while registering. |action| is the 58 // registration action taken during the error. |reason| is the reason for 59 // the failure. |printer_http_code| is the http code returned from the 60 // printer. If it is -1, an internal error occurred while trying to complete 61 // the request. |json| may be null if printer_http_code signifies an error. 62 virtual void OnPrivetRegisterError(const std::string& action, 63 FailureReason reason, 64 int printer_http_code, 65 const DictionaryValue* json) = 0; 66 67 // Called when the registration is done. 68 virtual void OnPrivetRegisterDone(const std::string& device_id) = 0; 69 }; 70 71 virtual ~PrivetRegisterOperation() {} 72 73 virtual void Start() = 0; 74 // Owner SHOULD call explicitly before destroying operation. 75 virtual void Cancel() = 0; 76 virtual void CompleteRegistration() = 0; 77 }; 78 79 // Privet HTTP client. Must not outlive the operations it creates. 80 class PrivetHTTPClient { 81 public: 82 virtual ~PrivetHTTPClient() {} 83 virtual const base::DictionaryValue* GetCachedInfo() const = 0; 84 85 virtual scoped_ptr<PrivetRegisterOperation> CreateRegisterOperation( 86 const std::string& user, 87 PrivetRegisterOperation::Delegate* delegate) = 0; 88 virtual scoped_ptr<PrivetInfoOperation> CreateInfoOperation( 89 PrivetInfoOperation::Delegate* delegate) = 0; 90 }; 91 92 } // namespace local_discovery 93 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_H_ 94