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.
5 #ifndef CLOUD_PRINT_GCP20_PROTOTYPE_CLOUD_PRINT_XMPP_LISTENER_H_
6 #define CLOUD_PRINT_GCP20_PROTOTYPE_CLOUD_PRINT_XMPP_LISTENER_H_
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"
19 class SingleThreadTaskRunner;
25 class URLRequestContextGetter;
33 } // namespace notifier
35 class CloudPrintXmppListener
36 : public base::SupportsWeakPtr<CloudPrintXmppListener>,
37 public notifier::PushClientObserver {
41 virtual ~Delegate() {}
43 // Invoked when XMPP connection was established.
44 virtual void OnXmppConnected() = 0;
46 // Invoked when server rejected our credentials.
47 virtual void OnXmppAuthError() = 0;
49 // Invoked when server is unavailable.
50 virtual void OnXmppNetworkError() = 0;
52 // Invoked when new printjob was received.
53 virtual void OnXmppNewPrintJob(const std::string& device_id) = 0;
55 // Invoked when local settings was updated.
56 virtual void OnXmppNewLocalSettings(const std::string& device_id) = 0;
58 // Invoked when printer was deleted from the server.
59 virtual void OnXmppDeleteNotification(const std::string& device_id) = 0;
62 CloudPrintXmppListener(
63 const std::string& robot_email,
64 int standard_ping_interval,
65 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
68 virtual ~CloudPrintXmppListener();
70 // Connects to the server.
71 void Connect(const std::string& access_token);
73 // Update ping interval when new local_settings was received.
74 void set_ping_interval(int interval);
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;
85 // Stops listening and sending pings.
88 // Schedules ping (unless it was already scheduled).
94 // Checks if ping was received.
95 void OnPingTimeoutReached();
98 std::string robot_email_;
99 std::string access_token_;
101 // Internal listener.
102 scoped_ptr<notifier::PushClient> push_client_;
104 // Interval between pings in regular workflow.
105 int standard_ping_interval_;
107 // Number of timeouts posted to MessageLoop. Is used for controlling "fake"
109 int ping_timeouts_posted_;
111 // Number of responses awaiting from XMPP server. Is used for controlling
112 // number of failed pings.
113 int ping_responses_pending_;
115 // Is used for preventing multiple pings at the moment.
116 bool ping_scheduled_;
118 scoped_refptr<net::URLRequestContextGetter> context_getter_;
122 DISALLOW_COPY_AND_ASSIGN(CloudPrintXmppListener);
125 #endif // CLOUD_PRINT_GCP20_PROTOTYPE_CLOUD_PRINT_XMPP_LISTENER_H_