1// Copyright (c) 2011 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 "chrome/browser/sync/notifier/sync_notifier_factory.h"
6
7#include <string>
8
9#include "base/command_line.h"
10#include "base/string_number_conversions.h"
11#include "base/string_util.h"
12#include "chrome/browser/sync/notifier/non_blocking_invalidation_notifier.h"
13#include "chrome/browser/sync/notifier/p2p_notifier.h"
14#include "chrome/browser/sync/notifier/sync_notifier.h"
15#include "chrome/common/chrome_switches.h"
16#include "jingle/notifier/base/const_communicator.h"
17#include "jingle/notifier/base/notifier_options.h"
18#include "net/base/host_port_pair.h"
19
20namespace sync_notifier {
21namespace {
22
23// TODO(akalin): Figure out whether this should be a method of
24// HostPortPair.
25net::HostPortPair StringToHostPortPair(const std::string& host_port_str,
26                                       uint16 default_port) {
27  std::string::size_type colon_index = host_port_str.find(':');
28  if (colon_index == std::string::npos) {
29    return net::HostPortPair(host_port_str, default_port);
30  }
31
32  std::string host = host_port_str.substr(0, colon_index);
33  std::string port_str = host_port_str.substr(colon_index + 1);
34  int port = default_port;
35  if (!base::StringToInt(port_str, &port) ||
36      (port <= 0) || (port > kuint16max)) {
37    LOG(WARNING) << "Could not parse valid port from " << port_str
38                 << "; using port " << default_port;
39    return net::HostPortPair(host, default_port);
40  }
41
42  return net::HostPortPair(host, port);
43}
44
45SyncNotifier* CreateDefaultSyncNotifier(
46    const CommandLine& command_line,
47    const scoped_refptr<net::URLRequestContextGetter>& request_context_getter,
48    const std::string& client_info) {
49  // Contains options specific to how sync clients send and listen to
50  // jingle notifications.
51  notifier::NotifierOptions notifier_options;
52  notifier_options.request_context_getter = request_context_getter;
53
54  // Override the notification server host from the command-line, if provided.
55  if (command_line.HasSwitch(switches::kSyncNotificationHost)) {
56    std::string value(command_line.GetSwitchValueASCII(
57        switches::kSyncNotificationHost));
58    if (!value.empty()) {
59      notifier_options.xmpp_host_port =
60          StringToHostPortPair(value, notifier::kDefaultXmppPort);
61    }
62    VLOG(1) << "Using " << notifier_options.xmpp_host_port.ToString()
63            << " for test sync notification server.";
64  }
65
66  notifier_options.try_ssltcp_first =
67      command_line.HasSwitch(switches::kSyncTrySsltcpFirstForXmpp);
68  if (notifier_options.try_ssltcp_first)
69    VLOG(1) << "Trying SSL/TCP port before XMPP port for notifications.";
70
71  notifier_options.invalidate_xmpp_login =
72      command_line.HasSwitch(switches::kSyncInvalidateXmppLogin);
73  if (notifier_options.invalidate_xmpp_login) {
74    VLOG(1) << "Invalidating sync XMPP login.";
75  }
76
77  notifier_options.allow_insecure_connection =
78      command_line.HasSwitch(switches::kSyncAllowInsecureXmppConnection);
79  if (notifier_options.allow_insecure_connection) {
80    VLOG(1) << "Allowing insecure XMPP connections.";
81  }
82
83  if (command_line.HasSwitch(switches::kSyncNotificationMethod)) {
84    const std::string notification_method_str(
85        command_line.GetSwitchValueASCII(switches::kSyncNotificationMethod));
86    notifier_options.notification_method =
87        notifier::StringToNotificationMethod(notification_method_str);
88  }
89
90  if (notifier_options.notification_method == notifier::NOTIFICATION_P2P) {
91    return new P2PNotifier(notifier_options);
92  }
93
94  return new NonBlockingInvalidationNotifier(notifier_options, client_info);
95}
96}  // namespace
97
98SyncNotifierFactory::SyncNotifierFactory(const std::string& client_info)
99    : client_info_(client_info) {}
100
101SyncNotifierFactory::~SyncNotifierFactory() {
102}
103
104SyncNotifier* SyncNotifierFactory::CreateSyncNotifier(
105    const CommandLine& command_line,
106    const scoped_refptr<net::URLRequestContextGetter>& request_context_getter) {
107  return CreateDefaultSyncNotifier(command_line,
108                                   request_context_getter,
109                                   client_info_);
110}
111}  // namespace sync_notifier
112