Home | History | Annotate | Download | only in prototype
      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 GCP20_PROTOTYPE_PRINTER_H_
      6 #define GCP20_PROTOTYPE_PRINTER_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/files/file_path.h"
     12 #include "base/memory/weak_ptr.h"
     13 #include "cloud_print/gcp20/prototype/cloud_print_requester.h"
     14 #include "cloud_print/gcp20/prototype/cloud_print_xmpp_listener.h"
     15 #include "cloud_print/gcp20/prototype/dns_sd_server.h"
     16 #include "cloud_print/gcp20/prototype/print_job_handler.h"
     17 #include "cloud_print/gcp20/prototype/privet_http_server.h"
     18 #include "cloud_print/gcp20/prototype/x_privet_token.h"
     19 
     20 extern const base::FilePath::CharType kPrinterStatePath[];
     21 
     22 // This class maintains work of DNS-SD server, HTTP server and others.
     23 class Printer : public base::SupportsWeakPtr<Printer>,
     24                 public PrivetHttpServer::Delegate,
     25                 public CloudPrintRequester::Delegate,
     26                 public CloudPrintXmppListener::Delegate {
     27  public:
     28   // Constructs uninitialized object.
     29   Printer();
     30 
     31   // Destroys the object.
     32   virtual ~Printer();
     33 
     34   // Starts all servers.
     35   bool Start();
     36 
     37   // Returns true if printer was started.
     38   bool IsRunning() const;
     39 
     40   // Stops all servers.
     41   void Stop();
     42 
     43  private:
     44   struct RegistrationInfo {
     45     enum RegistrationState {
     46       DEV_REG_UNREGISTERED,
     47       DEV_REG_REGISTRATION_STARTED,  // |action=start| was called,
     48                                      // request to CloudPrint was sent.
     49       DEV_REG_REGISTRATION_CLAIM_TOKEN_READY,  // The same as previous,
     50                                                // but request reply is already
     51                                                // received.
     52       DEV_REG_REGISTRATION_COMPLETING,  // |action=complete| was called,
     53                                         // |complete| request was sent.
     54       DEV_REG_REGISTRATION_ERROR,  // Is set when server error was occurred.
     55       DEV_REG_REGISTERED,
     56     };
     57 
     58     enum ConfirmationState {
     59       CONFIRMATION_PENDING,
     60       CONFIRMATION_CONFIRMED,
     61       CONFIRMATION_DISCARDED,
     62       CONFIRMATION_TIMEOUT,
     63     };
     64 
     65     RegistrationInfo();
     66     ~RegistrationInfo();
     67 
     68     std::string user;
     69     std::string refresh_token;
     70     std::string device_id;
     71     std::string xmpp_jid;
     72     RegistrationState state;
     73     ConfirmationState confirmation_state;
     74 
     75     std::string registration_token;
     76     std::string complete_invite_url;
     77 
     78     // Contains error response if |DEV_REG_REGISTRATION_ERROR| is set.
     79     std::string error_description;
     80   };
     81 
     82   enum RegistrationAction {
     83     REG_ACTION_START,
     84     REG_ACTION_GET_CLAIM_TOKEN,
     85     REG_ACTION_COMPLETE,
     86     REG_ACTION_CANCEL
     87   };
     88 
     89   enum ConnectionState {
     90     NOT_CONFIGURED,
     91     OFFLINE,
     92     ONLINE,
     93     CONNECTING
     94   };
     95 
     96   // PrivetHttpServer::Delegate methods:
     97   virtual PrivetHttpServer::RegistrationErrorStatus RegistrationStart(
     98       const std::string& user) OVERRIDE;
     99   virtual PrivetHttpServer::RegistrationErrorStatus RegistrationGetClaimToken(
    100       const std::string& user,
    101       std::string* token,
    102       std::string* claim_url) OVERRIDE;
    103   virtual PrivetHttpServer::RegistrationErrorStatus RegistrationComplete(
    104       const std::string& user,
    105       std::string* device_id) OVERRIDE;
    106   virtual PrivetHttpServer::RegistrationErrorStatus RegistrationCancel(
    107       const std::string& user) OVERRIDE;
    108   virtual void GetRegistrationServerError(std::string* description) OVERRIDE;
    109   virtual void CreateInfo(PrivetHttpServer::DeviceInfo* info) OVERRIDE;
    110   virtual bool IsRegistered() const OVERRIDE;
    111   virtual bool CheckXPrivetTokenHeader(const std::string& token) const OVERRIDE;
    112 
    113   // CloudRequester::Delegate methods:
    114   virtual void OnRegistrationStartResponseParsed(
    115       const std::string& registration_token,
    116       const std::string& complete_invite_url,
    117       const std::string& device_id) OVERRIDE;
    118   virtual void OnGetAuthCodeResponseParsed(
    119       const std::string& refresh_token,
    120       const std::string& access_token,
    121       int access_token_expires_in_seconds) OVERRIDE;
    122   virtual void OnXmppJidReceived(const std::string& xmpp_jid) OVERRIDE;
    123   virtual void OnAccesstokenReceviced(const std::string& access_token,
    124                                       int expires_in_seconds) OVERRIDE;
    125   virtual void OnRegistrationError(const std::string& description) OVERRIDE;
    126   virtual void OnNetworkError() OVERRIDE;
    127   virtual void OnServerError(const std::string& description) OVERRIDE;
    128   virtual void OnAuthError() OVERRIDE;
    129   virtual std::string GetAccessToken() OVERRIDE;
    130   virtual void OnPrintJobsAvailable(
    131       const std::vector<cloud_print_response_parser::Job>& jobs) OVERRIDE;
    132   virtual void OnPrintJobDownloaded(
    133       const cloud_print_response_parser::Job& job) OVERRIDE;
    134   virtual void OnPrintJobDone() OVERRIDE;
    135 
    136   // CloudPrintXmppListener::Delegate methods:
    137   virtual void OnXmppConnected() OVERRIDE;
    138   virtual void OnXmppAuthError() OVERRIDE;
    139   virtual void OnXmppNetworkError() OVERRIDE;
    140   virtual void OnXmppNewPrintJob(const std::string& device_id) OVERRIDE;
    141   virtual void OnXmppNewLocalSettings(const std::string& device_id) OVERRIDE;
    142   virtual void OnXmppDeleteNotification(const std::string& device_id) OVERRIDE;
    143 
    144   // Method for trying to reconnecting to server on start or after network fail.
    145   void TryConnect();
    146 
    147   // Connects XMPP.
    148   void ConnectXmpp();
    149 
    150   // Method to handle pending events.
    151   // Do *NOT* call this method instantly. Only with |PostOnIdle|.
    152   void OnIdle();
    153 
    154   // Method for checking printer status.
    155   // (e.g. printjobs, local settings, deleted status).
    156   void CheckPendingUpdates();
    157 
    158   // Ask CloudPrint server for new local sendings.
    159   void GetLocalSettings();
    160 
    161   // Ask CloudPrint server for printjobs.
    162   void FetchPrintJobs();
    163 
    164   // Saves |access_token| and calculates time for next update.
    165   void RememberAccessToken(const std::string& access_token,
    166                            int expires_in_seconds);
    167 
    168   // Checks if register call is called correctly (|user| is correct,
    169   // error is no set etc). Returns |false| if error status is put into |status|.
    170   // Otherwise no error was occurred.
    171   PrivetHttpServer::RegistrationErrorStatus CheckCommonRegErrors(
    172       const std::string& user) const;
    173 
    174   // Checks if confirmation was received.
    175   void WaitUserConfirmation(base::Time valid_until);
    176 
    177   // Generates ProxyId for this device.
    178   std::string GenerateProxyId() const;
    179 
    180   // Creates data for DNS TXT respond.
    181   std::vector<std::string> CreateTxt() const;
    182 
    183   // Saving and loading registration info from file.
    184   void SaveToFile() const;
    185   bool LoadFromFile();
    186 
    187   // Adds |OnIdle| method to the MessageLoop.
    188   void PostOnIdle();
    189 
    190   // Converts errors.
    191   PrivetHttpServer::RegistrationErrorStatus ConfirmationToRegistrationError(
    192       RegistrationInfo::ConfirmationState state);
    193 
    194   std::string ConnectionStateToString(ConnectionState state) const;
    195 
    196   // Changes state and update info in DNS server.
    197   bool ChangeState(ConnectionState new_state);
    198 
    199   RegistrationInfo reg_info_;
    200 
    201   // Contains DNS-SD server.
    202   DnsSdServer dns_server_;
    203 
    204   // Contains Privet HTTP server.
    205   PrivetHttpServer http_server_;
    206 
    207   // Connection state of device.
    208   ConnectionState connection_state_;
    209 
    210   // Contains CloudPrint client.
    211   scoped_ptr<CloudPrintRequester> requester_;
    212 
    213   // XMPP Listener.
    214   scoped_ptr<CloudPrintXmppListener> xmpp_listener_;
    215 
    216   XPrivetToken xtoken_;
    217 
    218   scoped_ptr<PrintJobHandler> print_job_handler_;
    219 
    220   // Last valid |access_token|.
    221   std::string access_token_;
    222   base::Time access_token_update_;
    223 
    224   // Uses for calculating uptime.
    225   base::Time starttime_;
    226 
    227   // Used for preventing two and more OnIdle posted in message loop.
    228   bool on_idle_posted_;
    229 
    230   // Contains |true| if Printer has to check pending local settings.
    231   bool pending_local_settings_check_;
    232 
    233   // Contains |true| if Printer has to check available printjobs.
    234   bool pending_print_jobs_check_;
    235 
    236   DISALLOW_COPY_AND_ASSIGN(Printer);
    237 };
    238 
    239 #endif  // GCP20_PROTOTYPE_PRINTER_H_
    240 
    241