1 // Copyright (c) 2012 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 // A simple wrapper around invalidation::InvalidationClient that
6 // handles all the startup/shutdown details and hookups.
8 #ifndef SYNC_NOTIFIER_CHROME_INVALIDATION_CLIENT_H_
9 #define SYNC_NOTIFIER_CHROME_INVALIDATION_CLIENT_H_
13 #include "base/basictypes.h"
14 #include "base/callback_forward.h"
15 #include "base/compiler_specific.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/threading/non_thread_safe.h"
19 #include "google/cacheinvalidation/include/invalidation-listener.h"
20 #include "jingle/notifier/listener/push_client_observer.h"
21 #include "sync/internal_api/public/util/weak_handle.h"
22 #include "sync/notifier/chrome_system_resources.h"
23 #include "sync/notifier/invalidation_state_tracker.h"
24 #include "sync/notifier/notifications_disabled_reason.h"
25 #include "sync/notifier/object_id_payload_map.h"
26 #include "sync/notifier/state_writer.h"
29 class XmppTaskParentInterface
;
34 } // namespace notifier
38 class RegistrationManager
;
40 // ChromeInvalidationClient is not thread-safe and lives on the sync
42 class ChromeInvalidationClient
43 : public invalidation::InvalidationListener
,
45 public notifier::PushClientObserver
,
46 public base::NonThreadSafe
{
48 typedef base::Callback
<invalidation::InvalidationClient
*(
49 invalidation::SystemResources
*,
51 const invalidation::string
&,
52 const invalidation::string
&,
53 invalidation::InvalidationListener
*)> CreateInvalidationClientCallback
;
59 virtual void OnInvalidate(const ObjectIdPayloadMap
& id_payloads
) = 0;
61 virtual void OnNotificationsEnabled() = 0;
63 virtual void OnNotificationsDisabled(
64 NotificationsDisabledReason reason
) = 0;
67 explicit ChromeInvalidationClient(
68 scoped_ptr
<notifier::PushClient
> push_client
);
71 virtual ~ChromeInvalidationClient();
73 // Does not take ownership of |listener| or |state_writer|.
74 // |invalidation_state_tracker| must be initialized.
76 const CreateInvalidationClientCallback
&
77 create_invalidation_client_callback
,
78 const std::string
& client_id
, const std::string
& client_info
,
79 const std::string
& state
,
80 const InvalidationVersionMap
& initial_max_invalidation_versions
,
81 const WeakHandle
<InvalidationStateTracker
>& invalidation_state_tracker
,
84 void UpdateCredentials(const std::string
& email
, const std::string
& token
);
86 // Update the set of object IDs that we're interested in getting
87 // notifications for. May be called at any time.
88 void UpdateRegisteredIds(const ObjectIdSet
& ids
);
90 // invalidation::InvalidationListener implementation.
92 invalidation::InvalidationClient
* client
) OVERRIDE
;
93 virtual void Invalidate(
94 invalidation::InvalidationClient
* client
,
95 const invalidation::Invalidation
& invalidation
,
96 const invalidation::AckHandle
& ack_handle
) OVERRIDE
;
97 virtual void InvalidateUnknownVersion(
98 invalidation::InvalidationClient
* client
,
99 const invalidation::ObjectId
& object_id
,
100 const invalidation::AckHandle
& ack_handle
) OVERRIDE
;
101 virtual void InvalidateAll(
102 invalidation::InvalidationClient
* client
,
103 const invalidation::AckHandle
& ack_handle
) OVERRIDE
;
104 virtual void InformRegistrationStatus(
105 invalidation::InvalidationClient
* client
,
106 const invalidation::ObjectId
& object_id
,
107 invalidation::InvalidationListener::RegistrationState reg_state
) OVERRIDE
;
108 virtual void InformRegistrationFailure(
109 invalidation::InvalidationClient
* client
,
110 const invalidation::ObjectId
& object_id
,
112 const std::string
& error_message
) OVERRIDE
;
113 virtual void ReissueRegistrations(
114 invalidation::InvalidationClient
* client
,
115 const std::string
& prefix
,
116 int prefix_length
) OVERRIDE
;
117 virtual void InformError(
118 invalidation::InvalidationClient
* client
,
119 const invalidation::ErrorInfo
& error_info
) OVERRIDE
;
121 // StateWriter implementation.
122 virtual void WriteState(const std::string
& state
) OVERRIDE
;
124 // notifier::PushClientObserver implementation.
125 virtual void OnNotificationsEnabled() OVERRIDE
;
126 virtual void OnNotificationsDisabled(
127 notifier::NotificationsDisabledReason reason
) OVERRIDE
;
128 virtual void OnIncomingNotification(
129 const notifier::Notification
& notification
) OVERRIDE
;
136 NotificationsDisabledReason
GetState() const;
138 void EmitStateChange();
140 void EmitInvalidation(const ObjectIdPayloadMap
& id_payloads
);
142 // Owned by |chrome_system_resources_|.
143 notifier::PushClient
* const push_client_
;
144 ChromeSystemResources chrome_system_resources_
;
145 InvalidationVersionMap max_invalidation_versions_
;
146 WeakHandle
<InvalidationStateTracker
> invalidation_state_tracker_
;
148 scoped_ptr
<invalidation::InvalidationClient
> invalidation_client_
;
149 scoped_ptr
<RegistrationManager
> registration_manager_
;
150 // Stored to pass to |registration_manager_| on start.
151 ObjectIdSet registered_ids_
;
153 // The states of the ticl and the push client (with
154 // NO_NOTIFICATION_ERROR meaning notifications are enabled).
155 NotificationsDisabledReason ticl_state_
;
156 NotificationsDisabledReason push_client_state_
;
158 DISALLOW_COPY_AND_ASSIGN(ChromeInvalidationClient
);
161 } // namespace syncer
163 #endif // SYNC_NOTIFIER_CHROME_INVALIDATION_CLIENT_H_