Home | History | Annotate | Download | only in local_discovery
      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 "chrome/browser/local_discovery/pwg_raster_converter.h"
     13 #include "net/base/host_port_pair.h"
     14 
     15 namespace base {
     16 class RefCountedBytes;
     17 }
     18 
     19 namespace gfx {
     20 class Size;
     21 }
     22 
     23 namespace printing {
     24 class PdfRenderSettings;
     25 }
     26 
     27 namespace local_discovery {
     28 
     29 class PrivetHTTPClient;
     30 
     31 // Represents a request to /privet/info. Will store a cached response and token
     32 // in the PrivetHTTPClient that created.
     33 class PrivetInfoOperation {
     34  public:
     35   class Delegate {
     36    public:
     37     virtual ~Delegate() {}
     38 
     39     // In case of non-HTTP errors, |http_code| will be -1.
     40 
     41     // TODO(noamsml): Remove http_code from this delegate; it's unnecessary in
     42     // practice
     43     virtual void OnPrivetInfoDone(
     44         PrivetInfoOperation* operation,
     45         int http_code,
     46         const base::DictionaryValue* json_value) = 0;
     47   };
     48 
     49   virtual ~PrivetInfoOperation() {}
     50 
     51   virtual void Start() = 0;
     52 
     53   virtual PrivetHTTPClient* GetHTTPClient() = 0;
     54 };
     55 
     56 // Represents a full registration flow (/privet/register), normally consisting
     57 // of calling the start action, the getClaimToken action, and calling the
     58 // complete action. Some intervention from the caller is required to display the
     59 // claim URL to the user (noted in OnPrivetRegisterClaimURL).
     60 class PrivetRegisterOperation {
     61  public:
     62   enum FailureReason {
     63     FAILURE_NETWORK,
     64     FAILURE_HTTP_ERROR,
     65     FAILURE_JSON_ERROR,
     66     FAILURE_MALFORMED_RESPONSE,
     67     FAILURE_TOKEN,
     68     FAILURE_RETRY
     69   };
     70 
     71   class Delegate {
     72    public:
     73     ~Delegate() {}
     74 
     75     // Called when a user needs to claim the printer by visiting the given URL.
     76     virtual void OnPrivetRegisterClaimToken(
     77         PrivetRegisterOperation* operation,
     78         const std::string& token,
     79         const GURL& url) = 0;
     80 
     81     // TODO(noamsml): Remove all unnecessary parameters.
     82     // Called in case of an error while registering.  |action| is the
     83     // registration action taken during the error. |reason| is the reason for
     84     // the failure. |printer_http_code| is the http code returned from the
     85     // printer. If it is -1, an internal error occurred while trying to complete
     86     // the request. |json| may be null if printer_http_code signifies an error.
     87     virtual void OnPrivetRegisterError(PrivetRegisterOperation* operation,
     88                                        const std::string& action,
     89                                        FailureReason reason,
     90                                        int printer_http_code,
     91                                        const DictionaryValue* json) = 0;
     92 
     93     // Called when the registration is done.
     94     virtual void OnPrivetRegisterDone(PrivetRegisterOperation* operation,
     95                                       const std::string& device_id) = 0;
     96   };
     97 
     98   virtual ~PrivetRegisterOperation() {}
     99 
    100   virtual void Start() = 0;
    101   // Owner SHOULD call explicitly before destroying operation.
    102   virtual void Cancel() = 0;
    103   virtual void CompleteRegistration() = 0;
    104 
    105   virtual PrivetHTTPClient* GetHTTPClient() = 0;
    106 };
    107 
    108 class PrivetCapabilitiesOperation {
    109  public:
    110   class Delegate {
    111    public:
    112     virtual ~Delegate() {}
    113 
    114     // |capabilities| will be NULL in case of an error.
    115     virtual void OnPrivetCapabilities(
    116         PrivetCapabilitiesOperation* capabilities_operation,
    117         int http_error,
    118         const base::DictionaryValue* capabilities) = 0;
    119   };
    120 
    121   virtual ~PrivetCapabilitiesOperation() {}
    122   virtual void Start() = 0;
    123 
    124   virtual PrivetHTTPClient* GetHTTPClient() = 0;
    125 };
    126 
    127 class PrivetLocalPrintOperation {
    128  public:
    129   class Delegate {
    130    public:
    131     virtual ~Delegate() {}
    132     virtual void OnPrivetPrintingDone(
    133         const PrivetLocalPrintOperation* print_operation) = 0;
    134     virtual void OnPrivetPrintingError(
    135         const PrivetLocalPrintOperation* print_operation, int http_code) = 0;
    136   };
    137 
    138   virtual ~PrivetLocalPrintOperation() {}
    139 
    140   virtual void Start() = 0;
    141 
    142 
    143   // Required print data. MUST be called before calling |Start()|.
    144   virtual void SetData(base::RefCountedBytes* data) = 0;
    145 
    146   // Optional attributes for /submitdoc. Call before calling |Start()|
    147   // |ticket| should be in CJT format.
    148   virtual void SetTicket(const std::string& ticket) = 0;
    149   // Username and jobname are for display only.
    150   virtual void SetUsername(const std::string& username) = 0;
    151   virtual void SetJobname(const std::string& jobname) = 0;
    152   // If |offline| is true, we will indicate to the printer not to post the job
    153   // to Google Cloud Print.
    154   virtual void SetOffline(bool offline) = 0;
    155   // Document page size.
    156   virtual void SetConversionSettings(
    157       const printing::PdfRenderSettings& conversion_settings) = 0;
    158 
    159   // For testing, inject an alternative PWG raster converter.
    160   virtual void SetPWGRasterConverterForTesting(
    161       scoped_ptr<PWGRasterConverter> pwg_raster_converter) = 0;
    162 
    163   virtual PrivetHTTPClient* GetHTTPClient() = 0;
    164 };
    165 
    166 // Privet HTTP client. Must not outlive the operations it creates.
    167 class PrivetHTTPClient {
    168  public:
    169   virtual ~PrivetHTTPClient() {}
    170   virtual const base::DictionaryValue* GetCachedInfo() const = 0;
    171 
    172   virtual scoped_ptr<PrivetRegisterOperation> CreateRegisterOperation(
    173       const std::string& user,
    174       PrivetRegisterOperation::Delegate* delegate) = 0;
    175   virtual scoped_ptr<PrivetInfoOperation> CreateInfoOperation(
    176       PrivetInfoOperation::Delegate* delegate) = 0;
    177   virtual scoped_ptr<PrivetCapabilitiesOperation> CreateCapabilitiesOperation(
    178       PrivetCapabilitiesOperation::Delegate* delegate) = 0;
    179   virtual scoped_ptr<PrivetLocalPrintOperation> CreateLocalPrintOperation(
    180       PrivetLocalPrintOperation::Delegate* delegate) = 0;
    181 
    182   // A name for the HTTP client, e.g. the device name for the privet device.
    183   virtual const std::string& GetName() = 0;
    184 };
    185 
    186 }  // namespace local_discovery
    187 #endif  // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_HTTP_H_
    188