| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/service/cloud_print/cloud_print_proxy.h" | 5 #include "chrome/service/cloud_print/cloud_print_proxy.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "base/process_util.h" | 9 #include "base/process_util.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "chrome/common/chrome_switches.h" | 11 #include "chrome/common/chrome_switches.h" |
| 12 #include "chrome/common/pref_names.h" | 12 #include "chrome/common/pref_names.h" |
| 13 #include "chrome/common/json_pref_store.h" | |
| 14 #include "chrome/service/cloud_print/cloud_print_consts.h" | 13 #include "chrome/service/cloud_print/cloud_print_consts.h" |
| 15 #include "chrome/service/cloud_print/print_system.h" | 14 #include "chrome/service/cloud_print/print_system.h" |
| 15 #include "chrome/service/service_prefs.h" |
| 16 #include "chrome/service/service_process.h" | 16 #include "chrome/service/service_process.h" |
| 17 | 17 |
| 18 // This method is invoked on the IO thread to launch the browser process to | 18 // This method is invoked on the IO thread to launch the browser process to |
| 19 // display a desktop notification that the Cloud Print token is invalid and | 19 // display a desktop notification that the Cloud Print token is invalid and |
| 20 // needs re-authentication. | 20 // needs re-authentication. |
| 21 static void ShowTokenExpiredNotificationInBrowser() { | 21 static void ShowTokenExpiredNotificationInBrowser() { |
| 22 DCHECK(g_service_process->io_thread()->message_loop_proxy()-> | 22 DCHECK(g_service_process->io_thread()->message_loop_proxy()-> |
| 23 BelongsToCurrentThread()); | 23 BelongsToCurrentThread()); |
| 24 FilePath exe_path; | 24 FilePath exe_path; |
| 25 PathService::Get(base::FILE_EXE, &exe_path); | 25 PathService::Get(base::FILE_EXE, &exe_path); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 41 CloudPrintProxy::CloudPrintProxy() | 41 CloudPrintProxy::CloudPrintProxy() |
| 42 : service_prefs_(NULL), | 42 : service_prefs_(NULL), |
| 43 client_(NULL) { | 43 client_(NULL) { |
| 44 } | 44 } |
| 45 | 45 |
| 46 CloudPrintProxy::~CloudPrintProxy() { | 46 CloudPrintProxy::~CloudPrintProxy() { |
| 47 DCHECK(CalledOnValidThread()); | 47 DCHECK(CalledOnValidThread()); |
| 48 Shutdown(); | 48 Shutdown(); |
| 49 } | 49 } |
| 50 | 50 |
| 51 void CloudPrintProxy::Initialize(JsonPrefStore* service_prefs, Client* client) { | 51 void CloudPrintProxy::Initialize(ServicePrefs* service_prefs, Client* client) { |
| 52 DCHECK(CalledOnValidThread()); | 52 DCHECK(CalledOnValidThread()); |
| 53 service_prefs_ = service_prefs; | 53 service_prefs_ = service_prefs; |
| 54 client_ = client; | 54 client_ = client; |
| 55 } | 55 } |
| 56 | 56 |
| 57 void CloudPrintProxy::EnableForUser(const std::string& lsid) { | 57 void CloudPrintProxy::EnableForUser(const std::string& lsid) { |
| 58 DCHECK(CalledOnValidThread()); | 58 DCHECK(CalledOnValidThread()); |
| 59 if (backend_.get()) | 59 if (backend_.get()) |
| 60 return; | 60 return; |
| 61 | 61 |
| 62 std::string proxy_id; | 62 std::string proxy_id; |
| 63 service_prefs_->prefs()->GetString(prefs::kCloudPrintProxyId, &proxy_id); | 63 service_prefs_->GetString(prefs::kCloudPrintProxyId, &proxy_id); |
| 64 if (proxy_id.empty()) { | 64 if (proxy_id.empty()) { |
| 65 proxy_id = cloud_print::PrintSystem::GenerateProxyId(); | 65 proxy_id = cloud_print::PrintSystem::GenerateProxyId(); |
| 66 service_prefs_->prefs()->SetString(prefs::kCloudPrintProxyId, proxy_id); | 66 service_prefs_->SetString(prefs::kCloudPrintProxyId, proxy_id); |
| 67 service_prefs_->WritePrefs(); | 67 service_prefs_->WritePrefs(); |
| 68 } | 68 } |
| 69 | 69 |
| 70 // Getting print system specific settings from the preferences. | 70 // Getting print system specific settings from the preferences. |
| 71 DictionaryValue* print_system_settings = NULL; | 71 DictionaryValue* print_system_settings = NULL; |
| 72 service_prefs_->prefs()->GetDictionary(prefs::kCloudPrintPrintSystemSettings, | 72 service_prefs_->GetDictionary(prefs::kCloudPrintPrintSystemSettings, |
| 73 &print_system_settings); | 73 &print_system_settings); |
| 74 | 74 |
| 75 // Check if there is an override for the cloud print server URL. | 75 // Check if there is an override for the cloud print server URL. |
| 76 std::string cloud_print_server_url_str; | 76 std::string cloud_print_server_url_str; |
| 77 service_prefs_->prefs()->GetString(prefs::kCloudPrintServiceURL, | 77 service_prefs_->GetString(prefs::kCloudPrintServiceURL, |
| 78 &cloud_print_server_url_str); | 78 &cloud_print_server_url_str); |
| 79 if (cloud_print_server_url_str.empty()) { | 79 if (cloud_print_server_url_str.empty()) { |
| 80 cloud_print_server_url_str = kDefaultCloudPrintServerUrl; | 80 cloud_print_server_url_str = kDefaultCloudPrintServerUrl; |
| 81 } | 81 } |
| 82 | 82 |
| 83 GURL cloud_print_server_url(cloud_print_server_url_str.c_str()); | 83 GURL cloud_print_server_url(cloud_print_server_url_str.c_str()); |
| 84 DCHECK(cloud_print_server_url.is_valid()); | 84 DCHECK(cloud_print_server_url.is_valid()); |
| 85 backend_.reset(new CloudPrintProxyBackend(this, cloud_print_server_url, | 85 backend_.reset(new CloudPrintProxyBackend(this, cloud_print_server_url, |
| 86 print_system_settings)); | 86 print_system_settings)); |
| 87 // If we have been passed in an LSID, we want to use this to authenticate. | 87 // If we have been passed in an LSID, we want to use this to authenticate. |
| 88 // Else we will try and retrieve the last used auth tokens from prefs. | 88 // Else we will try and retrieve the last used auth tokens from prefs. |
| 89 if (!lsid.empty()) { | 89 if (!lsid.empty()) { |
| 90 backend_->InitializeWithLsid(lsid, proxy_id); | 90 backend_->InitializeWithLsid(lsid, proxy_id); |
| 91 } else { | 91 } else { |
| 92 std::string cloud_print_token; | 92 std::string cloud_print_token; |
| 93 service_prefs_->prefs()->GetString(prefs::kCloudPrintAuthToken, | 93 service_prefs_->GetString(prefs::kCloudPrintAuthToken, |
| 94 &cloud_print_token); | 94 &cloud_print_token); |
| 95 DCHECK(!cloud_print_token.empty()); | 95 DCHECK(!cloud_print_token.empty()); |
| 96 std::string cloud_print_xmpp_token; | 96 std::string cloud_print_xmpp_token; |
| 97 service_prefs_->prefs()->GetString(prefs::kCloudPrintXMPPAuthToken, | 97 service_prefs_->GetString(prefs::kCloudPrintXMPPAuthToken, |
| 98 &cloud_print_xmpp_token); | 98 &cloud_print_xmpp_token); |
| 99 DCHECK(!cloud_print_xmpp_token.empty()); | 99 DCHECK(!cloud_print_xmpp_token.empty()); |
| 100 service_prefs_->prefs()->GetString(prefs::kCloudPrintEmail, | 100 service_prefs_->GetString(prefs::kCloudPrintEmail, |
| 101 &cloud_print_email_); | 101 &cloud_print_email_); |
| 102 DCHECK(!cloud_print_email_.empty()); | 102 DCHECK(!cloud_print_email_.empty()); |
| 103 backend_->InitializeWithToken(cloud_print_token, cloud_print_xmpp_token, | 103 backend_->InitializeWithToken(cloud_print_token, cloud_print_xmpp_token, |
| 104 cloud_print_email_, proxy_id); | 104 cloud_print_email_, proxy_id); |
| 105 } | 105 } |
| 106 if (client_) { | 106 if (client_) { |
| 107 client_->OnCloudPrintProxyEnabled(); | 107 client_->OnCloudPrintProxyEnabled(); |
| 108 } | 108 } |
| 109 } | 109 } |
| 110 | 110 |
| 111 void CloudPrintProxy::DisableForUser() { | 111 void CloudPrintProxy::DisableForUser() { |
| 112 DCHECK(CalledOnValidThread()); | 112 DCHECK(CalledOnValidThread()); |
| 113 cloud_print_email_.clear(); | 113 cloud_print_email_.clear(); |
| 114 Shutdown(); | 114 Shutdown(); |
| 115 if (client_) { | 115 if (client_) { |
| 116 client_->OnCloudPrintProxyDisabled(); | 116 client_->OnCloudPrintProxyDisabled(); |
| 117 } | 117 } |
| 118 } | 118 } |
| 119 | 119 |
| 120 bool CloudPrintProxy::IsEnabled(std::string* email) const { | 120 bool CloudPrintProxy::IsEnabled(std::string* email) const { |
| 121 bool enabled = !cloud_print_email().empty(); | 121 bool enabled = !cloud_print_email().empty(); |
| 122 if (enabled && email) { | 122 if (enabled && email) { |
| 123 *email = cloud_print_email(); | 123 *email = cloud_print_email(); |
| 124 } | 124 } |
| 125 return enabled; | 125 return enabled; |
| 126 } | 126 } |
| 127 | 127 |
| 128 void CloudPrintProxy::Shutdown() { | |
| 129 DCHECK(CalledOnValidThread()); | |
| 130 if (backend_.get()) | |
| 131 backend_->Shutdown(); | |
| 132 backend_.reset(); | |
| 133 } | |
| 134 | |
| 135 // Notification methods from the backend. Called on UI thread. | 128 // Notification methods from the backend. Called on UI thread. |
| 136 void CloudPrintProxy::OnPrinterListAvailable( | 129 void CloudPrintProxy::OnPrinterListAvailable( |
| 137 const printing::PrinterList& printer_list) { | 130 const printing::PrinterList& printer_list) { |
| 138 DCHECK(CalledOnValidThread()); | 131 DCHECK(CalledOnValidThread()); |
| 139 // We could potentially show UI here allowing the user to select which | 132 // We could potentially show UI here allowing the user to select which |
| 140 // printers to register. For now, we just register all. | 133 // printers to register. For now, we just register all. |
| 141 backend_->RegisterPrinters(printer_list); | 134 backend_->RegisterPrinters(printer_list); |
| 142 } | 135 } |
| 143 | 136 |
| 144 void CloudPrintProxy::OnAuthenticated( | 137 void CloudPrintProxy::OnAuthenticated( |
| 145 const std::string& cloud_print_token, | 138 const std::string& cloud_print_token, |
| 146 const std::string& cloud_print_xmpp_token, | 139 const std::string& cloud_print_xmpp_token, |
| 147 const std::string& email) { | 140 const std::string& email) { |
| 148 DCHECK(CalledOnValidThread()); | 141 DCHECK(CalledOnValidThread()); |
| 149 cloud_print_email_ = email; | 142 cloud_print_email_ = email; |
| 150 service_prefs_->prefs()->SetString(prefs::kCloudPrintAuthToken, | 143 service_prefs_->SetString(prefs::kCloudPrintAuthToken, |
| 151 cloud_print_token); | 144 cloud_print_token); |
| 152 service_prefs_->prefs()->SetString(prefs::kCloudPrintXMPPAuthToken, | 145 service_prefs_->SetString(prefs::kCloudPrintXMPPAuthToken, |
| 153 cloud_print_xmpp_token); | 146 cloud_print_xmpp_token); |
| 154 service_prefs_->prefs()->SetString(prefs::kCloudPrintEmail, email); | 147 service_prefs_->SetString(prefs::kCloudPrintEmail, |
| 148 email); |
| 155 service_prefs_->WritePrefs(); | 149 service_prefs_->WritePrefs(); |
| 156 } | 150 } |
| 157 | 151 |
| 158 void CloudPrintProxy::OnAuthenticationFailed() { | 152 void CloudPrintProxy::OnAuthenticationFailed() { |
| 159 DCHECK(CalledOnValidThread()); | 153 DCHECK(CalledOnValidThread()); |
| 160 // If authenticated failed, we will disable the cloud print proxy. | 154 // If authenticated failed, we will disable the cloud print proxy. |
| 161 DisableForUser(); | 155 DisableForUser(); |
| 162 // Launch the browser to display a notification that the credentials have | 156 // Launch the browser to display a notification that the credentials have |
| 163 // expired. | 157 // expired. |
| 164 g_service_process->io_thread()->message_loop_proxy()->PostTask( | 158 g_service_process->io_thread()->message_loop_proxy()->PostTask( |
| 165 FROM_HERE, NewRunnableFunction(&ShowTokenExpiredNotificationInBrowser)); | 159 FROM_HERE, NewRunnableFunction(&ShowTokenExpiredNotificationInBrowser)); |
| 166 } | 160 } |
| 161 |
| 162 void CloudPrintProxy::Shutdown() { |
| 163 DCHECK(CalledOnValidThread()); |
| 164 if (backend_.get()) |
| 165 backend_->Shutdown(); |
| 166 backend_.reset(); |
| 167 } |
| OLD | NEW |