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 CLOUD_PRINT_GCP20_PROTOTYPE_CLOUD_PRINT_XMPP_LISTENER_H_
      6 #define CLOUD_PRINT_GCP20_PROTOTYPE_CLOUD_PRINT_XMPP_LISTENER_H_
      7 
      8 #include <string>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/compiler_specific.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "base/memory/weak_ptr.h"
     15 #include "jingle/notifier/listener/push_client_observer.h"
     16 
     17 namespace base {
     18 
     19 class SingleThreadTaskRunner;
     20 
     21 }  // namespace base
     22 
     23 namespace net {
     24 
     25 class URLRequestContextGetter;
     26 
     27 }  // namespace net
     28 
     29 namespace notifier {
     30 
     31 class PushClient;
     32 
     33 }  // namespace notifier
     34 
     35 class CloudPrintXmppListener
     36     : public base::SupportsWeakPtr<CloudPrintXmppListener>,
     37       public notifier::PushClientObserver {
     38  public:
     39   class Delegate {
     40    public:
     41     virtual ~Delegate() {}
     42 
     43     // Invoked when XMPP connection was established.
     44     virtual void OnXmppConnected() = 0;
     45 
     46     // Invoked when server rejected our credentials.
     47     virtual void OnXmppAuthError() = 0;
     48 
     49     // Invoked when server is unavailable.
     50     virtual void OnXmppNetworkError() = 0;
     51 
     52     // Invoked when new printjob was received.
     53     virtual void OnXmppNewPrintJob(const std::string& device_id) = 0;
     54 
     55     // Invoked when local settings was updated.
     56     virtual void OnXmppNewLocalSettings(const std::string& device_id) = 0;
     57 
     58     // Invoked when printer was deleted from the server.
     59     virtual void OnXmppDeleteNotification(const std::string& device_id) = 0;
     60   };
     61 
     62   CloudPrintXmppListener(
     63       const std::string& robot_email,
     64       int standard_ping_interval,
     65       scoped_refptr<base::SingleThreadTaskRunner> task_runner,
     66       Delegate* delegate);
     67 
     68   virtual ~CloudPrintXmppListener();
     69 
     70   // Connects to the server.
     71   void Connect(const std::string& access_token);
     72 
     73   // Update ping interval when new local_settings was received.
     74   void set_ping_interval(int interval);
     75 
     76  private:
     77   // notifier::PushClientObserver methods:
     78   virtual void OnNotificationsEnabled() OVERRIDE;
     79   virtual void OnNotificationsDisabled(
     80       notifier::NotificationsDisabledReason reason) OVERRIDE;
     81   virtual void OnIncomingNotification(
     82       const notifier::Notification& notification) OVERRIDE;
     83   virtual void OnPingResponse() OVERRIDE;
     84 
     85   // Stops listening and sending pings.
     86   void Disconnect();
     87 
     88   // Schedules ping (unless it was already scheduled).
     89   void SchedulePing();
     90 
     91   // Sends ping.
     92   void SendPing();
     93 
     94   // Checks if ping was received.
     95   void OnPingTimeoutReached();
     96 
     97   // Credentials:
     98   std::string robot_email_;
     99   std::string access_token_;
    100 
    101   // Internal listener.
    102   scoped_ptr<notifier::PushClient> push_client_;
    103 
    104   // Interval between pings in regular workflow.
    105   int standard_ping_interval_;
    106 
    107   // Number of timeouts posted to MessageLoop. Is used for controlling "fake"
    108   // timeout calls.
    109   int ping_timeouts_posted_;
    110 
    111   // Number of responses awaiting from XMPP server. Is used for controlling
    112   // number of failed pings.
    113   int ping_responses_pending_;
    114 
    115   // Is used for preventing multiple pings at the moment.
    116   bool ping_scheduled_;
    117 
    118   scoped_refptr<net::URLRequestContextGetter> context_getter_;
    119 
    120   Delegate* delegate_;
    121 
    122   DISALLOW_COPY_AND_ASSIGN(CloudPrintXmppListener);
    123 };
    124 
    125 #endif  // CLOUD_PRINT_GCP20_PROTOTYPE_CLOUD_PRINT_XMPP_LISTENER_H_
    126 
    127