OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 #include <cstddef> | |
6 #include <cstdio> | |
7 #include <memory> | |
8 #include <string> | |
9 | |
10 #include "base/at_exit.h" | |
11 #include "base/command_line.h" | |
12 #include "base/compiler_specific.h" | |
13 #include "base/logging.h" | |
14 #include "base/macros.h" | |
15 #include "base/memory/ptr_util.h" | |
16 #include "base/memory/ref_counted.h" | |
17 #include "base/message_loop/message_loop.h" | |
18 #include "base/rand_util.h" | |
19 #include "base/run_loop.h" | |
20 #include "base/threading/thread.h" | |
21 #include "build/build_config.h" | |
22 #include "components/invalidation/impl/invalidation_state_tracker.h" | |
23 #include "components/invalidation/impl/invalidator.h" | |
24 #include "components/invalidation/impl/non_blocking_invalidator.h" | |
25 #include "components/invalidation/public/invalidation_handler.h" | |
26 #include "components/invalidation/public/invalidation_util.h" | |
27 #include "components/invalidation/public/object_id_invalidation_map.h" | |
28 #include "components/sync_driver/invalidation_helper.h" | |
29 #include "jingle/notifier/base/notification_method.h" | |
30 #include "jingle/notifier/base/notifier_options.h" | |
31 #include "net/base/host_port_pair.h" | |
32 #include "net/base/network_change_notifier.h" | |
33 #include "net/dns/host_resolver.h" | |
34 #include "net/http/transport_security_state.h" | |
35 #include "net/url_request/url_request_test_util.h" | |
36 #include "sync/internal_api/public/base/model_type.h" | |
37 #include "sync/tools/null_invalidation_state_tracker.h" | |
38 | |
39 #if defined(OS_MACOSX) | |
40 #include "base/mac/scoped_nsautorelease_pool.h" | |
41 #endif | |
42 | |
43 // This is a simple utility that initializes a sync notifier and | |
44 // listens to any received notifications. | |
45 | |
46 namespace syncer { | |
47 namespace { | |
48 | |
49 const char kEmailSwitch[] = "email"; | |
50 const char kTokenSwitch[] = "token"; | |
51 const char kHostPortSwitch[] = "host-port"; | |
52 const char kTrySslTcpFirstSwitch[] = "try-ssltcp-first"; | |
53 const char kAllowInsecureConnectionSwitch[] = "allow-insecure-connection"; | |
54 | |
55 // Class to print received notifications events. | |
56 class NotificationPrinter : public InvalidationHandler { | |
57 public: | |
58 NotificationPrinter() {} | |
59 ~NotificationPrinter() override {} | |
60 | |
61 void OnInvalidatorStateChange(InvalidatorState state) override { | |
62 LOG(INFO) << "Invalidator state changed to " | |
63 << InvalidatorStateToString(state); | |
64 } | |
65 | |
66 void OnIncomingInvalidation( | |
67 const ObjectIdInvalidationMap& invalidation_map) override { | |
68 ObjectIdSet ids = invalidation_map.GetObjectIds(); | |
69 for (ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); ++it) { | |
70 LOG(INFO) << "Remote invalidation: " | |
71 << invalidation_map.ToString(); | |
72 } | |
73 } | |
74 | |
75 std::string GetOwnerName() const override { return "NotificationPrinter"; } | |
76 | |
77 private: | |
78 DISALLOW_COPY_AND_ASSIGN(NotificationPrinter); | |
79 }; | |
80 | |
81 // Needed to use a real host resolver. | |
82 class MyTestURLRequestContext : public net::TestURLRequestContext { | |
83 public: | |
84 MyTestURLRequestContext() : TestURLRequestContext(true) { | |
85 context_storage_.set_host_resolver( | |
86 net::HostResolver::CreateDefaultResolver(NULL)); | |
87 context_storage_.set_transport_security_state( | |
88 base::WrapUnique(new net::TransportSecurityState())); | |
89 Init(); | |
90 } | |
91 | |
92 ~MyTestURLRequestContext() override {} | |
93 }; | |
94 | |
95 class MyTestURLRequestContextGetter : public net::TestURLRequestContextGetter { | |
96 public: | |
97 explicit MyTestURLRequestContextGetter( | |
98 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) | |
99 : TestURLRequestContextGetter(io_task_runner) {} | |
100 | |
101 net::TestURLRequestContext* GetURLRequestContext() override { | |
102 // Construct |context_| lazily so it gets constructed on the right | |
103 // thread (the IO thread). | |
104 if (!context_) | |
105 context_.reset(new MyTestURLRequestContext()); | |
106 return context_.get(); | |
107 } | |
108 | |
109 private: | |
110 ~MyTestURLRequestContextGetter() override {} | |
111 | |
112 std::unique_ptr<MyTestURLRequestContext> context_; | |
113 }; | |
114 | |
115 notifier::NotifierOptions ParseNotifierOptions( | |
116 const base::CommandLine& command_line, | |
117 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter) { | |
118 notifier::NotifierOptions notifier_options; | |
119 notifier_options.request_context_getter = request_context_getter; | |
120 | |
121 if (command_line.HasSwitch(kHostPortSwitch)) { | |
122 notifier_options.xmpp_host_port = | |
123 net::HostPortPair::FromString( | |
124 command_line.GetSwitchValueASCII(kHostPortSwitch)); | |
125 LOG(INFO) << "Using " << notifier_options.xmpp_host_port.ToString() | |
126 << " for test sync notification server."; | |
127 } | |
128 | |
129 notifier_options.try_ssltcp_first = | |
130 command_line.HasSwitch(kTrySslTcpFirstSwitch); | |
131 LOG_IF(INFO, notifier_options.try_ssltcp_first) | |
132 << "Trying SSL/TCP port before XMPP port for notifications."; | |
133 | |
134 notifier_options.allow_insecure_connection = | |
135 command_line.HasSwitch(kAllowInsecureConnectionSwitch); | |
136 LOG_IF(INFO, notifier_options.allow_insecure_connection) | |
137 << "Allowing insecure XMPP connections."; | |
138 | |
139 return notifier_options; | |
140 } | |
141 | |
142 int SyncListenNotificationsMain(int argc, char* argv[]) { | |
143 #if defined(OS_MACOSX) | |
144 base::mac::ScopedNSAutoreleasePool pool; | |
145 #endif | |
146 base::AtExitManager exit_manager; | |
147 base::CommandLine::Init(argc, argv); | |
148 logging::LoggingSettings settings; | |
149 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; | |
150 logging::InitLogging(settings); | |
151 | |
152 base::MessageLoop ui_loop; | |
153 base::Thread io_thread("IO thread"); | |
154 base::Thread::Options options; | |
155 options.message_loop_type = base::MessageLoop::TYPE_IO; | |
156 io_thread.StartWithOptions(options); | |
157 | |
158 // Parse command line. | |
159 const base::CommandLine& command_line = | |
160 *base::CommandLine::ForCurrentProcess(); | |
161 std::string email = command_line.GetSwitchValueASCII(kEmailSwitch); | |
162 std::string token = command_line.GetSwitchValueASCII(kTokenSwitch); | |
163 // TODO(akalin): Write a wrapper script that gets a token for an | |
164 // email and password and passes that in to this utility. | |
165 if (email.empty() || token.empty()) { | |
166 std::printf("Usage: %s --%s=foo@bar.com --%s=token\n" | |
167 "[--%s=host:port] [--%s] [--%s]\n" | |
168 "Run chrome and set a breakpoint on\n" | |
169 "syncer::SyncManagerImpl::UpdateCredentials() " | |
170 "after logging into\n" | |
171 "sync to get the token to pass into this utility.\n", | |
172 argv[0], | |
173 kEmailSwitch, kTokenSwitch, kHostPortSwitch, | |
174 kTrySslTcpFirstSwitch, kAllowInsecureConnectionSwitch); | |
175 return -1; | |
176 } | |
177 | |
178 // Set up objects that monitor the network. | |
179 std::unique_ptr<net::NetworkChangeNotifier> network_change_notifier( | |
180 net::NetworkChangeNotifier::Create()); | |
181 | |
182 const notifier::NotifierOptions& notifier_options = | |
183 ParseNotifierOptions( | |
184 command_line, | |
185 new MyTestURLRequestContextGetter(io_thread.task_runner())); | |
186 syncer::NetworkChannelCreator network_channel_creator = | |
187 syncer::NonBlockingInvalidator::MakePushClientChannelCreator( | |
188 notifier_options); | |
189 const char kClientInfo[] = "sync_listen_notifications"; | |
190 NullInvalidationStateTracker null_invalidation_state_tracker; | |
191 std::unique_ptr<Invalidator> invalidator(new NonBlockingInvalidator( | |
192 network_channel_creator, base::RandBytesAsString(8), | |
193 null_invalidation_state_tracker.GetSavedInvalidations(), | |
194 null_invalidation_state_tracker.GetBootstrapData(), | |
195 &null_invalidation_state_tracker, kClientInfo, | |
196 notifier_options.request_context_getter)); | |
197 | |
198 NotificationPrinter notification_printer; | |
199 | |
200 invalidator->UpdateCredentials(email, token); | |
201 | |
202 // Listen for notifications for all known types. | |
203 invalidator->RegisterHandler(¬ification_printer); | |
204 CHECK(invalidator->UpdateRegisteredIds( | |
205 ¬ification_printer, ModelTypeSetToObjectIdSet(ModelTypeSet::All()))); | |
206 | |
207 base::RunLoop().Run(); | |
208 | |
209 invalidator->UnregisterHandler(¬ification_printer); | |
210 io_thread.Stop(); | |
211 return 0; | |
212 } | |
213 | |
214 } // namespace | |
215 } // namespace syncer | |
216 | |
217 int main(int argc, char* argv[]) { | |
218 return syncer::SyncListenNotificationsMain(argc, argv); | |
219 } | |
OLD | NEW |