diff options
author | akalin@chromium.org <akalin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-18 23:50:21 +0000 |
---|---|---|
committer | akalin@chromium.org <akalin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-18 23:50:21 +0000 |
commit | 915679f57bee15233874af080d8bf30394988dbe (patch) | |
tree | aa399d41ad8fd66de0d6746aba42c5b44da71e6d | |
parent | 71b68126dfbd5f25a2bc01438ddc42a9ec60f5be (diff) | |
download | chromium_src-915679f57bee15233874af080d8bf30394988dbe.zip chromium_src-915679f57bee15233874af080d8bf30394988dbe.tar.gz chromium_src-915679f57bee15233874af080d8bf30394988dbe.tar.bz2 |
[Sync] Rewrite sync_listen_notifications tool to use SyncNotifier interface
BUG=
TEST=
Review URL: http://codereview.chromium.org/6688012
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@78776 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/sync/notifier/sync_notifier_impl.cc | 4 | ||||
-rw-r--r-- | chrome/browser/sync/tools/sync_listen_notifications.cc | 352 | ||||
-rw-r--r-- | chrome/browser/sync/tools/sync_tools.gyp | 8 | ||||
-rw-r--r-- | chrome/chrome.gyp | 2 | ||||
-rw-r--r-- | chrome/chrome_browser.gypi | 2 |
5 files changed, 59 insertions, 309 deletions
diff --git a/chrome/browser/sync/notifier/sync_notifier_impl.cc b/chrome/browser/sync/notifier/sync_notifier_impl.cc index ec58f3d..ac501f1 100644 --- a/chrome/browser/sync/notifier/sync_notifier_impl.cc +++ b/chrome/browser/sync/notifier/sync_notifier_impl.cc @@ -150,7 +150,9 @@ void SyncNotifierImpl::UpdateCredentials( server_notifier_thread_ = NULL; } talk_mediator_->SetDelegate(this); - talk_mediator_->SetAuthToken(email, token, SYNC_SERVICE_NAME); + if (!talk_mediator_->SetAuthToken(email, token, SYNC_SERVICE_NAME)) { + LOG(ERROR) << "Could not set auth info for " << email; + } talk_mediator_->Login(); } diff --git a/chrome/browser/sync/tools/sync_listen_notifications.cc b/chrome/browser/sync/tools/sync_listen_notifications.cc index d451c85..ee63194 100644 --- a/chrome/browser/sync/tools/sync_listen_notifications.cc +++ b/chrome/browser/sync/tools/sync_listen_notifications.cc @@ -1,247 +1,53 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// 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 <string> -#include <vector> #include "base/at_exit.h" #include "base/base64.h" #include "base/command_line.h" #include "base/logging.h" #include "base/message_loop.h" -#include "base/observer_list.h" -#include "base/string_util.h" -#include "base/task.h" -#include "base/weak_ptr.h" -#include "chrome/browser/sync/notifier/cache_invalidation_packet_handler.h" -#include "chrome/browser/sync/notifier/chrome_invalidation_client.h" -#include "chrome/browser/sync/notifier/chrome_system_resources.h" -#include "chrome/browser/sync/sync_constants.h" -#include "jingle/notifier/base/xmpp_connection.h" -#include "jingle/notifier/listener/notification_constants.h" -#include "jingle/notifier/listener/notification_defines.h" -#include "jingle/notifier/listener/push_notifications_listen_task.h" -#include "jingle/notifier/listener/push_notifications_send_update_task.h" -#include "jingle/notifier/listener/push_notifications_subscribe_task.h" -#include "jingle/notifier/listener/xml_element_util.h" -#include "net/base/cert_verifier.h" -#include "net/base/ssl_config_service.h" -#include "net/socket/client_socket_factory.h" -#include "talk/base/cryptstring.h" -#include "talk/base/logging.h" -#include "talk/base/sigslot.h" -#include "talk/base/task.h" -#include "talk/xmpp/jid.h" -#include "talk/xmpp/xmppclientsettings.h" -#include "talk/xmpp/constants.h" -#include "talk/xmpp/xmppengine.h" +#include "base/scoped_ptr.h" +#include "chrome/browser/sync/notifier/sync_notifier.h" +#include "chrome/browser/sync/notifier/sync_notifier_factory.h" +#include "chrome/browser/sync/notifier/sync_notifier_observer.h" +#include "chrome/browser/sync/syncable/model_type.h" -// This is a simple utility that logs into an XMPP server, subscribes -// to Sync notifications, and prints out any such notifications that -// are received. -// -// TODO(akalin): Rewrite this utility to use -// sync_notifier::SyncNotifier et al. +// This is a simple utility that initializes a sync notifier and +// listens to any received notifications. namespace { -// Main class that listens for and handles messages from the XMPP -// client. -class XmppNotificationClient : public notifier::XmppConnection::Delegate { +// Class to print received notifications events. +class NotificationPrinter : public sync_notifier::SyncNotifierObserver { public: - // An observer is notified when we are logged into XMPP or when an - // error occurs. - class Observer { - public: - virtual ~Observer() {} - - virtual void OnConnect(base::WeakPtr<talk_base::Task> base_task) = 0; - - virtual void OnError() = 0; - }; - - template <class T> XmppNotificationClient(T begin, T end) { - for (T it = begin; it != end; ++it) { - observer_list_.AddObserver(*it); - } - } - - virtual ~XmppNotificationClient() {} - - // Connect with the given XMPP settings and run until disconnected. - void Run(const buzz::XmppClientSettings& xmpp_client_settings, - net::CertVerifier* cert_verifier) { - DCHECK(!xmpp_connection_.get()); - xmpp_connection_.reset( - new notifier::XmppConnection(xmpp_client_settings, cert_verifier, - this, NULL)); - MessageLoop::current()->Run(); - DCHECK(!xmpp_connection_.get()); - } - - virtual void OnConnect(base::WeakPtr<talk_base::Task> base_task) { - FOR_EACH_OBSERVER(Observer, observer_list_, OnConnect(base_task)); - } - - virtual void OnError(buzz::XmppEngine::Error error, int subcode, - const buzz::XmlElement* stream_error) { - LOG(INFO) << "Error: " << error << ", subcode: " << subcode; - if (stream_error) { - LOG(INFO) << "Stream error: " - << notifier::XmlElementToString(*stream_error); - } - FOR_EACH_OBSERVER(Observer, observer_list_, OnError()); - // This has to go before the message loop quits. - xmpp_connection_.reset(); - MessageLoop::current()->Quit(); - } - - private: - ObserverList<Observer> observer_list_; - scoped_ptr<notifier::XmppConnection> xmpp_connection_; - - DISALLOW_COPY_AND_ASSIGN(XmppNotificationClient); -}; - -// Delegate for legacy notifications. -class LegacyNotifierDelegate - : public XmppNotificationClient::Observer, - public notifier::PushNotificationsListenTask::Delegate, - public notifier::PushNotificationsSubscribeTask::Delegate, - public sigslot::has_slots<> { - public: - LegacyNotifierDelegate(const buzz::Jid& jid, bool send_initial_update) - : jid_(jid), send_initial_update_(send_initial_update) {} - - virtual ~LegacyNotifierDelegate() {} - - // XmppNotificationClient::Observer implementation. - virtual void OnConnect(base::WeakPtr<talk_base::Task> base_task) { - LOG(INFO) << "Logged in"; - notifier::SubscriptionList subscriptions; - notifier::Subscription subscription; - subscription.channel = browser_sync::kSyncNotificationChannel; - subscription.from = jid_.Str(); - subscriptions.push_back(subscription); - // Owned by base_task. - notifier::PushNotificationsSubscribeTask* subscribe_task = - new notifier::PushNotificationsSubscribeTask( - base_task, subscriptions, this); - subscribe_task->Start(); - // Owned by xmpp_client. - notifier::PushNotificationsListenTask* listen_task = - new notifier::PushNotificationsListenTask(base_task, this); - listen_task->Start(); - if (send_initial_update_) { - notifier::Notification notification; - notification.channel = browser_sync::kSyncNotificationChannel; - notification.data = browser_sync::kSyncNotificationData; - // Owned by xmpp_client. - notifier::PushNotificationsSendUpdateTask* send_update_task = - new notifier::PushNotificationsSendUpdateTask( - base_task, notification); - send_update_task->Start(); + NotificationPrinter() {} + virtual ~NotificationPrinter() {} + + virtual void OnIncomingNotification( + const browser_sync::sessions::TypePayloadMap& type_payloads) { + for (browser_sync::sessions::TypePayloadMap::const_iterator it = + type_payloads.begin(); it != type_payloads.end(); ++it) { + LOG(INFO) << "Notification: type = " + << syncable::ModelTypeToString(it->first) + << ", payload = " << it->second; } } - virtual void OnError() { - LOG(WARNING) << "XMPP error"; - } - - // notifier::PushNotificationsListenTask::Delegate implementation. - virtual void OnNotificationReceived( - const notifier::Notification& notification) { - LOG(INFO) << "Notification received: " - << notification.channel << " " << notification.data; - } - - // notifier::PushNotificationsSubscribeTask::Delegate - // implementation. - virtual void OnSubscribed() { - LOG(INFO) << "Subscribed"; - } - - virtual void OnSubscriptionError() { - LOG(WARNING) << "Subscription error"; + virtual void OnNotificationStateChange(bool notifications_enabled) { + LOG(INFO) << "Notifications enabled: " << notifications_enabled; } - private: - buzz::Jid jid_; - bool send_initial_update_; -}; - -// The actual listener for sync notifications. -class ChromeInvalidationListener - : public sync_notifier::ChromeInvalidationClient::Listener { - public: - ChromeInvalidationListener() {} - - virtual void OnInvalidate(syncable::ModelType model_type, - const std::string& payload) { - LOG(INFO) << "OnInvalidate: " - << syncable::ModelTypeToString(model_type) << " - " << payload; - // A real implementation would respond to the invalidation. - } - - virtual void OnInvalidateAll() { - LOG(INFO) << "InvalidateAll"; - // A real implementation would loop over the current registered - // data types and send notifications for those. - } - - private: - DISALLOW_COPY_AND_ASSIGN(ChromeInvalidationListener); -}; - -// Delegate for server-issued notifications. -class ServerNotifierDelegate - : public XmppNotificationClient::Observer, - public sync_notifier::StateWriter { - public: - explicit ServerNotifierDelegate( - const std::string& server_notifier_state) - : server_notifier_state_(server_notifier_state) {} - - virtual ~ServerNotifierDelegate() {} - - virtual void OnConnect(base::WeakPtr<talk_base::Task> base_task) { - LOG(INFO) << "Logged in"; - - // TODO(akalin): app_name should be per-client unique. - const std::string kAppName = "cc_sync_listen_notifications"; - const std::string kAppInfo = kAppName; - chrome_invalidation_client_.Start(kAppName, kAppInfo, - server_notifier_state_, - &chrome_invalidation_listener_, - this, base_task); - syncable::ModelTypeSet all_types; - for (int i = syncable::FIRST_REAL_MODEL_TYPE; - i < syncable::MODEL_TYPE_COUNT; ++i) { - syncable::ModelType model_type = syncable::ModelTypeFromInt(i); - all_types.insert(model_type); - } - - chrome_invalidation_client_.RegisterTypes(all_types); - } - - virtual void OnError() { - chrome_invalidation_client_.Stop(); - } - - virtual void WriteState(const std::string& state) { + virtual void StoreState(const std::string& state) { std::string base64_state; CHECK(base::Base64Encode(state, &base64_state)); - LOG(INFO) << "New state: " << base64_state; + LOG(INFO) << "Got state to store: " << base64_state; } private: - ChromeInvalidationListener chrome_invalidation_listener_; - // Opaque blob capturing the notifications state of a previous run - // (i.e., the base64-decoded value of a string output by - // WriteState()). - std::string server_notifier_state_; - sync_notifier::ChromeInvalidationClient chrome_invalidation_client_; + DISALLOW_COPY_AND_ASSIGN(NotificationPrinter); }; } // namespace @@ -255,96 +61,46 @@ int main(int argc, char* argv[]) { logging::LOCK_LOG_FILE, logging::DELETE_OLD_LOG_FILE, logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS); - // TODO(akalin): Make sure that all log messages are printed to the - // console, even on Windows (SetMinLogLevel isn't enough). - talk_base::LogMessage::LogToDebug(talk_base::LS_VERBOSE); // Parse command line. const CommandLine& command_line = *CommandLine::ForCurrentProcess(); std::string email = command_line.GetSwitchValueASCII("email"); - if (email.empty()) { - printf("Usage: %s --email=foo@bar.com [--password=mypassword] " - "[--server=talk.google.com] [--port=5222] [--allow-plain] " - "[--disable-tls] [--use-ssl-tcp] [--server-notifier-state] " - "[--use-legacy-notifier] " - "[--legacy-notifier-send-initial-update]\n", + std::string token = command_line.GetSwitchValueASCII("token"); + // TODO(akalin): Write a wrapper script that gets a token for an + // email and password and passes that in to this utility. + if (email.empty() || token.empty()) { + printf("Usage: %s --email=foo@bar.com --token=token\n\n" + "See sync_notifier_factory.cc for more switches.\n\n" + "Run chrome and set a breakpoint on " + "SyncNotifierImpl::UpdateCredentials() after\n" + "logging into sync to get the token to pass into this utility.\n", argv[0]); return -1; } - std::string password = command_line.GetSwitchValueASCII("password"); - std::string server = command_line.GetSwitchValueASCII("server"); - if (server.empty()) { - server = "talk.google.com"; - } - std::string port_str = command_line.GetSwitchValueASCII("port"); - int port = 5222; - if (!port_str.empty()) { - int port_from_port_str = std::strtol(port_str.c_str(), NULL, 10); - if (port_from_port_str == 0) { - LOG(WARNING) << "Invalid port " << port_str << "; using default"; - } else { - port = port_from_port_str; - } - } - bool allow_plain = command_line.HasSwitch("allow-plain"); - bool disable_tls = command_line.HasSwitch("disable-tls"); - bool use_ssl_tcp = command_line.HasSwitch("use-ssl-tcp"); - if (use_ssl_tcp && (port != 443)) { - LOG(WARNING) << "--use-ssl-tcp is set but port is " << port - << " instead of 443"; - } - std::string server_notifier_state; - std::string server_notifier_state_encoded = - command_line.GetSwitchValueASCII("server-notifier-state"); - if (!server_notifier_state_encoded.empty() && - !base::Base64Decode(server_notifier_state_encoded, - &server_notifier_state)) { - LOG(ERROR) << "Could not decode state: " - << server_notifier_state_encoded; - } - bool use_legacy_notifier = command_line.HasSwitch("use-legacy-notifier"); - bool legacy_notifier_send_initial_update = - command_line.HasSwitch("legacy-notifier-send-initial-update"); - // Build XMPP client settings. - buzz::XmppClientSettings xmpp_client_settings; - buzz::Jid jid(email); - xmpp_client_settings.set_user(jid.node()); - xmpp_client_settings.set_resource("cc_sync_listen_notifications"); - xmpp_client_settings.set_host(jid.domain()); - xmpp_client_settings.set_allow_plain(allow_plain); - xmpp_client_settings.set_use_tls(!disable_tls); - if (use_ssl_tcp) { - xmpp_client_settings.set_protocol(cricket::PROTO_SSLTCP); - } - talk_base::InsecureCryptStringImpl insecure_crypt_string; - insecure_crypt_string.password() = password; - xmpp_client_settings.set_pass( - talk_base::CryptString(insecure_crypt_string)); - talk_base::SocketAddress addr(server, port); - if (!addr.ResolveIP()) { - LOG(ERROR) << "Could not resolve " << addr.ToString(); - return -1; - } - xmpp_client_settings.set_server(addr); + // Needed by the SyncNotifier implementations. + MessageLoop main_loop; - net::CertVerifier cert_verifier; - MessageLoopForIO message_loop; + const char kClientInfo[] = "sync_listen_notifications"; + sync_notifier::SyncNotifierFactory sync_notifier_factory(kClientInfo); + scoped_ptr<sync_notifier::SyncNotifier> sync_notifier( + sync_notifier_factory.CreateSyncNotifier(command_line)); + NotificationPrinter notification_printer; + sync_notifier->AddObserver(¬ification_printer); - // Connect and listen. - ServerNotifierDelegate server_notifier_delegate( - server_notifier_state); - LegacyNotifierDelegate legacy_notifier_delegate( - jid, legacy_notifier_send_initial_update); - std::vector<XmppNotificationClient::Observer*> observers; - if (use_legacy_notifier) { - observers.push_back(&legacy_notifier_delegate); - } else { - observers.push_back(&server_notifier_delegate); + sync_notifier->UpdateCredentials(email, token); + { + // Listen for notifications for all known types. + syncable::ModelTypeSet types; + for (int i = syncable::FIRST_REAL_MODEL_TYPE; + i < syncable::MODEL_TYPE_COUNT; ++i) { + types.insert(syncable::ModelTypeFromInt(i)); + } + sync_notifier->UpdateEnabledTypes(types); } - XmppNotificationClient xmpp_notification_client( - observers.begin(), observers.end()); - xmpp_notification_client.Run(xmpp_client_settings, &cert_verifier); + main_loop.Run(); + + sync_notifier->RemoveObserver(¬ification_printer); return 0; } diff --git a/chrome/browser/sync/tools/sync_tools.gyp b/chrome/browser/sync/tools/sync_tools.gyp index 18d163c..d4e290f 100644 --- a/chrome/browser/sync/tools/sync_tools.gyp +++ b/chrome/browser/sync/tools/sync_tools.gyp @@ -9,18 +9,10 @@ 'type': 'executable', 'sources': [ 'sync_listen_notifications.cc', - # We are directly including the sync_constants.cc and h files to avoid - # pulling in browser.lib. - # TODO(akalin): Fix this. - '<(DEPTH)/chrome/browser/sync/sync_constants.cc', - '<(DEPTH)/chrome/browser/sync/sync_constants.h', ], 'dependencies': [ '<(DEPTH)/base/base.gyp:base', - '<(DEPTH)/chrome/chrome.gyp:common_constants', '<(DEPTH)/chrome/chrome.gyp:sync_notifier', - '<(DEPTH)/jingle/jingle.gyp:notifier', - '<(DEPTH)/third_party/libjingle/libjingle.gyp:libjingle', ], }, ], diff --git a/chrome/chrome.gyp b/chrome/chrome.gyp index dbc8d0f..d89f17f 100644 --- a/chrome/chrome.gyp +++ b/chrome/chrome.gyp @@ -744,6 +744,8 @@ 'browser/sync/sessions/sync_session.h', 'browser/sync/sessions/sync_session_context.cc', 'browser/sync/sessions/sync_session_context.h', + 'browser/sync/sync_constants.cc', + 'browser/sync/sync_constants.h', 'browser/sync/syncable/autofill_migration.h', 'browser/sync/syncable/blob.h', 'browser/sync/syncable/dir_open_result.h', diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index 8a0b7d5..858379e 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -1942,8 +1942,6 @@ 'browser/sync/profile_sync_service_observer.h', 'browser/sync/signin_manager.cc', 'browser/sync/signin_manager.h', - 'browser/sync/sync_constants.cc', - 'browser/sync/sync_constants.h', 'browser/sync/sync_setup_flow.cc', 'browser/sync/sync_setup_flow.h', 'browser/sync/sync_setup_wizard.cc', |