Chromium Code Reviews| 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/service/cloud_print/cloud_print_proxy_backend.h" | 5 #include "chrome/service/cloud_print/cloud_print_proxy_backend.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 56 // | 56 // |
| 57 // The Do* methods are the various entry points from CloudPrintProxyBackend | 57 // The Do* methods are the various entry points from CloudPrintProxyBackend |
| 58 // It calls us on a dedicated thread to actually perform synchronous | 58 // It calls us on a dedicated thread to actually perform synchronous |
| 59 // (and potentially blocking) operations. | 59 // (and potentially blocking) operations. |
| 60 void DoInitializeWithToken(const std::string& cloud_print_token); | 60 void DoInitializeWithToken(const std::string& cloud_print_token); |
| 61 void DoInitializeWithRobotToken(const std::string& robot_oauth_refresh_token, | 61 void DoInitializeWithRobotToken(const std::string& robot_oauth_refresh_token, |
| 62 const std::string& robot_email); | 62 const std::string& robot_email); |
| 63 void DoInitializeWithRobotAuthCode(const std::string& robot_oauth_auth_code, | 63 void DoInitializeWithRobotAuthCode(const std::string& robot_oauth_auth_code, |
| 64 const std::string& robot_email); | 64 const std::string& robot_email); |
| 65 | 65 |
| 66 // Called on the CloudPrintProxyBackend core_thread_ to perform | 66 // Called on the CloudPrintProxyBackend |core_thread_| to perform |
| 67 // shutdown. | 67 // shutdown. |
|
dschuyler
2016/04/20 22:00:47
will 'shutdown.' fit on the line above?
Lei Zhang
2016/04/20 22:04:46
Yes, done.
| |
| 68 void DoShutdown(); | 68 void DoShutdown(); |
| 69 void DoRegisterSelectedPrinters( | 69 void DoRegisterSelectedPrinters( |
| 70 const printing::PrinterList& printer_list); | 70 const printing::PrinterList& printer_list); |
| 71 void DoUnregisterPrinters(); | 71 void DoUnregisterPrinters(); |
| 72 | 72 |
| 73 // CloudPrintAuth::Client implementation. | 73 // CloudPrintAuth::Client implementation. |
| 74 void OnAuthenticationComplete(const std::string& access_token, | 74 void OnAuthenticationComplete(const std::string& access_token, |
| 75 const std::string& robot_oauth_refresh_token, | 75 const std::string& robot_oauth_refresh_token, |
| 76 const std::string& robot_email, | 76 const std::string& robot_email, |
| 77 const std::string& user_email) override; | 77 const std::string& user_email) override; |
| 78 void OnInvalidCredentials() override; | 78 void OnInvalidCredentials() override; |
| 79 | 79 |
| 80 // CloudPrintConnector::Client implementation. | 80 // CloudPrintConnector::Client implementation. |
| 81 void OnAuthFailed() override; | 81 void OnAuthFailed() override; |
| 82 void OnXmppPingUpdated(int ping_timeout) override; | 82 void OnXmppPingUpdated(int ping_timeout) override; |
| 83 | 83 |
| 84 // notifier::PushClientObserver implementation. | 84 // notifier::PushClientObserver implementation. |
| 85 void OnNotificationsEnabled() override; | 85 void OnNotificationsEnabled() override; |
| 86 void OnNotificationsDisabled( | 86 void OnNotificationsDisabled( |
| 87 notifier::NotificationsDisabledReason reason) override; | 87 notifier::NotificationsDisabledReason reason) override; |
| 88 void OnIncomingNotification( | 88 void OnIncomingNotification( |
| 89 const notifier::Notification& notification) override; | 89 const notifier::Notification& notification) override; |
| 90 void OnPingResponse() override; | 90 void OnPingResponse() override; |
| 91 | 91 |
| 92 private: | 92 private: |
| 93 friend class base::RefCountedThreadSafe<Core>; | 93 friend class base::RefCountedThreadSafe<Core>; |
| 94 | 94 |
| 95 ~Core() override {} | 95 ~Core() override {} |
| 96 | 96 |
| 97 CloudPrintProxyFrontend* frontend() { return backend_->frontend_; } | |
| 98 | |
| 99 bool PostFrontendTask(const tracked_objects::Location& from_here, | |
| 100 const base::Closure& task); | |
| 101 | |
| 102 bool CurrentlyOnFrontendThread() const; | |
| 103 bool CurrentlyOnCoreThread() const; | |
| 104 | |
| 97 void CreateAuthAndConnector(); | 105 void CreateAuthAndConnector(); |
| 98 void DestroyAuthAndConnector(); | 106 void DestroyAuthAndConnector(); |
| 99 | 107 |
| 100 // NotifyXXX is how the Core communicates with the frontend across | 108 // NotifyXXX is how the Core communicates with the frontend across |
| 101 // threads. | 109 // threads. |
| 102 void NotifyPrinterListAvailable( | 110 void NotifyPrinterListAvailable( |
| 103 const printing::PrinterList& printer_list); | 111 const printing::PrinterList& printer_list); |
| 104 void NotifyAuthenticated( | 112 void NotifyAuthenticated( |
| 105 const std::string& robot_oauth_refresh_token, | 113 const std::string& robot_oauth_refresh_token, |
| 106 const std::string& robot_email, | 114 const std::string& robot_email, |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 120 // Schedules a task to poll for jobs. Does nothing if a task is already | 128 // Schedules a task to poll for jobs. Does nothing if a task is already |
| 121 // scheduled. | 129 // scheduled. |
| 122 void ScheduleJobPoll(); | 130 void ScheduleJobPoll(); |
| 123 void PingXmppServer(); | 131 void PingXmppServer(); |
| 124 void ScheduleXmppPing(); | 132 void ScheduleXmppPing(); |
| 125 void CheckXmppPingStatus(); | 133 void CheckXmppPingStatus(); |
| 126 | 134 |
| 127 CloudPrintTokenStore* GetTokenStore(); | 135 CloudPrintTokenStore* GetTokenStore(); |
| 128 | 136 |
| 129 // Our parent CloudPrintProxyBackend | 137 // Our parent CloudPrintProxyBackend |
| 130 CloudPrintProxyBackend* backend_; | 138 CloudPrintProxyBackend* const backend_; |
| 131 | 139 |
| 132 // Cloud Print authenticator. | 140 // Cloud Print authenticator. |
| 133 scoped_refptr<CloudPrintAuth> auth_; | 141 scoped_refptr<CloudPrintAuth> auth_; |
| 134 | 142 |
| 135 // Cloud Print connector. | 143 // Cloud Print connector. |
| 136 scoped_refptr<CloudPrintConnector> connector_; | 144 scoped_refptr<CloudPrintConnector> connector_; |
| 137 | 145 |
| 138 // OAuth client info. | 146 // OAuth client info. |
| 139 gaia::OAuthClientInfo oauth_client_info_; | 147 gaia::OAuthClientInfo oauth_client_info_; |
| 140 // Notification (xmpp) handler. | 148 // Notification (xmpp) handler. |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 171 DCHECK(frontend_); | 179 DCHECK(frontend_); |
| 172 core_ = new Core(this, settings, oauth_client_info, enable_job_poll); | 180 core_ = new Core(this, settings, oauth_client_info, enable_job_poll); |
| 173 } | 181 } |
| 174 | 182 |
| 175 CloudPrintProxyBackend::~CloudPrintProxyBackend() { DCHECK(!core_.get()); } | 183 CloudPrintProxyBackend::~CloudPrintProxyBackend() { DCHECK(!core_.get()); } |
| 176 | 184 |
| 177 bool CloudPrintProxyBackend::InitializeWithToken( | 185 bool CloudPrintProxyBackend::InitializeWithToken( |
| 178 const std::string& cloud_print_token) { | 186 const std::string& cloud_print_token) { |
| 179 if (!core_thread_.Start()) | 187 if (!core_thread_.Start()) |
| 180 return false; | 188 return false; |
| 181 core_thread_.task_runner()->PostTask( | 189 PostCoreTask(FROM_HERE, |
| 182 FROM_HERE, | 190 base::Bind(&CloudPrintProxyBackend::Core::DoInitializeWithToken, |
| 183 base::Bind(&CloudPrintProxyBackend::Core::DoInitializeWithToken, | 191 core_.get(), cloud_print_token)); |
| 184 core_.get(), cloud_print_token)); | |
| 185 return true; | 192 return true; |
| 186 } | 193 } |
| 187 | 194 |
| 188 bool CloudPrintProxyBackend::InitializeWithRobotToken( | 195 bool CloudPrintProxyBackend::InitializeWithRobotToken( |
| 189 const std::string& robot_oauth_refresh_token, | 196 const std::string& robot_oauth_refresh_token, |
| 190 const std::string& robot_email) { | 197 const std::string& robot_email) { |
| 191 if (!core_thread_.Start()) | 198 if (!core_thread_.Start()) |
| 192 return false; | 199 return false; |
| 193 core_thread_.task_runner()->PostTask( | 200 PostCoreTask( |
| 194 FROM_HERE, | 201 FROM_HERE, |
| 195 base::Bind(&CloudPrintProxyBackend::Core::DoInitializeWithRobotToken, | 202 base::Bind(&CloudPrintProxyBackend::Core::DoInitializeWithRobotToken, |
| 196 core_.get(), robot_oauth_refresh_token, robot_email)); | 203 core_.get(), robot_oauth_refresh_token, robot_email)); |
| 197 return true; | 204 return true; |
| 198 } | 205 } |
| 199 | 206 |
| 200 bool CloudPrintProxyBackend::InitializeWithRobotAuthCode( | 207 bool CloudPrintProxyBackend::InitializeWithRobotAuthCode( |
| 201 const std::string& robot_oauth_auth_code, | 208 const std::string& robot_oauth_auth_code, |
| 202 const std::string& robot_email) { | 209 const std::string& robot_email) { |
| 203 if (!core_thread_.Start()) | 210 if (!core_thread_.Start()) |
| 204 return false; | 211 return false; |
| 205 core_thread_.task_runner()->PostTask( | 212 PostCoreTask( |
| 206 FROM_HERE, | 213 FROM_HERE, |
| 207 base::Bind(&CloudPrintProxyBackend::Core::DoInitializeWithRobotAuthCode, | 214 base::Bind(&CloudPrintProxyBackend::Core::DoInitializeWithRobotAuthCode, |
| 208 core_.get(), robot_oauth_auth_code, robot_email)); | 215 core_.get(), robot_oauth_auth_code, robot_email)); |
| 209 return true; | 216 return true; |
| 210 } | 217 } |
| 211 | 218 |
| 212 void CloudPrintProxyBackend::Shutdown() { | 219 void CloudPrintProxyBackend::Shutdown() { |
| 213 core_thread_.task_runner()->PostTask( | 220 PostCoreTask(FROM_HERE, base::Bind(&CloudPrintProxyBackend::Core::DoShutdown, |
| 214 FROM_HERE, | 221 core_.get())); |
| 215 base::Bind(&CloudPrintProxyBackend::Core::DoShutdown, core_.get())); | |
| 216 core_thread_.Stop(); | 222 core_thread_.Stop(); |
| 217 core_ = NULL; // Releases reference to core_. | 223 core_ = nullptr; // Releases reference to |core_|. |
| 218 } | 224 } |
| 219 | 225 |
| 220 void CloudPrintProxyBackend::UnregisterPrinters() { | 226 void CloudPrintProxyBackend::UnregisterPrinters() { |
| 221 core_thread_.task_runner()->PostTask( | 227 PostCoreTask(FROM_HERE, |
| 222 FROM_HERE, base::Bind(&CloudPrintProxyBackend::Core::DoUnregisterPrinters, | 228 base::Bind(&CloudPrintProxyBackend::Core::DoUnregisterPrinters, |
| 223 core_.get())); | 229 core_.get())); |
| 230 } | |
| 231 | |
| 232 bool CloudPrintProxyBackend::PostCoreTask( | |
| 233 const tracked_objects::Location& from_here, | |
| 234 const base::Closure& task) { | |
| 235 return core_thread_.task_runner()->PostTask(from_here, task); | |
| 224 } | 236 } |
| 225 | 237 |
| 226 CloudPrintProxyBackend::Core::Core( | 238 CloudPrintProxyBackend::Core::Core( |
| 227 CloudPrintProxyBackend* backend, | 239 CloudPrintProxyBackend* backend, |
| 228 const ConnectorSettings& settings, | 240 const ConnectorSettings& settings, |
| 229 const gaia::OAuthClientInfo& oauth_client_info, | 241 const gaia::OAuthClientInfo& oauth_client_info, |
| 230 bool enable_job_poll) | 242 bool enable_job_poll) |
| 231 : backend_(backend), | 243 : backend_(backend), |
| 232 oauth_client_info_(oauth_client_info), | 244 oauth_client_info_(oauth_client_info), |
| 233 notifications_enabled_(false), | 245 notifications_enabled_(false), |
| 234 job_poll_scheduled_(false), | 246 job_poll_scheduled_(false), |
| 235 enable_job_poll_(enable_job_poll), | 247 enable_job_poll_(enable_job_poll), |
| 236 xmpp_ping_scheduled_(false), | 248 xmpp_ping_scheduled_(false), |
| 237 pending_xmpp_pings_(0) { | 249 pending_xmpp_pings_(0) { |
| 238 settings_.CopyFrom(settings); | 250 settings_.CopyFrom(settings); |
| 239 } | 251 } |
| 240 | 252 |
| 253 bool CloudPrintProxyBackend::Core::PostFrontendTask( | |
| 254 const tracked_objects::Location& from_here, | |
| 255 const base::Closure& task) { | |
| 256 return backend_->frontend_loop_->task_runner()->PostTask(from_here, task); | |
| 257 } | |
| 258 | |
| 259 bool CloudPrintProxyBackend::Core::CurrentlyOnFrontendThread() const { | |
| 260 return base::MessageLoop::current() == backend_->frontend_loop_; | |
| 261 } | |
| 262 | |
| 263 bool CloudPrintProxyBackend::Core::CurrentlyOnCoreThread() const { | |
| 264 return base::MessageLoop::current() == backend_->core_thread_.message_loop(); | |
| 265 } | |
| 266 | |
| 241 void CloudPrintProxyBackend::Core::CreateAuthAndConnector() { | 267 void CloudPrintProxyBackend::Core::CreateAuthAndConnector() { |
| 242 if (!auth_.get()) { | 268 if (!auth_.get()) { |
| 243 auth_ = new CloudPrintAuth(this, settings_.server_url(), oauth_client_info_, | 269 auth_ = new CloudPrintAuth(this, settings_.server_url(), oauth_client_info_, |
| 244 settings_.proxy_id()); | 270 settings_.proxy_id()); |
| 245 } | 271 } |
| 246 | 272 |
| 247 if (!connector_.get()) { | 273 if (!connector_.get()) { |
| 248 connector_ = new CloudPrintConnector(this, settings_); | 274 connector_ = new CloudPrintConnector(this, settings_); |
| 249 } | 275 } |
| 250 } | 276 } |
| 251 | 277 |
| 252 void CloudPrintProxyBackend::Core::DestroyAuthAndConnector() { | 278 void CloudPrintProxyBackend::Core::DestroyAuthAndConnector() { |
| 253 auth_ = NULL; | 279 auth_ = NULL; |
| 254 connector_ = NULL; | 280 connector_ = NULL; |
| 255 } | 281 } |
| 256 | 282 |
| 257 void CloudPrintProxyBackend::Core::DoInitializeWithToken( | 283 void CloudPrintProxyBackend::Core::DoInitializeWithToken( |
| 258 const std::string& cloud_print_token) { | 284 const std::string& cloud_print_token) { |
| 259 DCHECK(base::MessageLoop::current() == backend_->core_thread_.message_loop()); | 285 DCHECK(CurrentlyOnCoreThread()); |
| 260 CreateAuthAndConnector(); | 286 CreateAuthAndConnector(); |
| 261 auth_->AuthenticateWithToken(cloud_print_token); | 287 auth_->AuthenticateWithToken(cloud_print_token); |
| 262 } | 288 } |
| 263 | 289 |
| 264 void CloudPrintProxyBackend::Core::DoInitializeWithRobotToken( | 290 void CloudPrintProxyBackend::Core::DoInitializeWithRobotToken( |
| 265 const std::string& robot_oauth_refresh_token, | 291 const std::string& robot_oauth_refresh_token, |
| 266 const std::string& robot_email) { | 292 const std::string& robot_email) { |
| 267 DCHECK(base::MessageLoop::current() == backend_->core_thread_.message_loop()); | 293 DCHECK(CurrentlyOnCoreThread()); |
| 268 CreateAuthAndConnector(); | 294 CreateAuthAndConnector(); |
| 269 auth_->AuthenticateWithRobotToken(robot_oauth_refresh_token, robot_email); | 295 auth_->AuthenticateWithRobotToken(robot_oauth_refresh_token, robot_email); |
| 270 } | 296 } |
| 271 | 297 |
| 272 void CloudPrintProxyBackend::Core::DoInitializeWithRobotAuthCode( | 298 void CloudPrintProxyBackend::Core::DoInitializeWithRobotAuthCode( |
| 273 const std::string& robot_oauth_auth_code, | 299 const std::string& robot_oauth_auth_code, |
| 274 const std::string& robot_email) { | 300 const std::string& robot_email) { |
| 275 DCHECK(base::MessageLoop::current() == backend_->core_thread_.message_loop()); | 301 DCHECK(CurrentlyOnCoreThread()); |
| 276 CreateAuthAndConnector(); | 302 CreateAuthAndConnector(); |
| 277 auth_->AuthenticateWithRobotAuthCode(robot_oauth_auth_code, robot_email); | 303 auth_->AuthenticateWithRobotAuthCode(robot_oauth_auth_code, robot_email); |
| 278 } | 304 } |
| 279 | 305 |
| 280 void CloudPrintProxyBackend::Core::OnAuthenticationComplete( | 306 void CloudPrintProxyBackend::Core::OnAuthenticationComplete( |
| 281 const std::string& access_token, | 307 const std::string& access_token, |
| 282 const std::string& robot_oauth_refresh_token, | 308 const std::string& robot_oauth_refresh_token, |
| 283 const std::string& robot_email, | 309 const std::string& robot_email, |
| 284 const std::string& user_email) { | 310 const std::string& user_email) { |
| 285 CloudPrintTokenStore* token_store = GetTokenStore(); | 311 CloudPrintTokenStore* token_store = GetTokenStore(); |
| 286 bool first_time = token_store->token().empty(); | 312 bool first_time = token_store->token().empty(); |
| 287 token_store->SetToken(access_token); | 313 token_store->SetToken(access_token); |
| 288 robot_email_ = robot_email; | 314 robot_email_ = robot_email; |
| 289 // Let the frontend know that we have authenticated. | 315 // Let the frontend know that we have authenticated. |
| 290 backend_->frontend_loop_->task_runner()->PostTask( | 316 PostFrontendTask(FROM_HERE, base::Bind(&Core::NotifyAuthenticated, this, |
| 291 FROM_HERE, | 317 robot_oauth_refresh_token, robot_email, |
| 292 base::Bind(&Core::NotifyAuthenticated, this, robot_oauth_refresh_token, | 318 user_email)); |
| 293 robot_email, user_email)); | |
| 294 if (first_time) { | 319 if (first_time) { |
| 295 InitNotifications(robot_email, access_token); | 320 InitNotifications(robot_email, access_token); |
| 296 } else { | 321 } else { |
| 297 // If we are refreshing a token, update the XMPP token too. | 322 // If we are refreshing a token, update the XMPP token too. |
| 298 DCHECK(push_client_.get()); | 323 DCHECK(push_client_.get()); |
| 299 push_client_->UpdateCredentials(robot_email, access_token); | 324 push_client_->UpdateCredentials(robot_email, access_token); |
| 300 } | 325 } |
| 301 // Start cloud print connector if needed. | 326 // Start cloud print connector if needed. |
| 302 if (!connector_->IsRunning()) { | 327 if (!connector_->IsRunning()) { |
| 303 if (!connector_->Start()) { | 328 if (!connector_->Start()) { |
| 304 // Let the frontend know that we do not have a print system. | 329 // Let the frontend know that we do not have a print system. |
| 305 backend_->frontend_loop_->task_runner()->PostTask( | 330 PostFrontendTask(FROM_HERE, |
| 306 FROM_HERE, base::Bind(&Core::NotifyPrintSystemUnavailable, this)); | 331 base::Bind(&Core::NotifyPrintSystemUnavailable, this)); |
| 307 } | 332 } |
| 308 } | 333 } |
| 309 } | 334 } |
| 310 | 335 |
| 311 void CloudPrintProxyBackend::Core::OnInvalidCredentials() { | 336 void CloudPrintProxyBackend::Core::OnInvalidCredentials() { |
| 312 DCHECK(base::MessageLoop::current() == backend_->core_thread_.message_loop()); | 337 DCHECK(CurrentlyOnCoreThread()); |
| 313 VLOG(1) << "CP_CONNECTOR: Auth Error"; | 338 VLOG(1) << "CP_CONNECTOR: Auth Error"; |
| 314 backend_->frontend_loop_->task_runner()->PostTask( | 339 PostFrontendTask(FROM_HERE, |
| 315 FROM_HERE, base::Bind(&Core::NotifyAuthenticationFailed, this)); | 340 base::Bind(&Core::NotifyAuthenticationFailed, this)); |
| 316 } | 341 } |
| 317 | 342 |
| 318 void CloudPrintProxyBackend::Core::OnAuthFailed() { | 343 void CloudPrintProxyBackend::Core::OnAuthFailed() { |
| 319 VLOG(1) << "CP_CONNECTOR: Authentication failed in connector."; | 344 VLOG(1) << "CP_CONNECTOR: Authentication failed in connector."; |
| 320 // Let's stop connecter and refresh token. We'll restart connecter once | 345 // Let's stop connecter and refresh token. We'll restart connecter once |
| 321 // new token available. | 346 // new token available. |
| 322 if (connector_->IsRunning()) | 347 if (connector_->IsRunning()) |
| 323 connector_->Stop(); | 348 connector_->Stop(); |
| 324 | 349 |
| 325 // Refresh Auth token. | 350 // Refresh Auth token. |
| 326 auth_->RefreshAccessToken(); | 351 auth_->RefreshAccessToken(); |
| 327 } | 352 } |
| 328 | 353 |
| 329 void CloudPrintProxyBackend::Core::OnXmppPingUpdated(int ping_timeout) { | 354 void CloudPrintProxyBackend::Core::OnXmppPingUpdated(int ping_timeout) { |
| 330 settings_.SetXmppPingTimeoutSec(ping_timeout); | 355 settings_.SetXmppPingTimeoutSec(ping_timeout); |
| 331 backend_->frontend_loop_->task_runner()->PostTask( | 356 PostFrontendTask( |
| 332 FROM_HERE, base::Bind(&Core::NotifyXmppPingUpdated, this, ping_timeout)); | 357 FROM_HERE, base::Bind(&Core::NotifyXmppPingUpdated, this, ping_timeout)); |
| 333 } | 358 } |
| 334 | 359 |
| 335 void CloudPrintProxyBackend::Core::InitNotifications( | 360 void CloudPrintProxyBackend::Core::InitNotifications( |
| 336 const std::string& robot_email, | 361 const std::string& robot_email, |
| 337 const std::string& access_token) { | 362 const std::string& access_token) { |
| 338 DCHECK(base::MessageLoop::current() == backend_->core_thread_.message_loop()); | 363 DCHECK(CurrentlyOnCoreThread()); |
| 339 | 364 |
| 340 pending_xmpp_pings_ = 0; | 365 pending_xmpp_pings_ = 0; |
| 341 notifier::NotifierOptions notifier_options; | 366 notifier::NotifierOptions notifier_options; |
| 342 notifier_options.request_context_getter = | 367 notifier_options.request_context_getter = |
| 343 g_service_process->GetServiceURLRequestContextGetter(); | 368 g_service_process->GetServiceURLRequestContextGetter(); |
| 344 notifier_options.auth_mechanism = "X-OAUTH2"; | 369 notifier_options.auth_mechanism = "X-OAUTH2"; |
| 345 notifier_options.try_ssltcp_first = true; | 370 notifier_options.try_ssltcp_first = true; |
| 346 notifier_options.xmpp_host_port = net::HostPortPair::FromString( | 371 notifier_options.xmpp_host_port = net::HostPortPair::FromString( |
| 347 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 372 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 348 switches::kCloudPrintXmppEndpoint)); | 373 switches::kCloudPrintXmppEndpoint)); |
| 349 push_client_ = notifier::PushClient::CreateDefault(notifier_options); | 374 push_client_ = notifier::PushClient::CreateDefault(notifier_options); |
| 350 push_client_->AddObserver(this); | 375 push_client_->AddObserver(this); |
| 351 notifier::Subscription subscription; | 376 notifier::Subscription subscription; |
| 352 subscription.channel = kCloudPrintPushNotificationsSource; | 377 subscription.channel = kCloudPrintPushNotificationsSource; |
| 353 subscription.from = kCloudPrintPushNotificationsSource; | 378 subscription.from = kCloudPrintPushNotificationsSource; |
| 354 push_client_->UpdateSubscriptions( | 379 push_client_->UpdateSubscriptions( |
| 355 notifier::SubscriptionList(1, subscription)); | 380 notifier::SubscriptionList(1, subscription)); |
| 356 push_client_->UpdateCredentials(robot_email, access_token); | 381 push_client_->UpdateCredentials(robot_email, access_token); |
| 357 } | 382 } |
| 358 | 383 |
| 359 void CloudPrintProxyBackend::Core::DoShutdown() { | 384 void CloudPrintProxyBackend::Core::DoShutdown() { |
| 360 DCHECK(base::MessageLoop::current() == backend_->core_thread_.message_loop()); | 385 DCHECK(CurrentlyOnCoreThread()); |
| 361 VLOG(1) << "CP_CONNECTOR: Shutdown connector, id: " << settings_.proxy_id(); | 386 VLOG(1) << "CP_CONNECTOR: Shutdown connector, id: " << settings_.proxy_id(); |
| 362 | 387 |
| 363 if (connector_->IsRunning()) | 388 if (connector_->IsRunning()) |
| 364 connector_->Stop(); | 389 connector_->Stop(); |
| 365 | 390 |
| 366 // Important to delete the PushClient on this thread. | 391 // Important to delete the PushClient on this thread. |
| 367 if (push_client_.get()) { | 392 if (push_client_.get()) { |
| 368 push_client_->RemoveObserver(this); | 393 push_client_->RemoveObserver(this); |
| 369 } | 394 } |
| 370 push_client_.reset(); | 395 push_client_.reset(); |
| 371 notifications_enabled_ = false; | 396 notifications_enabled_ = false; |
| 372 notifications_enabled_since_ = base::TimeTicks(); | 397 notifications_enabled_since_ = base::TimeTicks(); |
| 373 token_store_.reset(); | 398 token_store_.reset(); |
| 374 | 399 |
| 375 DestroyAuthAndConnector(); | 400 DestroyAuthAndConnector(); |
| 376 } | 401 } |
| 377 | 402 |
| 378 void CloudPrintProxyBackend::Core::DoUnregisterPrinters() { | 403 void CloudPrintProxyBackend::Core::DoUnregisterPrinters() { |
| 379 DCHECK(base::MessageLoop::current() == backend_->core_thread_.message_loop()); | 404 DCHECK(CurrentlyOnCoreThread()); |
| 380 | 405 |
| 381 std::string access_token = GetTokenStore()->token(); | 406 std::string access_token = GetTokenStore()->token(); |
| 382 | 407 |
| 383 std::list<std::string> printer_ids; | 408 std::list<std::string> printer_ids; |
| 384 connector_->GetPrinterIds(&printer_ids); | 409 connector_->GetPrinterIds(&printer_ids); |
| 385 | 410 |
| 386 backend_->frontend_loop_->task_runner()->PostTask( | 411 PostFrontendTask(FROM_HERE, base::Bind(&Core::NotifyUnregisterPrinters, this, |
| 387 FROM_HERE, base::Bind(&Core::NotifyUnregisterPrinters, this, access_token, | 412 access_token, printer_ids)); |
| 388 printer_ids)); | |
| 389 } | 413 } |
| 390 | 414 |
| 391 void CloudPrintProxyBackend::Core::HandlePrinterNotification( | 415 void CloudPrintProxyBackend::Core::HandlePrinterNotification( |
| 392 const std::string& notification) { | 416 const std::string& notification) { |
| 393 DCHECK(base::MessageLoop::current() == backend_->core_thread_.message_loop()); | 417 DCHECK(CurrentlyOnCoreThread()); |
| 394 | 418 |
| 395 size_t pos = notification.rfind(kNotificationUpdateSettings); | 419 size_t pos = notification.rfind(kNotificationUpdateSettings); |
| 396 if (pos == std::string::npos) { | 420 if (pos == std::string::npos) { |
| 397 VLOG(1) << "CP_CONNECTOR: Handle printer notification, id: " | 421 VLOG(1) << "CP_CONNECTOR: Handle printer notification, id: " |
| 398 << notification; | 422 << notification; |
| 399 connector_->CheckForJobs(kJobFetchReasonNotified, notification); | 423 connector_->CheckForJobs(kJobFetchReasonNotified, notification); |
| 400 } else { | 424 } else { |
| 401 DCHECK(pos == notification.length() - strlen(kNotificationUpdateSettings)); | 425 DCHECK(pos == notification.length() - strlen(kNotificationUpdateSettings)); |
| 402 std::string printer_id = notification.substr(0, pos); | 426 std::string printer_id = notification.substr(0, pos); |
| 403 VLOG(1) << "CP_CONNECTOR: Update printer settings, id: " << printer_id; | 427 VLOG(1) << "CP_CONNECTOR: Update printer settings, id: " << printer_id; |
| 404 connector_->UpdatePrinterSettings(printer_id); | 428 connector_->UpdatePrinterSettings(printer_id); |
| 405 } | 429 } |
| 406 } | 430 } |
| 407 | 431 |
| 408 void CloudPrintProxyBackend::Core::PollForJobs() { | 432 void CloudPrintProxyBackend::Core::PollForJobs() { |
| 409 VLOG(1) << "CP_CONNECTOR: Polling for jobs."; | 433 VLOG(1) << "CP_CONNECTOR: Polling for jobs."; |
| 410 DCHECK(base::MessageLoop::current() == backend_->core_thread_.message_loop()); | 434 DCHECK(CurrentlyOnCoreThread()); |
| 411 // Check all printers for jobs. | 435 // Check all printers for jobs. |
| 412 connector_->CheckForJobs(kJobFetchReasonPoll, std::string()); | 436 connector_->CheckForJobs(kJobFetchReasonPoll, std::string()); |
| 413 | 437 |
| 414 job_poll_scheduled_ = false; | 438 job_poll_scheduled_ = false; |
| 415 // If we don't have notifications and job polling is enabled, poll again | 439 // If we don't have notifications and job polling is enabled, poll again |
| 416 // after a while. | 440 // after a while. |
| 417 if (!notifications_enabled_ && enable_job_poll_) | 441 if (!notifications_enabled_ && enable_job_poll_) |
| 418 ScheduleJobPoll(); | 442 ScheduleJobPoll(); |
| 419 } | 443 } |
| 420 | 444 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 470 if (pending_xmpp_pings_ >= kMaxFailedXmppPings) { | 494 if (pending_xmpp_pings_ >= kMaxFailedXmppPings) { |
| 471 UMA_HISTOGRAM_COUNTS_100("CloudPrint.XmppPingTry", 99); // Max on fail. | 495 UMA_HISTOGRAM_COUNTS_100("CloudPrint.XmppPingTry", 99); // Max on fail. |
| 472 // Reconnect to XMPP. | 496 // Reconnect to XMPP. |
| 473 pending_xmpp_pings_ = 0; | 497 pending_xmpp_pings_ = 0; |
| 474 push_client_.reset(); | 498 push_client_.reset(); |
| 475 InitNotifications(robot_email_, GetTokenStore()->token()); | 499 InitNotifications(robot_email_, GetTokenStore()->token()); |
| 476 } | 500 } |
| 477 } | 501 } |
| 478 | 502 |
| 479 CloudPrintTokenStore* CloudPrintProxyBackend::Core::GetTokenStore() { | 503 CloudPrintTokenStore* CloudPrintProxyBackend::Core::GetTokenStore() { |
| 480 DCHECK(base::MessageLoop::current() == backend_->core_thread_.message_loop()); | 504 DCHECK(CurrentlyOnCoreThread()); |
| 481 if (!token_store_.get()) | 505 if (!token_store_.get()) |
| 482 token_store_.reset(new CloudPrintTokenStore); | 506 token_store_.reset(new CloudPrintTokenStore); |
| 483 return token_store_.get(); | 507 return token_store_.get(); |
| 484 } | 508 } |
| 485 | 509 |
| 486 void CloudPrintProxyBackend::Core::NotifyAuthenticated( | 510 void CloudPrintProxyBackend::Core::NotifyAuthenticated( |
| 487 const std::string& robot_oauth_refresh_token, | 511 const std::string& robot_oauth_refresh_token, |
| 488 const std::string& robot_email, | 512 const std::string& robot_email, |
| 489 const std::string& user_email) { | 513 const std::string& user_email) { |
| 490 DCHECK(base::MessageLoop::current() == backend_->frontend_loop_); | 514 DCHECK(CurrentlyOnFrontendThread()); |
| 491 backend_->frontend_ | 515 frontend()->OnAuthenticated(robot_oauth_refresh_token, robot_email, |
| 492 ->OnAuthenticated(robot_oauth_refresh_token, robot_email, user_email); | 516 user_email); |
| 493 } | 517 } |
| 494 | 518 |
| 495 void CloudPrintProxyBackend::Core::NotifyAuthenticationFailed() { | 519 void CloudPrintProxyBackend::Core::NotifyAuthenticationFailed() { |
| 496 DCHECK(base::MessageLoop::current() == backend_->frontend_loop_); | 520 DCHECK(CurrentlyOnFrontendThread()); |
| 497 backend_->frontend_->OnAuthenticationFailed(); | 521 frontend()->OnAuthenticationFailed(); |
| 498 } | 522 } |
| 499 | 523 |
| 500 void CloudPrintProxyBackend::Core::NotifyPrintSystemUnavailable() { | 524 void CloudPrintProxyBackend::Core::NotifyPrintSystemUnavailable() { |
| 501 DCHECK(base::MessageLoop::current() == backend_->frontend_loop_); | 525 DCHECK(CurrentlyOnFrontendThread()); |
| 502 backend_->frontend_->OnPrintSystemUnavailable(); | 526 frontend()->OnPrintSystemUnavailable(); |
| 503 } | 527 } |
| 504 | 528 |
| 505 void CloudPrintProxyBackend::Core::NotifyUnregisterPrinters( | 529 void CloudPrintProxyBackend::Core::NotifyUnregisterPrinters( |
| 506 const std::string& auth_token, | 530 const std::string& auth_token, |
| 507 const std::list<std::string>& printer_ids) { | 531 const std::list<std::string>& printer_ids) { |
| 508 DCHECK(base::MessageLoop::current() == backend_->frontend_loop_); | 532 DCHECK(CurrentlyOnFrontendThread()); |
| 509 backend_->frontend_->OnUnregisterPrinters(auth_token, printer_ids); | 533 frontend()->OnUnregisterPrinters(auth_token, printer_ids); |
| 510 } | 534 } |
| 511 | 535 |
| 512 void CloudPrintProxyBackend::Core::NotifyXmppPingUpdated(int ping_timeout) { | 536 void CloudPrintProxyBackend::Core::NotifyXmppPingUpdated(int ping_timeout) { |
| 513 DCHECK(base::MessageLoop::current() == backend_->frontend_loop_); | 537 DCHECK(CurrentlyOnFrontendThread()); |
| 514 backend_->frontend_->OnXmppPingUpdated(ping_timeout); | 538 frontend()->OnXmppPingUpdated(ping_timeout); |
| 515 } | 539 } |
| 516 | 540 |
| 517 void CloudPrintProxyBackend::Core::OnNotificationsEnabled() { | 541 void CloudPrintProxyBackend::Core::OnNotificationsEnabled() { |
| 518 DCHECK(base::MessageLoop::current() == backend_->core_thread_.message_loop()); | 542 DCHECK(CurrentlyOnCoreThread()); |
| 519 notifications_enabled_ = true; | 543 notifications_enabled_ = true; |
| 520 notifications_enabled_since_ = base::TimeTicks::Now(); | 544 notifications_enabled_since_ = base::TimeTicks::Now(); |
| 521 VLOG(1) << "Notifications for connector " << settings_.proxy_id() | 545 VLOG(1) << "Notifications for connector " << settings_.proxy_id() |
| 522 << " were enabled at " | 546 << " were enabled at " |
| 523 << notifications_enabled_since_.ToInternalValue(); | 547 << notifications_enabled_since_.ToInternalValue(); |
| 524 // Notifications just got re-enabled. In this case we want to schedule | 548 // Notifications just got re-enabled. In this case we want to schedule |
| 525 // a poll once for jobs we might have missed when we were dark. | 549 // a poll once for jobs we might have missed when we were dark. |
| 526 // Note that ScheduleJobPoll will not schedule again if a job poll task is | 550 // Note that ScheduleJobPoll will not schedule again if a job poll task is |
| 527 // already scheduled. | 551 // already scheduled. |
| 528 ScheduleJobPoll(); | 552 ScheduleJobPoll(); |
| 529 | 553 |
| 530 // Schedule periodic ping for XMPP notification channel. | 554 // Schedule periodic ping for XMPP notification channel. |
| 531 ScheduleXmppPing(); | 555 ScheduleXmppPing(); |
| 532 } | 556 } |
| 533 | 557 |
| 534 void CloudPrintProxyBackend::Core::OnNotificationsDisabled( | 558 void CloudPrintProxyBackend::Core::OnNotificationsDisabled( |
| 535 notifier::NotificationsDisabledReason reason) { | 559 notifier::NotificationsDisabledReason reason) { |
| 536 DCHECK(base::MessageLoop::current() == backend_->core_thread_.message_loop()); | 560 DCHECK(CurrentlyOnCoreThread()); |
| 537 notifications_enabled_ = false; | 561 notifications_enabled_ = false; |
| 538 LOG(ERROR) << "Notifications for connector " << settings_.proxy_id() | 562 LOG(ERROR) << "Notifications for connector " << settings_.proxy_id() |
| 539 << " disabled."; | 563 << " disabled."; |
| 540 notifications_enabled_since_ = base::TimeTicks(); | 564 notifications_enabled_since_ = base::TimeTicks(); |
| 541 // We just lost notifications. This this case we want to schedule a | 565 // We just lost notifications. This this case we want to schedule a |
| 542 // job poll if enable_job_poll_ is true. | 566 // job poll if enable_job_poll_ is true. |
| 543 if (enable_job_poll_) | 567 if (enable_job_poll_) |
| 544 ScheduleJobPoll(); | 568 ScheduleJobPoll(); |
| 545 } | 569 } |
| 546 | 570 |
| 547 | 571 |
| 548 void CloudPrintProxyBackend::Core::OnIncomingNotification( | 572 void CloudPrintProxyBackend::Core::OnIncomingNotification( |
| 549 const notifier::Notification& notification) { | 573 const notifier::Notification& notification) { |
| 574 DCHECK(CurrentlyOnCoreThread()); | |
| 575 | |
| 550 // Since we got some notification from the server, | 576 // Since we got some notification from the server, |
| 551 // reset pending ping counter to 0. | 577 // reset pending ping counter to 0. |
| 552 pending_xmpp_pings_ = 0; | 578 pending_xmpp_pings_ = 0; |
| 553 | 579 |
| 554 DCHECK(base::MessageLoop::current() == backend_->core_thread_.message_loop()); | |
| 555 VLOG(1) << "CP_CONNECTOR: Incoming notification."; | 580 VLOG(1) << "CP_CONNECTOR: Incoming notification."; |
| 556 if (base::EqualsCaseInsensitiveASCII(kCloudPrintPushNotificationsSource, | 581 if (base::EqualsCaseInsensitiveASCII(kCloudPrintPushNotificationsSource, |
| 557 notification.channel)) | 582 notification.channel)) |
| 558 HandlePrinterNotification(notification.data); | 583 HandlePrinterNotification(notification.data); |
| 559 } | 584 } |
| 560 | 585 |
| 561 void CloudPrintProxyBackend::Core::OnPingResponse() { | 586 void CloudPrintProxyBackend::Core::OnPingResponse() { |
| 562 UMA_HISTOGRAM_COUNTS_100("CloudPrint.XmppPingTry", pending_xmpp_pings_); | 587 UMA_HISTOGRAM_COUNTS_100("CloudPrint.XmppPingTry", pending_xmpp_pings_); |
| 563 pending_xmpp_pings_ = 0; | 588 pending_xmpp_pings_ = 0; |
| 564 VLOG(1) << "CP_CONNECTOR: Ping response received."; | 589 VLOG(1) << "CP_CONNECTOR: Ping response received."; |
| 565 } | 590 } |
| 566 | 591 |
| 567 } // namespace cloud_print | 592 } // namespace cloud_print |
| OLD | NEW |