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.
5 #include "chrome/service/cloud_print/connector_settings.h"
7 #include "base/command_line.h"
8 #include "base/metrics/histogram.h"
9 #include "base/values.h"
10 #include "chrome/common/chrome_switches.h"
11 #include "chrome/common/cloud_print/cloud_print_constants.h"
12 #include "chrome/common/pref_names.h"
13 #include "chrome/service/cloud_print/print_system.h"
14 #include "chrome/service/service_process_prefs.h"
18 const char kDefaultCloudPrintServerUrl
[] = "https://www.google.com/cloudprint";
19 const char kDeleteOnEnumFail
[] = "delete_on_enum_fail";
20 const char kName
[] = "name";
21 const char kConnect
[] = "connect";
25 namespace cloud_print
{
27 ConnectorSettings::ConnectorSettings()
28 : delete_on_enum_fail_(false),
29 connect_new_printers_(true),
30 xmpp_ping_enabled_(false),
31 xmpp_ping_timeout_sec_(kDefaultXmppPingTimeoutSecs
) {
34 ConnectorSettings::~ConnectorSettings() {
37 void ConnectorSettings::InitFrom(ServiceProcessPrefs
* prefs
) {
38 CopyFrom(ConnectorSettings());
40 proxy_id_
= prefs
->GetString(prefs::kCloudPrintProxyId
, std::string());
41 if (proxy_id_
.empty()) {
42 proxy_id_
= PrintSystem::GenerateProxyId();
43 prefs
->SetString(prefs::kCloudPrintProxyId
, proxy_id_
);
47 // Getting print system specific settings from the preferences.
48 const base::DictionaryValue
* print_system_settings
=
49 prefs
->GetDictionary(prefs::kCloudPrintPrintSystemSettings
);
50 if (print_system_settings
) {
51 print_system_settings_
.reset(print_system_settings
->DeepCopy());
52 // TODO(vitalybuka) : Consider to rename and move out option from
53 // print_system_settings.
54 print_system_settings_
->GetBoolean(kDeleteOnEnumFail
,
55 &delete_on_enum_fail_
);
58 // Check if there is an override for the cloud print server URL.
59 const CommandLine
& command_line
= *CommandLine::ForCurrentProcess();
61 GURL(command_line
.GetSwitchValueASCII(switches::kCloudPrintServiceURL
));
62 if (server_url_
.is_empty() || !server_url_
.is_valid()) {
64 GURL(prefs
->GetString(prefs::kCloudPrintServiceURL
, std::string()));
65 DCHECK(server_url_
.is_empty() || server_url_
.is_valid());
66 if (server_url_
.is_empty() || !server_url_
.is_valid()) {
67 server_url_
= GURL(kDefaultCloudPrintServerUrl
);
70 DCHECK(server_url_
.is_valid());
72 connect_new_printers_
= prefs
->GetBoolean(
73 prefs::kCloudPrintConnectNewPrinters
, true);
75 xmpp_ping_enabled_
= prefs
->GetBoolean(
76 prefs::kCloudPrintXmppPingEnabled
, false);
77 int timeout
= prefs
->GetInt(
78 prefs::kCloudPrintXmppPingTimeout
, kDefaultXmppPingTimeoutSecs
);
79 SetXmppPingTimeoutSec(timeout
);
80 UMA_HISTOGRAM_LONG_TIMES(
81 "CloudPrint.XmppTimeout",
82 base::TimeDelta::FromSeconds(xmpp_ping_timeout_sec_
));
84 const base::ListValue
* printers
= prefs
->GetList(prefs::kCloudPrintPrinters
);
86 for (size_t i
= 0; i
< printers
->GetSize(); ++i
) {
87 const base::DictionaryValue
* dictionary
= NULL
;
88 if (printers
->GetDictionary(i
, &dictionary
) && dictionary
) {
90 dictionary
->GetString(kName
, &name
);
92 bool connect
= connect_new_printers_
;
93 dictionary
->GetBoolean(kConnect
, &connect
);
94 if (connect
!= connect_new_printers_
)
95 printers_
.insert(name
);
100 if (connect_new_printers_
) {
101 UMA_HISTOGRAM_COUNTS_10000("CloudPrint.PrinterBlacklistSize",
104 UMA_HISTOGRAM_COUNTS_10000("CloudPrint.PrinterWhitelistSize",
109 bool ConnectorSettings::ShouldConnect(const std::string
& printer_name
) const {
110 Printers::const_iterator printer
= printers_
.find(printer_name
);
111 if (printer
!= printers_
.end())
112 return !connect_new_printers_
;
113 return connect_new_printers_
;
116 void ConnectorSettings::CopyFrom(const ConnectorSettings
& source
) {
117 server_url_
= source
.server_url();
118 proxy_id_
= source
.proxy_id();
119 delete_on_enum_fail_
= source
.delete_on_enum_fail();
120 connect_new_printers_
= source
.connect_new_printers_
;
121 xmpp_ping_enabled_
= source
.xmpp_ping_enabled();
122 xmpp_ping_timeout_sec_
= source
.xmpp_ping_timeout_sec();
123 printers_
= source
.printers_
;
124 if (source
.print_system_settings())
125 print_system_settings_
.reset(source
.print_system_settings()->DeepCopy());
128 void ConnectorSettings::SetXmppPingTimeoutSec(int timeout
) {
129 xmpp_ping_timeout_sec_
= timeout
;
130 if (xmpp_ping_timeout_sec_
< kMinXmppPingTimeoutSecs
) {
132 "CP_CONNECTOR: XMPP ping timeout is less then minimal value";
133 xmpp_ping_timeout_sec_
= kMinXmppPingTimeoutSecs
;
137 } // namespace cloud_print