1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/sync/notifier/invalidation_notifier.h"
#include "base/logging.h"
#include "base/message_loop_proxy.h"
#include "chrome/browser/sync/notifier/sync_notifier_observer.h"
#include "chrome/browser/sync/protocol/service_constants.h"
#include "chrome/browser/sync/syncable/model_type_payload_map.h"
#include "jingle/notifier/base/const_communicator.h"
#include "jingle/notifier/base/notifier_options_util.h"
#include "jingle/notifier/communicator/connection_options.h"
#include "net/base/host_port_pair.h"
#include "net/url_request/url_request_context.h"
#include "talk/xmpp/jid.h"
#include "talk/xmpp/xmppclientsettings.h"
namespace sync_notifier {
InvalidationNotifier::InvalidationNotifier(
const notifier::NotifierOptions& notifier_options,
const std::string& client_info)
: state_(STOPPED),
notifier_options_(notifier_options),
client_info_(client_info) {
DCHECK_EQ(notifier::NOTIFICATION_SERVER,
notifier_options.notification_method);
DCHECK(notifier_options_.request_context_getter);
// TODO(akalin): Replace NonThreadSafe checks with IO thread checks.
DCHECK(notifier_options_.request_context_getter->GetIOMessageLoopProxy()->
BelongsToCurrentThread());
}
InvalidationNotifier::~InvalidationNotifier() {
DCHECK(non_thread_safe_.CalledOnValidThread());
}
void InvalidationNotifier::AddObserver(SyncNotifierObserver* observer) {
DCHECK(non_thread_safe_.CalledOnValidThread());
observers_.AddObserver(observer);
}
void InvalidationNotifier::RemoveObserver(SyncNotifierObserver* observer) {
DCHECK(non_thread_safe_.CalledOnValidThread());
observers_.RemoveObserver(observer);
}
void InvalidationNotifier::SetUniqueId(const std::string& unique_id) {
DCHECK(non_thread_safe_.CalledOnValidThread());
invalidation_client_id_ = unique_id;
}
void InvalidationNotifier::SetState(const std::string& state) {
DCHECK(non_thread_safe_.CalledOnValidThread());
invalidation_state_ = state;
}
void InvalidationNotifier::UpdateCredentials(
const std::string& email, const std::string& token) {
DCHECK(non_thread_safe_.CalledOnValidThread());
VLOG(1) << "Updating credentials for " << email;
buzz::XmppClientSettings xmpp_client_settings =
notifier::MakeXmppClientSettings(notifier_options_,
email, token, SYNC_SERVICE_NAME);
if (state_ >= CONNECTING) {
login_->UpdateXmppSettings(xmpp_client_settings);
} else {
notifier::ConnectionOptions options;
VLOG(1) << "First time updating credentials: connecting";
login_.reset(
new notifier::Login(this,
xmpp_client_settings,
notifier::ConnectionOptions(),
notifier_options_.request_context_getter,
notifier::GetServerList(notifier_options_),
notifier_options_.try_ssltcp_first,
notifier_options_.auth_mechanism));
login_->StartConnection();
state_ = CONNECTING;
}
}
void InvalidationNotifier::UpdateEnabledTypes(
const syncable::ModelTypeSet& enabled_types) {
DCHECK(non_thread_safe_.CalledOnValidThread());
invalidation_client_.RegisterTypes(enabled_types);
}
void InvalidationNotifier::SendNotification(
const syncable::ModelTypeSet& changed_types) {
DCHECK(non_thread_safe_.CalledOnValidThread());
// Do nothing.
}
void InvalidationNotifier::OnConnect(
base::WeakPtr<buzz::XmppTaskParentInterface> base_task) {
DCHECK(non_thread_safe_.CalledOnValidThread());
VLOG(1) << "OnConnect";
if (state_ >= STARTED) {
invalidation_client_.ChangeBaseTask(base_task);
} else {
VLOG(1) << "First time connecting: starting invalidation client";
// TODO(akalin): Make cache_guid() part of the client ID. If we
// do so and we somehow propagate it up to the server somehow, we
// can make it so that we won't receive any notifications that
// were generated from our own changes.
const std::string kClientId = "invalidation_notifier";
invalidation_client_.Start(
kClientId, client_info_, invalidation_state_, this, this, base_task);
invalidation_state_.clear();
state_ = STARTED;
}
}
void InvalidationNotifier::OnDisconnect() {
DCHECK(non_thread_safe_.CalledOnValidThread());
VLOG(1) << "OnDisconnect";
}
void InvalidationNotifier::OnInvalidate(
const syncable::ModelTypePayloadMap& type_payloads) {
DCHECK(non_thread_safe_.CalledOnValidThread());
FOR_EACH_OBSERVER(SyncNotifierObserver, observers_,
OnIncomingNotification(type_payloads));
}
void InvalidationNotifier::OnSessionStatusChanged(bool has_session) {
FOR_EACH_OBSERVER(SyncNotifierObserver, observers_,
OnNotificationStateChange(has_session));
}
void InvalidationNotifier::WriteState(const std::string& state) {
DCHECK(non_thread_safe_.CalledOnValidThread());
VLOG(1) << "WriteState";
FOR_EACH_OBSERVER(SyncNotifierObserver, observers_, StoreState(state));
}
} // namespace sync_notifier
|