summaryrefslogtreecommitdiffstats
path: root/sync
diff options
context:
space:
mode:
authorakalin@chromium.org <akalin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-04-20 01:10:11 +0000
committerakalin@chromium.org <akalin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-04-20 01:10:11 +0000
commitfdb5e3ce58e66d6a273968b9897fcd5208928068 (patch)
treed54d153180cfd2e92436b357072aa83c65d2d464 /sync
parent8711ac947f1afc1e40c9e08ed1639c5b2182a919 (diff)
downloadchromium_src-fdb5e3ce58e66d6a273968b9897fcd5208928068.zip
chromium_src-fdb5e3ce58e66d6a273968b9897fcd5208928068.tar.gz
chromium_src-fdb5e3ce58e66d6a273968b9897fcd5208928068.tar.bz2
[Sync] Move 'sync_listen_notifications' target to sync/tools
BUG=117585 TEST= Review URL: http://codereview.chromium.org/10139004 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@133109 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'sync')
-rw-r--r--sync/sync.gyp17
-rw-r--r--sync/tools/DEPS8
-rw-r--r--sync/tools/sync_listen_notifications.cc195
3 files changed, 220 insertions, 0 deletions
diff --git a/sync/sync.gyp b/sync/sync.gyp
index 69221e3..cc4d83d 100644
--- a/sync/sync.gyp
+++ b/sync/sync.gyp
@@ -482,5 +482,22 @@
}],
],
},
+
+ # A tool to listen to sync notifications and print them out.
+ {
+ 'target_name': 'sync_listen_notifications',
+ 'type': 'executable',
+ 'dependencies': [
+ '../base/base.gyp:base',
+ '../jingle/jingle.gyp:notifier',
+ '../net/net.gyp:net',
+ '../net/net.gyp:net_test_support',
+ 'sync',
+ 'sync_notifier',
+ ],
+ 'sources': [
+ 'tools/sync_listen_notifications.cc',
+ ],
+ },
],
}
diff --git a/sync/tools/DEPS b/sync/tools/DEPS
new file mode 100644
index 0000000..befcabe
--- /dev/null
+++ b/sync/tools/DEPS
@@ -0,0 +1,8 @@
+include_rules = [
+ "+jingle/notifier/base",
+ "+net/base/host_port_pair.h",
+ "+net/url_request/url_request_test_util.h",
+ "+sync/notifier",
+ "+sync/syncable/model_type.h",
+ "+sync/syncable/model_type_payload_map.h",
+]
diff --git a/sync/tools/sync_listen_notifications.cc b/sync/tools/sync_listen_notifications.cc
new file mode 100644
index 0000000..7b38c0c
--- /dev/null
+++ b/sync/tools/sync_listen_notifications.cc
@@ -0,0 +1,195 @@
+// Copyright (c) 2012 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 <cstdio>
+#include <string>
+
+#include "base/at_exit.h"
+#include "base/base64.h"
+#include "base/command_line.h"
+#include "base/compiler_specific.h"
+#include "base/logging.h"
+#include "base/memory/ref_counted.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/memory/weak_ptr.h"
+#include "base/message_loop.h"
+#include "base/threading/thread.h"
+#include "jingle/notifier/base/notification_method.h"
+#include "jingle/notifier/base/notifier_options.h"
+#include "net/base/host_port_pair.h"
+#include "net/url_request/url_request_test_util.h"
+#include "sync/notifier/invalidation_version_tracker.h"
+#include "sync/notifier/sync_notifier.h"
+#include "sync/notifier/sync_notifier_factory.h"
+#include "sync/notifier/sync_notifier_observer.h"
+#include "sync/syncable/model_type.h"
+#include "sync/syncable/model_type_payload_map.h"
+
+// This is a simple utility that initializes a sync notifier and
+// listens to any received notifications.
+
+namespace {
+
+// Class to print received notifications events.
+class NotificationPrinter : public sync_notifier::SyncNotifierObserver {
+ public:
+ NotificationPrinter() {}
+ virtual ~NotificationPrinter() {}
+
+ virtual void OnIncomingNotification(
+ const syncable::ModelTypePayloadMap& type_payloads,
+ sync_notifier::IncomingNotificationSource source) OVERRIDE {
+ for (syncable::ModelTypePayloadMap::const_iterator it =
+ type_payloads.begin(); it != type_payloads.end(); ++it) {
+ LOG(INFO) << (source == sync_notifier::REMOTE_NOTIFICATION ?
+ "Remote" : "Local")
+ << " Notification: type = "
+ << syncable::ModelTypeToString(it->first)
+ << ", payload = " << it->second;
+ }
+ }
+
+ virtual void OnNotificationStateChange(
+ bool notifications_enabled) OVERRIDE {
+ LOG(INFO) << "Notifications enabled: " << notifications_enabled;
+ }
+
+ virtual void StoreState(const std::string& state) OVERRIDE {
+ std::string base64_state;
+ CHECK(base::Base64Encode(state, &base64_state));
+ LOG(INFO) << "Got state to store: " << base64_state;
+ }
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(NotificationPrinter);
+};
+
+class NullInvalidationVersionTracker
+ : public base::SupportsWeakPtr<NullInvalidationVersionTracker>,
+ public sync_notifier::InvalidationVersionTracker {
+ public:
+ NullInvalidationVersionTracker() {}
+ virtual ~NullInvalidationVersionTracker() {}
+
+ virtual sync_notifier::InvalidationVersionMap
+ GetAllMaxVersions() const OVERRIDE {
+ return sync_notifier::InvalidationVersionMap();
+ }
+
+ virtual void SetMaxVersion(
+ syncable::ModelType model_type,
+ int64 max_invalidation_version) OVERRIDE {
+ LOG(INFO) << "Setting max invalidation version for "
+ << syncable::ModelTypeToString(model_type) << " to "
+ << max_invalidation_version;
+ }
+};
+
+const char kEmailSwitch[] = "email";
+const char kTokenSwitch[] = "token";
+const char kHostPortSwitch[] = "host-port";
+const char kTrySslTcpFirstSwitch[] = "try-ssltcp-first";
+const char kAllowInsecureConnectionSwitch[] = "allow-insecure-connection";
+const char kNotificationMethodSwitch[] = "notification-method";
+
+notifier::NotifierOptions ParseNotifierOptions(
+ const CommandLine& command_line,
+ const scoped_refptr<net::URLRequestContextGetter>&
+ request_context_getter) {
+ notifier::NotifierOptions notifier_options;
+ notifier_options.request_context_getter = request_context_getter;
+
+ if (command_line.HasSwitch(kHostPortSwitch)) {
+ notifier_options.xmpp_host_port =
+ net::HostPortPair::FromString(
+ command_line.GetSwitchValueASCII(kHostPortSwitch));
+ LOG(INFO) << "Using " << notifier_options.xmpp_host_port.ToString()
+ << " for test sync notification server.";
+ }
+
+ notifier_options.try_ssltcp_first =
+ command_line.HasSwitch(kTrySslTcpFirstSwitch);
+ LOG_IF(INFO, notifier_options.try_ssltcp_first)
+ << "Trying SSL/TCP port before XMPP port for notifications.";
+
+ notifier_options.allow_insecure_connection =
+ command_line.HasSwitch(kAllowInsecureConnectionSwitch);
+ LOG_IF(INFO, notifier_options.allow_insecure_connection)
+ << "Allowing insecure XMPP connections.";
+
+ if (command_line.HasSwitch(kNotificationMethodSwitch)) {
+ notifier_options.notification_method =
+ notifier::StringToNotificationMethod(
+ command_line.GetSwitchValueASCII(kNotificationMethodSwitch));
+ }
+
+ return notifier_options;
+}
+
+} // namespace
+
+int main(int argc, char* argv[]) {
+ base::AtExitManager exit_manager;
+ CommandLine::Init(argc, argv);
+ logging::InitLogging(
+ NULL,
+ logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG,
+ logging::LOCK_LOG_FILE,
+ logging::DELETE_OLD_LOG_FILE,
+ logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS);
+
+ MessageLoop ui_loop;
+ base::Thread io_thread("IO thread");
+ base::Thread::Options options;
+ options.message_loop_type = MessageLoop::TYPE_IO;
+ io_thread.StartWithOptions(options);
+
+ // Parse command line.
+ const CommandLine& command_line = *CommandLine::ForCurrentProcess();
+ std::string email = command_line.GetSwitchValueASCII(kEmailSwitch);
+ std::string token = command_line.GetSwitchValueASCII(kTokenSwitch);
+ // 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()) {
+ std::printf("Usage: %s --%s=foo@bar.com --%s=token\n"
+ "[--%s=host:port] [--%s] [--%s]\n"
+ "[--%s=(server|p2p)]\n\n"
+ "Run chrome and set a breakpoint on\n"
+ "sync_api::SyncManager::SyncInternal::UpdateCredentials() "
+ "after logging into\n"
+ "sync to get the token to pass into this utility.\n",
+ argv[0],
+ kTokenSwitch, kEmailSwitch, kHostPortSwitch,
+ kTrySslTcpFirstSwitch, kAllowInsecureConnectionSwitch,
+ kNotificationMethodSwitch);
+ return -1;
+ }
+
+ const notifier::NotifierOptions& notifier_options =
+ ParseNotifierOptions(
+ command_line,
+ new TestURLRequestContextGetter(io_thread.message_loop_proxy()));
+ const char kClientInfo[] = "sync_listen_notifications";
+ NullInvalidationVersionTracker null_invalidation_version_tracker;
+ sync_notifier::SyncNotifierFactory sync_notifier_factory(
+ notifier_options, kClientInfo,
+ null_invalidation_version_tracker.AsWeakPtr());
+ scoped_ptr<sync_notifier::SyncNotifier> sync_notifier(
+ sync_notifier_factory.CreateSyncNotifier());
+ NotificationPrinter notification_printer;
+ sync_notifier->AddObserver(&notification_printer);
+
+ const char kUniqueId[] = "fake_unique_id";
+ sync_notifier->SetUniqueId(kUniqueId);
+ sync_notifier->SetState("");
+ sync_notifier->UpdateCredentials(email, token);
+ // Listen for notifications for all known types.
+ sync_notifier->UpdateEnabledTypes(syncable::ModelTypeSet::All());
+
+ ui_loop.Run();
+
+ sync_notifier->RemoveObserver(&notification_printer);
+ io_thread.Stop();
+ return 0;
+}