OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/sync/notifier/sync_notifier_factory.h" | 5 #include "chrome/browser/sync/notifier/sync_notifier_factory.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/command_line.h" | |
10 #include "base/logging.h" | 9 #include "base/logging.h" |
11 #include "base/string_number_conversions.h" | |
12 #include "base/string_util.h" | |
13 #include "chrome/browser/sync/notifier/non_blocking_invalidation_notifier.h" | 10 #include "chrome/browser/sync/notifier/non_blocking_invalidation_notifier.h" |
14 #include "chrome/browser/sync/notifier/p2p_notifier.h" | 11 #include "chrome/browser/sync/notifier/p2p_notifier.h" |
15 #include "chrome/browser/sync/notifier/sync_notifier.h" | 12 #include "chrome/browser/sync/notifier/sync_notifier.h" |
16 #include "chrome/common/chrome_switches.h" | |
17 #include "jingle/notifier/base/const_communicator.h" | |
18 #include "jingle/notifier/base/notifier_options.h" | |
19 #include "jingle/notifier/listener/mediator_thread_impl.h" | 13 #include "jingle/notifier/listener/mediator_thread_impl.h" |
20 #include "jingle/notifier/listener/talk_mediator_impl.h" | 14 #include "jingle/notifier/listener/talk_mediator_impl.h" |
21 #include "net/base/host_port_pair.h" | |
22 | 15 |
23 namespace sync_notifier { | 16 namespace sync_notifier { |
24 namespace { | 17 namespace { |
25 | 18 |
26 // TODO(akalin): Figure out whether this should be a method of | |
27 // HostPortPair. | |
28 net::HostPortPair StringToHostPortPair(const std::string& host_port_str, | |
29 uint16 default_port) { | |
30 std::string::size_type colon_index = host_port_str.find(':'); | |
31 if (colon_index == std::string::npos) { | |
32 return net::HostPortPair(host_port_str, default_port); | |
33 } | |
34 | |
35 std::string host = host_port_str.substr(0, colon_index); | |
36 std::string port_str = host_port_str.substr(colon_index + 1); | |
37 int port = default_port; | |
38 if (!base::StringToInt(port_str, &port) || | |
39 (port <= 0) || (port > kuint16max)) { | |
40 LOG(WARNING) << "Could not parse valid port from " << port_str | |
41 << "; using port " << default_port; | |
42 return net::HostPortPair(host, default_port); | |
43 } | |
44 | |
45 return net::HostPortPair(host, port); | |
46 } | |
47 | |
48 SyncNotifier* CreateDefaultSyncNotifier( | 19 SyncNotifier* CreateDefaultSyncNotifier( |
49 const CommandLine& command_line, | 20 const notifier::NotifierOptions& notifier_options, |
50 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, | |
51 const InvalidationVersionMap& initial_max_invalidation_versions, | 21 const InvalidationVersionMap& initial_max_invalidation_versions, |
52 const browser_sync::WeakHandle<InvalidationVersionTracker>& | 22 const browser_sync::WeakHandle<InvalidationVersionTracker>& |
53 invalidation_version_tracker, | 23 invalidation_version_tracker, |
54 const std::string& client_info) { | 24 const std::string& client_info) { |
55 // Contains options specific to how sync clients send and listen to | |
56 // jingle notifications. | |
57 notifier::NotifierOptions notifier_options; | |
58 notifier_options.request_context_getter = request_context_getter; | |
59 | |
60 // Override the notification server host from the command-line, if provided. | |
61 if (command_line.HasSwitch(switches::kSyncNotificationHost)) { | |
62 std::string value(command_line.GetSwitchValueASCII( | |
63 switches::kSyncNotificationHost)); | |
64 if (!value.empty()) { | |
65 notifier_options.xmpp_host_port = | |
66 StringToHostPortPair(value, notifier::kDefaultXmppPort); | |
67 } | |
68 DVLOG(1) << "Using " << notifier_options.xmpp_host_port.ToString() | |
69 << " for test sync notification server."; | |
70 } | |
71 | |
72 notifier_options.try_ssltcp_first = | |
73 command_line.HasSwitch(switches::kSyncTrySsltcpFirstForXmpp); | |
74 if (notifier_options.try_ssltcp_first) | |
75 DVLOG(1) << "Trying SSL/TCP port before XMPP port for notifications."; | |
76 | |
77 notifier_options.invalidate_xmpp_login = | |
78 command_line.HasSwitch(switches::kSyncInvalidateXmppLogin); | |
79 if (notifier_options.invalidate_xmpp_login) { | |
80 DVLOG(1) << "Invalidating sync XMPP login."; | |
81 } | |
82 | |
83 notifier_options.allow_insecure_connection = | |
84 command_line.HasSwitch(switches::kSyncAllowInsecureXmppConnection); | |
85 if (notifier_options.allow_insecure_connection) { | |
86 DVLOG(1) << "Allowing insecure XMPP connections."; | |
87 } | |
88 | |
89 if (command_line.HasSwitch(switches::kSyncNotificationMethod)) { | |
90 const std::string notification_method_str( | |
91 command_line.GetSwitchValueASCII(switches::kSyncNotificationMethod)); | |
92 notifier_options.notification_method = | |
93 notifier::StringToNotificationMethod(notification_method_str); | |
94 } | |
95 | |
96 if (notifier_options.notification_method == notifier::NOTIFICATION_P2P) { | 25 if (notifier_options.notification_method == notifier::NOTIFICATION_P2P) { |
97 notifier::TalkMediator* const talk_mediator = | 26 notifier::TalkMediator* const talk_mediator = |
98 new notifier::TalkMediatorImpl( | 27 new notifier::TalkMediatorImpl( |
99 new notifier::MediatorThreadImpl(notifier_options), | 28 new notifier::MediatorThreadImpl(notifier_options), |
100 notifier_options); | 29 notifier_options); |
101 // TODO(rlarocque): Ideally, the notification target would be | 30 // TODO(rlarocque): Ideally, the notification target would be |
102 // NOTIFY_OTHERS. There's no good reason to notify ourselves of our own | 31 // NOTIFY_OTHERS. There's no good reason to notify ourselves of our own |
103 // commits. We self-notify for now only because the integration tests rely | 32 // commits. We self-notify for now only because the integration tests rely |
104 // on this behaviour. See crbug.com/97780. | 33 // on this behaviour. See crbug.com/97780. |
105 // | 34 // |
106 // Takes ownership of |talk_mediator|. | 35 // Takes ownership of |talk_mediator|. |
107 return new P2PNotifier(talk_mediator, NOTIFY_ALL); | 36 return new P2PNotifier(talk_mediator, NOTIFY_ALL); |
108 } | 37 } |
109 | 38 |
110 return new NonBlockingInvalidationNotifier( | 39 return new NonBlockingInvalidationNotifier( |
111 notifier_options, initial_max_invalidation_versions, | 40 notifier_options, initial_max_invalidation_versions, |
112 invalidation_version_tracker, client_info); | 41 invalidation_version_tracker, client_info); |
113 } | 42 } |
114 | 43 |
115 } // namespace | 44 } // namespace |
116 | 45 |
117 SyncNotifierFactory::SyncNotifierFactory( | 46 SyncNotifierFactory::SyncNotifierFactory( |
| 47 const notifier::NotifierOptions& notifier_options, |
118 const std::string& client_info, | 48 const std::string& client_info, |
119 const scoped_refptr<net::URLRequestContextGetter>& | |
120 request_context_getter, | |
121 const base::WeakPtr<InvalidationVersionTracker>& | 49 const base::WeakPtr<InvalidationVersionTracker>& |
122 invalidation_version_tracker, | 50 invalidation_version_tracker) |
123 const CommandLine& command_line) | 51 : notifier_options_(notifier_options), |
124 : client_info_(client_info), | 52 client_info_(client_info), |
125 request_context_getter_(request_context_getter), | |
126 initial_max_invalidation_versions_( | 53 initial_max_invalidation_versions_( |
127 invalidation_version_tracker.get() ? | 54 invalidation_version_tracker.get() ? |
128 invalidation_version_tracker->GetAllMaxVersions() : | 55 invalidation_version_tracker->GetAllMaxVersions() : |
129 InvalidationVersionMap()), | 56 InvalidationVersionMap()), |
130 invalidation_version_tracker_(invalidation_version_tracker), | 57 invalidation_version_tracker_(invalidation_version_tracker) { |
131 command_line_(command_line) { | |
132 } | 58 } |
133 | 59 |
134 SyncNotifierFactory::~SyncNotifierFactory() { | 60 SyncNotifierFactory::~SyncNotifierFactory() { |
135 } | 61 } |
136 | 62 |
137 SyncNotifier* SyncNotifierFactory::CreateSyncNotifier() { | 63 SyncNotifier* SyncNotifierFactory::CreateSyncNotifier() { |
138 return CreateDefaultSyncNotifier(command_line_, | 64 return CreateDefaultSyncNotifier(notifier_options_, |
139 request_context_getter_, | |
140 initial_max_invalidation_versions_, | 65 initial_max_invalidation_versions_, |
141 invalidation_version_tracker_, | 66 invalidation_version_tracker_, |
142 client_info_); | 67 client_info_); |
143 } | 68 } |
144 } // namespace sync_notifier | 69 } // namespace sync_notifier |
OLD | NEW |