• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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 // A simple wrapper around invalidation::InvalidationClient that
6 // handles all the startup/shutdown details and hookups.
7 
8 #ifndef CHROME_BROWSER_SYNC_NOTIFIER_CHROME_INVALIDATION_CLIENT_H_
9 #define CHROME_BROWSER_SYNC_NOTIFIER_CHROME_INVALIDATION_CLIENT_H_
10 #pragma once
11 
12 #include <map>
13 #include <string>
14 
15 #include "base/basictypes.h"
16 #include "base/compiler_specific.h"
17 #include "base/memory/scoped_callback_factory.h"
18 #include "base/memory/scoped_ptr.h"
19 #include "base/memory/weak_ptr.h"
20 #include "base/threading/non_thread_safe.h"
21 #include "chrome/browser/sync/notifier/chrome_system_resources.h"
22 #include "chrome/browser/sync/notifier/state_writer.h"
23 #include "chrome/browser/sync/syncable/model_type.h"
24 #include "chrome/browser/sync/syncable/model_type_payload_map.h"
25 #include "google/cacheinvalidation/invalidation-client.h"
26 
27 // TODO(akalin): Move invalidation::InvalidationListener into its own
28 // file and include that instead of invalidation-client.h (which
29 // includes generated protobuf header files).
30 
31 namespace talk_base {
32 class Task;
33 }  // namespace
34 
35 namespace sync_notifier {
36 
37 class CacheInvalidationPacketHandler;
38 class RegistrationManager;
39 
40 class ChromeInvalidationClient
41     : public invalidation::InvalidationListener,
42       public StateWriter {
43  public:
44   class Listener {
45    public:
46     virtual ~Listener();
47 
48     virtual void OnInvalidate(
49         const syncable::ModelTypePayloadMap& type_payloads) = 0;
50 
51     virtual void OnSessionStatusChanged(bool has_session) = 0;
52   };
53 
54   ChromeInvalidationClient();
55 
56   // Calls Stop().
57   virtual ~ChromeInvalidationClient();
58 
59   // Does not take ownership of |listener| or |state_writer|.
60   // |base_task| must still be non-NULL.
61   void Start(
62       const std::string& client_id, const std::string& client_info,
63       const std::string& state, Listener* listener,
64       StateWriter* state_writer, base::WeakPtr<talk_base::Task> base_task);
65 
66   void Stop();
67 
68   // Changes the task used to |base_task|, which must still be
69   // non-NULL.  Must only be called between calls to Start() and
70   // Stop().
71   void ChangeBaseTask(base::WeakPtr<talk_base::Task> base_task);
72 
73   // Register the sync types that we're interested in getting
74   // notifications for.  May be called at any time.
75   void RegisterTypes(const syncable::ModelTypeSet& types);
76 
77   // invalidation::InvalidationListener implementation.
78   virtual void Invalidate(const invalidation::Invalidation& invalidation,
79                           invalidation::Closure* callback) OVERRIDE;
80   virtual void InvalidateAll(invalidation::Closure* callback) OVERRIDE;
81   virtual void RegistrationStateChanged(
82       const invalidation::ObjectId& object_id,
83       invalidation::RegistrationState new_state,
84       const invalidation::UnknownHint& unknown_hint) OVERRIDE;
85   virtual void AllRegistrationsLost(invalidation::Closure* callback) OVERRIDE;
86   virtual void SessionStatusChanged(bool has_session) OVERRIDE;
87 
88   // StateWriter implementation.
89   virtual void WriteState(const std::string& state) OVERRIDE;
90 
91  private:
92   friend class ChromeInvalidationClientTest;
93 
94   // Should only be called between calls to Start() and Stop().
95   void HandleOutboundPacket(
96       invalidation::NetworkEndpoint* const& network_endpoint);
97   void EmitInvalidation(
98       const syncable::ModelTypeSet& types, const std::string& payload);
99 
100   base::NonThreadSafe non_thread_safe_;
101   ChromeSystemResources chrome_system_resources_;
102   base::ScopedCallbackFactory<ChromeInvalidationClient>
103       scoped_callback_factory_;
104   scoped_ptr<invalidation::NetworkCallback> handle_outbound_packet_callback_;
105   Listener* listener_;
106   StateWriter* state_writer_;
107   scoped_ptr<invalidation::InvalidationClient> invalidation_client_;
108   scoped_ptr<CacheInvalidationPacketHandler>
109       cache_invalidation_packet_handler_;
110   scoped_ptr<RegistrationManager> registration_manager_;
111   std::map<syncable::ModelType, int64> max_invalidation_versions_;
112   // Stored to pass to |registration_manager_| on start.
113   syncable::ModelTypeSet registered_types_;
114 
115   DISALLOW_COPY_AND_ASSIGN(ChromeInvalidationClient);
116 };
117 
118 }  // namespace sync_notifier
119 
120 #endif  // CHROME_BROWSER_SYNC_NOTIFIER_CHROME_INVALIDATION_CLIENT_H_
121