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// An implementation of SyncNotifier that wraps an invalidation
6// client.  Handles the details of connecting to XMPP and hooking it
7// up to the invalidation client.
8//
9// You probably don't want to use this directly; use
10// NonBlockingInvalidationNotifier.
11
12#ifndef CHROME_BROWSER_SYNC_NOTIFIER_INVALIDATION_NOTIFIER_H_
13#define CHROME_BROWSER_SYNC_NOTIFIER_INVALIDATION_NOTIFIER_H_
14#pragma once
15
16#include <string>
17
18#include "base/basictypes.h"
19#include "base/compiler_specific.h"
20#include "base/memory/scoped_ptr.h"
21#include "base/observer_list.h"
22#include "base/threading/non_thread_safe.h"
23#include "chrome/browser/sync/notifier/chrome_invalidation_client.h"
24#include "chrome/browser/sync/notifier/state_writer.h"
25#include "chrome/browser/sync/notifier/sync_notifier.h"
26#include "chrome/browser/sync/syncable/model_type.h"
27#include "jingle/notifier/base/notifier_options.h"
28#include "jingle/notifier/communicator/login.h"
29
30namespace sync_notifier {
31
32// This class must live on the IO thread.
33class InvalidationNotifier
34    : public SyncNotifier,
35      public notifier::LoginDelegate,
36      public ChromeInvalidationClient::Listener,
37      public StateWriter {
38 public:
39  InvalidationNotifier(
40      const notifier::NotifierOptions& notifier_options,
41      const std::string& client_info);
42
43  virtual ~InvalidationNotifier();
44
45  // SyncNotifier implementation.
46  virtual void AddObserver(SyncNotifierObserver* observer) OVERRIDE;
47  virtual void RemoveObserver(SyncNotifierObserver* observer) OVERRIDE;
48  virtual void SetState(const std::string& state) OVERRIDE;
49  virtual void UpdateCredentials(
50      const std::string& email, const std::string& token) OVERRIDE;
51  virtual void UpdateEnabledTypes(
52      const syncable::ModelTypeSet& types) OVERRIDE;
53  virtual void SendNotification() OVERRIDE;
54
55  // notifier::LoginDelegate implementation.
56  virtual void OnConnect(base::WeakPtr<talk_base::Task> base_task) OVERRIDE;
57  virtual void OnDisconnect() OVERRIDE;
58
59  // ChromeInvalidationClient::Listener implementation.
60  virtual void OnInvalidate(
61      const syncable::ModelTypePayloadMap& type_payloads) OVERRIDE;
62  virtual void OnSessionStatusChanged(bool has_session) OVERRIDE;
63
64  // StateWriter implementation.
65  virtual void WriteState(const std::string& state) OVERRIDE;
66
67 private:
68  base::NonThreadSafe non_thread_safe_;
69
70  // We start off in the STOPPED state.  When we get our initial
71  // credentials, we connect and move to the CONNECTING state.  When
72  // we're connected we start the invalidation client and move to the
73  // STARTED state.  We never go back to a previous state.
74  enum State {
75    STOPPED,
76    CONNECTING,
77    STARTED
78  };
79  State state_;
80
81  // Used to build parameters for |login_|.
82  const notifier::NotifierOptions notifier_options_;
83
84  // Passed to |invalidation_client_|.
85  const std::string client_info_;
86
87  // Our observers (which must live on the same thread).
88  ObserverList<SyncNotifierObserver> observers_;
89
90  // The state to pass to |chrome_invalidation_client_|.
91  std::string invalidation_state_;
92
93  // The XMPP connection manager.
94  scoped_ptr<notifier::Login> login_;
95
96  // The invalidation client.
97  ChromeInvalidationClient invalidation_client_;
98
99  DISALLOW_COPY_AND_ASSIGN(InvalidationNotifier);
100};
101
102}  // namespace sync_notifier
103
104#endif  // CHROME_BROWSER_SYNC_NOTIFIER_INVALIDATION_NOTIFIER_H_
105