1cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// found in the LICENSE file.
4a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
5cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/invalidation/invalidation_service_util.h"
6a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
7a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "base/base64.h"
8a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "base/command_line.h"
9a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "base/logging.h"
10a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "base/rand_util.h"
11cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/invalidation/invalidation_switches.h"
12a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
13a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)namespace invalidation {
14a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
15a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)notifier::NotifierOptions ParseNotifierOptions(
16a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    const CommandLine& command_line) {
17a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  notifier::NotifierOptions notifier_options;
18a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
19a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (command_line.HasSwitch(switches::kSyncNotificationHostPort)) {
20a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    notifier_options.xmpp_host_port =
21a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        net::HostPortPair::FromString(
22a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)            command_line.GetSwitchValueASCII(
23a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                switches::kSyncNotificationHostPort));
24a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    DVLOG(1) << "Using " << notifier_options.xmpp_host_port.ToString()
25a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)             << " for test sync notification server.";
26a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
27a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
28a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  notifier_options.allow_insecure_connection =
29a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      command_line.HasSwitch(switches::kSyncAllowInsecureXmppConnection);
30a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DVLOG_IF(1, notifier_options.allow_insecure_connection)
31a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      << "Allowing insecure XMPP connections.";
32a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
33a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return notifier_options;
34a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
35a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
36a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)std::string GenerateInvalidatorClientId() {
37a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // Generate a GUID with 128 bits worth of base64-encoded randomness.
38a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // This format is similar to that of sync's cache_guid.
39a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  const int kGuidBytes = 128 / 8;
40a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  std::string guid;
41a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  base::Base64Encode(base::RandBytesAsString(kGuidBytes), &guid);
42a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DCHECK(!guid.empty());
43a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return guid;
44a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
45a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
46a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}  // namespace invalidation
47