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 "chrome/service/cloud_print/connector_settings.h"
6
7#include "base/metrics/histogram.h"
8#include "base/values.h"
9#include "chrome/common/cloud_print/cloud_print_constants.h"
10#include "chrome/common/pref_names.h"
11#include "chrome/service/cloud_print/print_system.h"
12#include "chrome/service/service_process_prefs.h"
13#include "components/cloud_devices/common/cloud_devices_urls.h"
14
15namespace {
16
17const char kDeleteOnEnumFail[] = "delete_on_enum_fail";
18const char kName[] = "name";
19const char kConnect[] = "connect";
20
21}  // namespace
22
23namespace cloud_print {
24
25ConnectorSettings::ConnectorSettings()
26    : delete_on_enum_fail_(false),
27      connect_new_printers_(true),
28      xmpp_ping_enabled_(false),
29      xmpp_ping_timeout_sec_(kDefaultXmppPingTimeoutSecs) {
30}
31
32ConnectorSettings::~ConnectorSettings() {
33}
34
35void ConnectorSettings::InitFrom(ServiceProcessPrefs* prefs) {
36  CopyFrom(ConnectorSettings());
37
38  proxy_id_ = prefs->GetString(prefs::kCloudPrintProxyId, std::string());
39  if (proxy_id_.empty()) {
40    proxy_id_ = PrintSystem::GenerateProxyId();
41    prefs->SetString(prefs::kCloudPrintProxyId, proxy_id_);
42    prefs->WritePrefs();
43  }
44
45  // Getting print system specific settings from the preferences.
46  const base::DictionaryValue* print_system_settings =
47      prefs->GetDictionary(prefs::kCloudPrintPrintSystemSettings);
48  if (print_system_settings) {
49    print_system_settings_.reset(print_system_settings->DeepCopy());
50    // TODO(vitalybuka) : Consider to rename and move out option from
51    // print_system_settings.
52    print_system_settings_->GetBoolean(kDeleteOnEnumFail,
53                                       &delete_on_enum_fail_);
54  }
55
56  // Check if there is an override for the cloud print server URL.
57  server_url_ = cloud_devices::GetCloudPrintURL();
58  DCHECK(server_url_.is_valid());
59
60  connect_new_printers_ = prefs->GetBoolean(
61      prefs::kCloudPrintConnectNewPrinters, true);
62
63  xmpp_ping_enabled_ = prefs->GetBoolean(
64      prefs::kCloudPrintXmppPingEnabled, false);
65  int timeout = prefs->GetInt(
66      prefs::kCloudPrintXmppPingTimeout, kDefaultXmppPingTimeoutSecs);
67  SetXmppPingTimeoutSec(timeout);
68  UMA_HISTOGRAM_LONG_TIMES(
69      "CloudPrint.XmppTimeout",
70      base::TimeDelta::FromSeconds(xmpp_ping_timeout_sec_));
71
72  const base::ListValue* printers = prefs->GetList(prefs::kCloudPrintPrinters);
73  if (printers) {
74    for (size_t i = 0; i < printers->GetSize(); ++i) {
75      const base::DictionaryValue* dictionary = NULL;
76      if (printers->GetDictionary(i, &dictionary) && dictionary) {
77        std::string name;
78        dictionary->GetString(kName, &name);
79        if (!name.empty()) {
80          bool connect = connect_new_printers_;
81          dictionary->GetBoolean(kConnect, &connect);
82          if (connect != connect_new_printers_)
83            printers_.insert(name);
84        }
85      }
86    }
87  }
88  if (connect_new_printers_) {
89    UMA_HISTOGRAM_COUNTS_10000("CloudPrint.PrinterBlacklistSize",
90                               printers_.size());
91  } else {
92    UMA_HISTOGRAM_COUNTS_10000("CloudPrint.PrinterWhitelistSize",
93                               printers_.size());
94  }
95}
96
97bool ConnectorSettings::ShouldConnect(const std::string& printer_name) const {
98  Printers::const_iterator printer = printers_.find(printer_name);
99  if (printer != printers_.end())
100    return !connect_new_printers_;
101  return connect_new_printers_;
102}
103
104void ConnectorSettings::CopyFrom(const ConnectorSettings& source) {
105  server_url_ = source.server_url();
106  proxy_id_ = source.proxy_id();
107  delete_on_enum_fail_ = source.delete_on_enum_fail();
108  connect_new_printers_ = source.connect_new_printers_;
109  xmpp_ping_enabled_ = source.xmpp_ping_enabled();
110  xmpp_ping_timeout_sec_ = source.xmpp_ping_timeout_sec();
111  printers_ = source.printers_;
112  if (source.print_system_settings())
113    print_system_settings_.reset(source.print_system_settings()->DeepCopy());
114}
115
116void ConnectorSettings::SetXmppPingTimeoutSec(int timeout) {
117  xmpp_ping_timeout_sec_ = timeout;
118  if (xmpp_ping_timeout_sec_ < kMinXmppPingTimeoutSecs) {
119    LOG(WARNING) <<
120        "CP_CONNECTOR: XMPP ping timeout is less then minimal value";
121    xmpp_ping_timeout_sec_ = kMinXmppPingTimeoutSecs;
122  }
123}
124
125}  // namespace cloud_print
126