| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service.h" | 5 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service.h" |
| 6 | 6 |
| 7 #include <stack> | 7 #include <stack> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 CloudPrintProxyService::CloudPrintProxyService(Profile* profile) | 38 CloudPrintProxyService::CloudPrintProxyService(Profile* profile) |
| 39 : profile_(profile), | 39 : profile_(profile), |
| 40 weak_factory_(this), | 40 weak_factory_(this), |
| 41 enforcing_connector_policy_(false) { | 41 enforcing_connector_policy_(false) { |
| 42 } | 42 } |
| 43 | 43 |
| 44 CloudPrintProxyService::~CloudPrintProxyService() { | 44 CloudPrintProxyService::~CloudPrintProxyService() { |
| 45 } | 45 } |
| 46 | 46 |
| 47 void CloudPrintProxyService::Initialize() { | 47 void CloudPrintProxyService::Initialize() { |
| 48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 48 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents", | 49 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents", |
| 49 ServiceProcessControl::SERVICE_EVENT_INITIALIZE, | 50 ServiceProcessControl::SERVICE_EVENT_INITIALIZE, |
| 50 ServiceProcessControl::SERVICE_EVENT_MAX); | 51 ServiceProcessControl::SERVICE_EVENT_MAX); |
| 51 if (profile_->GetPrefs()->HasPrefPath(prefs::kCloudPrintEmail) && | 52 if (profile_->GetPrefs()->HasPrefPath(prefs::kCloudPrintEmail) && |
| 52 (!profile_->GetPrefs()->GetString(prefs::kCloudPrintEmail).empty() || | 53 (!profile_->GetPrefs()->GetString(prefs::kCloudPrintEmail).empty() || |
| 53 !profile_->GetPrefs()->GetBoolean(prefs::kCloudPrintProxyEnabled))) { | 54 !profile_->GetPrefs()->GetBoolean(prefs::kCloudPrintProxyEnabled))) { |
| 54 // If the cloud print proxy is enabled, or the policy preventing it from | 55 // If the cloud print proxy is enabled, or the policy preventing it from |
| 55 // being enabled is set, establish a channel with the service process and | 56 // being enabled is set, establish a channel with the service process and |
| 56 // update the status. This will check the policy when the status is sent | 57 // update the status. This will check the policy when the status is sent |
| 57 // back. | 58 // back. |
| 58 UMA_HISTOGRAM_ENUMERATION( | 59 UMA_HISTOGRAM_ENUMERATION( |
| 59 "CloudPrint.ServiceEvents", | 60 "CloudPrint.ServiceEvents", |
| 60 ServiceProcessControl::SERVICE_EVENT_ENABLED_ON_LAUNCH, | 61 ServiceProcessControl::SERVICE_EVENT_ENABLED_ON_LAUNCH, |
| 61 ServiceProcessControl::SERVICE_EVENT_MAX); | 62 ServiceProcessControl::SERVICE_EVENT_MAX); |
| 62 RefreshStatusFromService(); | 63 RefreshStatusFromService(); |
| 63 } | 64 } |
| 64 | 65 |
| 65 pref_change_registrar_.Init(profile_->GetPrefs()); | 66 pref_change_registrar_.Init(profile_->GetPrefs()); |
| 66 pref_change_registrar_.Add( | 67 pref_change_registrar_.Add( |
| 67 prefs::kCloudPrintProxyEnabled, | 68 prefs::kCloudPrintProxyEnabled, |
| 68 base::Bind( | 69 base::Bind( |
| 69 base::IgnoreResult( | 70 base::IgnoreResult( |
| 70 &CloudPrintProxyService::ApplyCloudPrintConnectorPolicy), | 71 &CloudPrintProxyService::ApplyCloudPrintConnectorPolicy), |
| 71 base::Unretained(this))); | 72 base::Unretained(this))); |
| 72 } | 73 } |
| 73 | 74 |
| 74 void CloudPrintProxyService::RefreshStatusFromService() { | 75 void CloudPrintProxyService::RefreshStatusFromService() { |
| 76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 75 InvokeServiceTask( | 77 InvokeServiceTask( |
| 76 base::Bind(&CloudPrintProxyService::RefreshCloudPrintProxyStatus, | 78 base::Bind(&CloudPrintProxyService::RefreshCloudPrintProxyStatus, |
| 77 weak_factory_.GetWeakPtr())); | 79 weak_factory_.GetWeakPtr())); |
| 78 } | 80 } |
| 79 | 81 |
| 80 bool CloudPrintProxyService::EnforceCloudPrintConnectorPolicyAndQuit() { | 82 bool CloudPrintProxyService::EnforceCloudPrintConnectorPolicyAndQuit() { |
| 81 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 82 enforcing_connector_policy_ = true; | 84 enforcing_connector_policy_ = true; |
| 83 if (ApplyCloudPrintConnectorPolicy()) | 85 if (ApplyCloudPrintConnectorPolicy()) |
| 84 return true; | 86 return true; |
| 85 return false; | 87 return false; |
| 86 } | 88 } |
| 87 | 89 |
| 88 void CloudPrintProxyService::EnableForUserWithRobot( | 90 void CloudPrintProxyService::EnableForUserWithRobot( |
| 89 const std::string& robot_auth_code, | 91 const std::string& robot_auth_code, |
| 90 const std::string& robot_email, | 92 const std::string& robot_email, |
| 91 const std::string& user_email, | 93 const std::string& user_email, |
| 92 const base::DictionaryValue& user_preferences) { | 94 const base::DictionaryValue& user_preferences) { |
| 95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 93 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents", | 96 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents", |
| 94 ServiceProcessControl::SERVICE_EVENT_ENABLE, | 97 ServiceProcessControl::SERVICE_EVENT_ENABLE, |
| 95 ServiceProcessControl::SERVICE_EVENT_MAX); | 98 ServiceProcessControl::SERVICE_EVENT_MAX); |
| 96 if (profile_->GetPrefs()->GetBoolean(prefs::kCloudPrintProxyEnabled)) { | 99 if (profile_->GetPrefs()->GetBoolean(prefs::kCloudPrintProxyEnabled)) { |
| 97 InvokeServiceTask( | 100 InvokeServiceTask( |
| 98 base::Bind(&CloudPrintProxyService::EnableCloudPrintProxyWithRobot, | 101 base::Bind(&CloudPrintProxyService::EnableCloudPrintProxyWithRobot, |
| 99 weak_factory_.GetWeakPtr(), robot_auth_code, robot_email, | 102 weak_factory_.GetWeakPtr(), robot_auth_code, robot_email, |
| 100 user_email, base::Owned(user_preferences.DeepCopy()))); | 103 user_email, base::Owned(user_preferences.DeepCopy()))); |
| 101 } | 104 } |
| 102 } | 105 } |
| 103 | 106 |
| 104 void CloudPrintProxyService::DisableForUser() { | 107 void CloudPrintProxyService::DisableForUser() { |
| 108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 105 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents", | 109 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents", |
| 106 ServiceProcessControl::SERVICE_EVENT_DISABLE, | 110 ServiceProcessControl::SERVICE_EVENT_DISABLE, |
| 107 ServiceProcessControl::SERVICE_EVENT_MAX); | 111 ServiceProcessControl::SERVICE_EVENT_MAX); |
| 108 InvokeServiceTask( | 112 InvokeServiceTask( |
| 109 base::Bind(&CloudPrintProxyService::DisableCloudPrintProxy, | 113 base::Bind(&CloudPrintProxyService::DisableCloudPrintProxy, |
| 110 weak_factory_.GetWeakPtr())); | 114 weak_factory_.GetWeakPtr())); |
| 111 } | 115 } |
| 112 | 116 |
| 113 bool CloudPrintProxyService::ApplyCloudPrintConnectorPolicy() { | 117 bool CloudPrintProxyService::ApplyCloudPrintConnectorPolicy() { |
| 118 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 114 if (!profile_->GetPrefs()->GetBoolean(prefs::kCloudPrintProxyEnabled)) { | 119 if (!profile_->GetPrefs()->GetBoolean(prefs::kCloudPrintProxyEnabled)) { |
| 115 std::string email = | 120 std::string email = |
| 116 profile_->GetPrefs()->GetString(prefs::kCloudPrintEmail); | 121 profile_->GetPrefs()->GetString(prefs::kCloudPrintEmail); |
| 117 if (!email.empty()) { | 122 if (!email.empty()) { |
| 118 UMA_HISTOGRAM_ENUMERATION( | 123 UMA_HISTOGRAM_ENUMERATION( |
| 119 "CloudPrint.ServiceEvents", | 124 "CloudPrint.ServiceEvents", |
| 120 ServiceProcessControl::SERVICE_EVENT_DISABLE_BY_POLICY, | 125 ServiceProcessControl::SERVICE_EVENT_DISABLE_BY_POLICY, |
| 121 ServiceProcessControl::SERVICE_EVENT_MAX); | 126 ServiceProcessControl::SERVICE_EVENT_MAX); |
| 122 DisableForUser(); | 127 DisableForUser(); |
| 123 profile_->GetPrefs()->SetString(prefs::kCloudPrintEmail, std::string()); | 128 profile_->GetPrefs()->SetString(prefs::kCloudPrintEmail, std::string()); |
| 124 if (enforcing_connector_policy_) { | 129 if (enforcing_connector_policy_) { |
| 125 base::MessageLoop::current()->PostTask( | 130 base::MessageLoop::current()->PostTask( |
| 126 FROM_HERE, | 131 FROM_HERE, |
| 127 base::Bind(&CloudPrintProxyService::RefreshCloudPrintProxyStatus, | 132 base::Bind(&CloudPrintProxyService::RefreshCloudPrintProxyStatus, |
| 128 weak_factory_.GetWeakPtr())); | 133 weak_factory_.GetWeakPtr())); |
| 129 } | 134 } |
| 130 return false; | 135 return false; |
| 131 } else if (enforcing_connector_policy_) { | 136 } else if (enforcing_connector_policy_) { |
| 132 base::MessageLoop::current()->PostTask(FROM_HERE, | 137 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 133 base::MessageLoop::QuitClosure()); | 138 base::MessageLoop::QuitClosure()); |
| 134 } | 139 } |
| 135 } | 140 } |
| 136 return true; | 141 return true; |
| 137 } | 142 } |
| 138 | 143 |
| 139 void CloudPrintProxyService::GetPrintersAvalibleForRegistration( | 144 void CloudPrintProxyService::GetPrinters(const PrintersCallback& callback) { |
| 140 std::vector<std::string>* printers) { | 145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 146 if (!profile_->GetPrefs()->GetBoolean(prefs::kCloudPrintProxyEnabled)) |
| 147 return; |
| 148 |
| 141 base::FilePath list_path( | 149 base::FilePath list_path( |
| 142 CommandLine::ForCurrentProcess()->GetSwitchValuePath( | 150 CommandLine::ForCurrentProcess()->GetSwitchValuePath( |
| 143 switches::kCloudPrintSetupProxy)); | 151 switches::kCloudPrintSetupProxy)); |
| 144 if (!list_path.empty()) { | 152 if (!list_path.empty()) { |
| 145 std::string printers_json; | 153 std::string printers_json; |
| 146 base::ReadFileToString(list_path, &printers_json); | 154 base::ReadFileToString(list_path, &printers_json); |
| 147 scoped_ptr<base::Value> value(base::JSONReader::Read(printers_json)); | 155 scoped_ptr<base::Value> value(base::JSONReader::Read(printers_json)); |
| 148 base::ListValue* list = NULL; | 156 base::ListValue* list = NULL; |
| 157 std::vector<std::string> printers; |
| 149 if (value && value->GetAsList(&list) && list) { | 158 if (value && value->GetAsList(&list) && list) { |
| 150 for (size_t i = 0; i < list->GetSize(); ++i) { | 159 for (size_t i = 0; i < list->GetSize(); ++i) { |
| 151 std::string printer; | 160 std::string printer; |
| 152 if (list->GetString(i, &printer)) | 161 if (list->GetString(i, &printer)) |
| 153 printers->push_back(printer); | 162 printers.push_back(printer); |
| 154 } | 163 } |
| 155 } | 164 } |
| 156 UMA_HISTOGRAM_COUNTS_10000("CloudPrint.AvailablePrintersList", | 165 UMA_HISTOGRAM_COUNTS_10000("CloudPrint.AvailablePrintersList", |
| 157 printers->size()); | 166 printers.size()); |
| 167 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 168 base::Bind(callback, printers)); |
| 158 } else { | 169 } else { |
| 159 printing::PrinterList printer_list; | 170 InvokeServiceTask( |
| 160 scoped_refptr<printing::PrintBackend> backend( | 171 base::Bind(&CloudPrintProxyService::GetCloudPrintProxyPrinters, |
| 161 printing::PrintBackend::CreateInstance(NULL)); | 172 weak_factory_.GetWeakPtr(), |
| 162 if (backend.get()) | 173 callback)); |
| 163 backend->EnumeratePrinters(&printer_list); | |
| 164 for (size_t i = 0; i < printer_list.size(); ++i) | |
| 165 printers->push_back(printer_list[i].printer_name); | |
| 166 UMA_HISTOGRAM_COUNTS_10000("CloudPrint.AvailablePrinters", | |
| 167 printers->size()); | |
| 168 } | 174 } |
| 169 } | 175 } |
| 170 | 176 |
| 177 void CloudPrintProxyService::GetCloudPrintProxyPrinters( |
| 178 const PrintersCallback& callback) { |
| 179 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 180 ServiceProcessControl* process_control = GetServiceProcessControl(); |
| 181 DCHECK(process_control->IsConnected()); |
| 182 process_control->GetPrinters(callback); |
| 183 } |
| 184 |
| 171 void CloudPrintProxyService::RefreshCloudPrintProxyStatus() { | 185 void CloudPrintProxyService::RefreshCloudPrintProxyStatus() { |
| 172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 186 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 173 ServiceProcessControl* process_control = GetServiceProcessControl(); | 187 ServiceProcessControl* process_control = GetServiceProcessControl(); |
| 174 DCHECK(process_control->IsConnected()); | 188 DCHECK(process_control->IsConnected()); |
| 175 ServiceProcessControl::CloudPrintProxyInfoHandler callback = | 189 ServiceProcessControl::CloudPrintProxyInfoCallback callback = base::Bind( |
| 176 base::Bind(&CloudPrintProxyService::ProxyInfoCallback, | 190 &CloudPrintProxyService::ProxyInfoCallback, base::Unretained(this)); |
| 177 base::Unretained(this)); | |
| 178 // GetCloudPrintProxyInfo takes ownership of callback. | |
| 179 process_control->GetCloudPrintProxyInfo(callback); | 191 process_control->GetCloudPrintProxyInfo(callback); |
| 180 } | 192 } |
| 181 | 193 |
| 182 void CloudPrintProxyService::EnableCloudPrintProxyWithRobot( | 194 void CloudPrintProxyService::EnableCloudPrintProxyWithRobot( |
| 183 const std::string& robot_auth_code, | 195 const std::string& robot_auth_code, |
| 184 const std::string& robot_email, | 196 const std::string& robot_email, |
| 185 const std::string& user_email, | 197 const std::string& user_email, |
| 186 const base::DictionaryValue* user_preferences) { | 198 const base::DictionaryValue* user_preferences) { |
| 187 ServiceProcessControl* process_control = GetServiceProcessControl(); | 199 ServiceProcessControl* process_control = GetServiceProcessControl(); |
| 188 DCHECK(process_control->IsConnected()); | 200 DCHECK(process_control->IsConnected()); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 211 } | 223 } |
| 212 | 224 |
| 213 bool CloudPrintProxyService::InvokeServiceTask(const base::Closure& task) { | 225 bool CloudPrintProxyService::InvokeServiceTask(const base::Closure& task) { |
| 214 GetServiceProcessControl()->Launch(task, base::Closure()); | 226 GetServiceProcessControl()->Launch(task, base::Closure()); |
| 215 return true; | 227 return true; |
| 216 } | 228 } |
| 217 | 229 |
| 218 ServiceProcessControl* CloudPrintProxyService::GetServiceProcessControl() { | 230 ServiceProcessControl* CloudPrintProxyService::GetServiceProcessControl() { |
| 219 return ServiceProcessControl::GetInstance(); | 231 return ServiceProcessControl::GetInstance(); |
| 220 } | 232 } |
| OLD | NEW |