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" |
16 #include "chrome/service/service_process.h" | 15 #include "chrome/service/service_process.h" |
| 16 #include "chrome/service/service_process_prefs.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); |
26 if (exe_path.empty()) { | 26 if (exe_path.empty()) { |
(...skipping 14 matching lines...) 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(ServiceProcessPrefs* service_prefs, |
| 52 Client* client) { |
52 DCHECK(CalledOnValidThread()); | 53 DCHECK(CalledOnValidThread()); |
53 service_prefs_ = service_prefs; | 54 service_prefs_ = service_prefs; |
54 client_ = client; | 55 client_ = client; |
55 } | 56 } |
56 | 57 |
57 void CloudPrintProxy::EnableForUser(const std::string& lsid) { | 58 void CloudPrintProxy::EnableForUser(const std::string& lsid) { |
58 DCHECK(CalledOnValidThread()); | 59 DCHECK(CalledOnValidThread()); |
59 if (backend_.get()) | 60 if (backend_.get()) |
60 return; | 61 return; |
61 | 62 |
62 std::string proxy_id; | 63 std::string proxy_id; |
63 service_prefs_->prefs()->GetString(prefs::kCloudPrintProxyId, &proxy_id); | 64 service_prefs_->GetString(prefs::kCloudPrintProxyId, &proxy_id); |
64 if (proxy_id.empty()) { | 65 if (proxy_id.empty()) { |
65 proxy_id = cloud_print::PrintSystem::GenerateProxyId(); | 66 proxy_id = cloud_print::PrintSystem::GenerateProxyId(); |
66 service_prefs_->prefs()->SetString(prefs::kCloudPrintProxyId, proxy_id); | 67 service_prefs_->SetString(prefs::kCloudPrintProxyId, proxy_id); |
67 service_prefs_->WritePrefs(); | 68 service_prefs_->WritePrefs(); |
68 } | 69 } |
69 | 70 |
70 // Getting print system specific settings from the preferences. | 71 // Getting print system specific settings from the preferences. |
71 DictionaryValue* print_system_settings = NULL; | 72 DictionaryValue* print_system_settings = NULL; |
72 service_prefs_->prefs()->GetDictionary(prefs::kCloudPrintPrintSystemSettings, | 73 service_prefs_->GetDictionary(prefs::kCloudPrintPrintSystemSettings, |
73 &print_system_settings); | 74 &print_system_settings); |
74 | 75 |
75 // Check if there is an override for the cloud print server URL. | 76 // Check if there is an override for the cloud print server URL. |
76 std::string cloud_print_server_url_str; | 77 std::string cloud_print_server_url_str; |
77 service_prefs_->prefs()->GetString(prefs::kCloudPrintServiceURL, | 78 service_prefs_->GetString(prefs::kCloudPrintServiceURL, |
78 &cloud_print_server_url_str); | 79 &cloud_print_server_url_str); |
79 if (cloud_print_server_url_str.empty()) { | 80 if (cloud_print_server_url_str.empty()) { |
80 cloud_print_server_url_str = kDefaultCloudPrintServerUrl; | 81 cloud_print_server_url_str = kDefaultCloudPrintServerUrl; |
81 } | 82 } |
82 | 83 |
83 GURL cloud_print_server_url(cloud_print_server_url_str.c_str()); | 84 GURL cloud_print_server_url(cloud_print_server_url_str.c_str()); |
84 DCHECK(cloud_print_server_url.is_valid()); | 85 DCHECK(cloud_print_server_url.is_valid()); |
85 backend_.reset(new CloudPrintProxyBackend(this, cloud_print_server_url, | 86 backend_.reset(new CloudPrintProxyBackend(this, cloud_print_server_url, |
86 print_system_settings)); | 87 print_system_settings)); |
87 // If we have been passed in an LSID, we want to use this to authenticate. | 88 // 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. | 89 // Else we will try and retrieve the last used auth tokens from prefs. |
89 if (!lsid.empty()) { | 90 if (!lsid.empty()) { |
90 backend_->InitializeWithLsid(lsid, proxy_id); | 91 backend_->InitializeWithLsid(lsid, proxy_id); |
91 } else { | 92 } else { |
92 std::string cloud_print_token; | 93 std::string cloud_print_token; |
93 service_prefs_->prefs()->GetString(prefs::kCloudPrintAuthToken, | 94 service_prefs_->GetString(prefs::kCloudPrintAuthToken, |
94 &cloud_print_token); | 95 &cloud_print_token); |
95 DCHECK(!cloud_print_token.empty()); | 96 DCHECK(!cloud_print_token.empty()); |
96 std::string cloud_print_xmpp_token; | 97 std::string cloud_print_xmpp_token; |
97 service_prefs_->prefs()->GetString(prefs::kCloudPrintXMPPAuthToken, | 98 service_prefs_->GetString(prefs::kCloudPrintXMPPAuthToken, |
98 &cloud_print_xmpp_token); | 99 &cloud_print_xmpp_token); |
99 DCHECK(!cloud_print_xmpp_token.empty()); | 100 DCHECK(!cloud_print_xmpp_token.empty()); |
100 service_prefs_->prefs()->GetString(prefs::kCloudPrintEmail, | 101 service_prefs_->GetString(prefs::kCloudPrintEmail, |
101 &cloud_print_email_); | 102 &cloud_print_email_); |
102 DCHECK(!cloud_print_email_.empty()); | 103 DCHECK(!cloud_print_email_.empty()); |
103 backend_->InitializeWithToken(cloud_print_token, cloud_print_xmpp_token, | 104 backend_->InitializeWithToken(cloud_print_token, cloud_print_xmpp_token, |
104 cloud_print_email_, proxy_id); | 105 cloud_print_email_, proxy_id); |
105 } | 106 } |
106 if (client_) { | 107 if (client_) { |
107 client_->OnCloudPrintProxyEnabled(); | 108 client_->OnCloudPrintProxyEnabled(); |
108 } | 109 } |
109 } | 110 } |
110 | 111 |
111 void CloudPrintProxy::DisableForUser() { | 112 void CloudPrintProxy::DisableForUser() { |
112 DCHECK(CalledOnValidThread()); | 113 DCHECK(CalledOnValidThread()); |
113 cloud_print_email_.clear(); | 114 cloud_print_email_.clear(); |
114 Shutdown(); | 115 Shutdown(); |
115 if (client_) { | 116 if (client_) { |
116 client_->OnCloudPrintProxyDisabled(); | 117 client_->OnCloudPrintProxyDisabled(); |
117 } | 118 } |
118 } | 119 } |
119 | 120 |
120 bool CloudPrintProxy::IsEnabled(std::string* email) const { | 121 bool CloudPrintProxy::IsEnabled(std::string* email) const { |
121 bool enabled = !cloud_print_email().empty(); | 122 bool enabled = !cloud_print_email().empty(); |
122 if (enabled && email) { | 123 if (enabled && email) { |
123 *email = cloud_print_email(); | 124 *email = cloud_print_email(); |
124 } | 125 } |
125 return enabled; | 126 return enabled; |
126 } | 127 } |
127 | 128 |
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. | 129 // Notification methods from the backend. Called on UI thread. |
136 void CloudPrintProxy::OnPrinterListAvailable( | 130 void CloudPrintProxy::OnPrinterListAvailable( |
137 const printing::PrinterList& printer_list) { | 131 const printing::PrinterList& printer_list) { |
138 DCHECK(CalledOnValidThread()); | 132 DCHECK(CalledOnValidThread()); |
139 // We could potentially show UI here allowing the user to select which | 133 // We could potentially show UI here allowing the user to select which |
140 // printers to register. For now, we just register all. | 134 // printers to register. For now, we just register all. |
141 backend_->RegisterPrinters(printer_list); | 135 backend_->RegisterPrinters(printer_list); |
142 } | 136 } |
143 | 137 |
144 void CloudPrintProxy::OnAuthenticated( | 138 void CloudPrintProxy::OnAuthenticated( |
145 const std::string& cloud_print_token, | 139 const std::string& cloud_print_token, |
146 const std::string& cloud_print_xmpp_token, | 140 const std::string& cloud_print_xmpp_token, |
147 const std::string& email) { | 141 const std::string& email) { |
148 DCHECK(CalledOnValidThread()); | 142 DCHECK(CalledOnValidThread()); |
149 cloud_print_email_ = email; | 143 cloud_print_email_ = email; |
150 service_prefs_->prefs()->SetString(prefs::kCloudPrintAuthToken, | 144 service_prefs_->SetString(prefs::kCloudPrintAuthToken, |
151 cloud_print_token); | 145 cloud_print_token); |
152 service_prefs_->prefs()->SetString(prefs::kCloudPrintXMPPAuthToken, | 146 service_prefs_->SetString(prefs::kCloudPrintXMPPAuthToken, |
153 cloud_print_xmpp_token); | 147 cloud_print_xmpp_token); |
154 service_prefs_->prefs()->SetString(prefs::kCloudPrintEmail, email); | 148 service_prefs_->SetString(prefs::kCloudPrintEmail, |
| 149 email); |
155 service_prefs_->WritePrefs(); | 150 service_prefs_->WritePrefs(); |
156 } | 151 } |
157 | 152 |
158 void CloudPrintProxy::OnAuthenticationFailed() { | 153 void CloudPrintProxy::OnAuthenticationFailed() { |
159 DCHECK(CalledOnValidThread()); | 154 DCHECK(CalledOnValidThread()); |
160 // If authenticated failed, we will disable the cloud print proxy. | 155 // If authenticated failed, we will disable the cloud print proxy. |
161 DisableForUser(); | 156 DisableForUser(); |
162 // Launch the browser to display a notification that the credentials have | 157 // Launch the browser to display a notification that the credentials have |
163 // expired. | 158 // expired. |
164 g_service_process->io_thread()->message_loop_proxy()->PostTask( | 159 g_service_process->io_thread()->message_loop_proxy()->PostTask( |
165 FROM_HERE, NewRunnableFunction(&ShowTokenExpiredNotificationInBrowser)); | 160 FROM_HERE, NewRunnableFunction(&ShowTokenExpiredNotificationInBrowser)); |
166 } | 161 } |
| 162 |
| 163 void CloudPrintProxy::Shutdown() { |
| 164 DCHECK(CalledOnValidThread()); |
| 165 if (backend_.get()) |
| 166 backend_->Shutdown(); |
| 167 backend_.reset(); |
| 168 } |
OLD | NEW |