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/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" 15 16 namespace { 17 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"; 22 23 } // namespace 24 25 namespace cloud_print { 26 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) { 32 } 33 34 ConnectorSettings::~ConnectorSettings() { 35 } 36 37 void ConnectorSettings::InitFrom(ServiceProcessPrefs* prefs) { 38 CopyFrom(ConnectorSettings()); 39 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_); 44 prefs->WritePrefs(); 45 } 46 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_); 56 } 57 58 // Check if there is an override for the cloud print server URL. 59 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); 60 server_url_ = 61 GURL(command_line.GetSwitchValueASCII(switches::kCloudPrintServiceURL)); 62 if (server_url_.is_empty() || !server_url_.is_valid()) { 63 server_url_ = 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); 68 } 69 } 70 DCHECK(server_url_.is_valid()); 71 72 connect_new_printers_ = prefs->GetBoolean( 73 prefs::kCloudPrintConnectNewPrinters, true); 74 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_)); 83 84 const base::ListValue* printers = prefs->GetList(prefs::kCloudPrintPrinters); 85 if (printers) { 86 for (size_t i = 0; i < printers->GetSize(); ++i) { 87 const base::DictionaryValue* dictionary = NULL; 88 if (printers->GetDictionary(i, &dictionary) && dictionary) { 89 std::string name; 90 dictionary->GetString(kName, &name); 91 if (!name.empty()) { 92 bool connect = connect_new_printers_; 93 dictionary->GetBoolean(kConnect, &connect); 94 if (connect != connect_new_printers_) 95 printers_.insert(name); 96 } 97 } 98 } 99 } 100 if (connect_new_printers_) { 101 UMA_HISTOGRAM_COUNTS_10000("CloudPrint.PrinterBlacklistSize", 102 printers_.size()); 103 } else { 104 UMA_HISTOGRAM_COUNTS_10000("CloudPrint.PrinterWhitelistSize", 105 printers_.size()); 106 } 107 } 108 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_; 114 } 115 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()); 126 } 127 128 void ConnectorSettings::SetXmppPingTimeoutSec(int timeout) { 129 xmpp_ping_timeout_sec_ = timeout; 130 if (xmpp_ping_timeout_sec_ < kMinXmppPingTimeoutSecs) { 131 LOG(WARNING) << 132 "CP_CONNECTOR: XMPP ping timeout is less then minimal value"; 133 xmpp_ping_timeout_sec_ = kMinXmppPingTimeoutSecs; 134 } 135 } 136 137 } // namespace cloud_print 138