Chromium Code Reviews| Index: chrome/service/cloud_print/cloud_print_proxy_backend.cc |
| diff --git a/chrome/service/cloud_print/cloud_print_proxy_backend.cc b/chrome/service/cloud_print/cloud_print_proxy_backend.cc |
| index bc09b0701997065a6183570a9dfccf8c45ecbc1a..bcc0900671796ed98c9f576510edbe9b61ad22fd 100644 |
| --- a/chrome/service/cloud_print/cloud_print_proxy_backend.cc |
| +++ b/chrome/service/cloud_print/cloud_print_proxy_backend.cc |
| @@ -63,7 +63,7 @@ class CloudPrintProxyBackend::Core |
| void DoInitializeWithRobotAuthCode(const std::string& robot_oauth_auth_code, |
| const std::string& robot_email); |
| - // Called on the CloudPrintProxyBackend core_thread_ to perform |
| + // Called on the CloudPrintProxyBackend |core_thread_| to perform |
| // 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.
|
| void DoShutdown(); |
| void DoRegisterSelectedPrinters( |
| @@ -94,6 +94,14 @@ class CloudPrintProxyBackend::Core |
| ~Core() override {} |
| + CloudPrintProxyFrontend* frontend() { return backend_->frontend_; } |
| + |
| + bool PostFrontendTask(const tracked_objects::Location& from_here, |
| + const base::Closure& task); |
| + |
| + bool CurrentlyOnFrontendThread() const; |
| + bool CurrentlyOnCoreThread() const; |
| + |
| void CreateAuthAndConnector(); |
| void DestroyAuthAndConnector(); |
| @@ -127,7 +135,7 @@ class CloudPrintProxyBackend::Core |
| CloudPrintTokenStore* GetTokenStore(); |
| // Our parent CloudPrintProxyBackend |
| - CloudPrintProxyBackend* backend_; |
| + CloudPrintProxyBackend* const backend_; |
| // Cloud Print authenticator. |
| scoped_refptr<CloudPrintAuth> auth_; |
| @@ -178,10 +186,9 @@ bool CloudPrintProxyBackend::InitializeWithToken( |
| const std::string& cloud_print_token) { |
| if (!core_thread_.Start()) |
| return false; |
| - core_thread_.task_runner()->PostTask( |
| - FROM_HERE, |
| - base::Bind(&CloudPrintProxyBackend::Core::DoInitializeWithToken, |
| - core_.get(), cloud_print_token)); |
| + PostCoreTask(FROM_HERE, |
| + base::Bind(&CloudPrintProxyBackend::Core::DoInitializeWithToken, |
| + core_.get(), cloud_print_token)); |
| return true; |
| } |
| @@ -190,7 +197,7 @@ bool CloudPrintProxyBackend::InitializeWithRobotToken( |
| const std::string& robot_email) { |
| if (!core_thread_.Start()) |
| return false; |
| - core_thread_.task_runner()->PostTask( |
| + PostCoreTask( |
| FROM_HERE, |
| base::Bind(&CloudPrintProxyBackend::Core::DoInitializeWithRobotToken, |
| core_.get(), robot_oauth_refresh_token, robot_email)); |
| @@ -202,7 +209,7 @@ bool CloudPrintProxyBackend::InitializeWithRobotAuthCode( |
| const std::string& robot_email) { |
| if (!core_thread_.Start()) |
| return false; |
| - core_thread_.task_runner()->PostTask( |
| + PostCoreTask( |
| FROM_HERE, |
| base::Bind(&CloudPrintProxyBackend::Core::DoInitializeWithRobotAuthCode, |
| core_.get(), robot_oauth_auth_code, robot_email)); |
| @@ -210,17 +217,22 @@ bool CloudPrintProxyBackend::InitializeWithRobotAuthCode( |
| } |
| void CloudPrintProxyBackend::Shutdown() { |
| - core_thread_.task_runner()->PostTask( |
| - FROM_HERE, |
| - base::Bind(&CloudPrintProxyBackend::Core::DoShutdown, core_.get())); |
| + PostCoreTask(FROM_HERE, base::Bind(&CloudPrintProxyBackend::Core::DoShutdown, |
| + core_.get())); |
| core_thread_.Stop(); |
| - core_ = NULL; // Releases reference to core_. |
| + core_ = nullptr; // Releases reference to |core_|. |
| } |
| void CloudPrintProxyBackend::UnregisterPrinters() { |
| - core_thread_.task_runner()->PostTask( |
| - FROM_HERE, base::Bind(&CloudPrintProxyBackend::Core::DoUnregisterPrinters, |
| - core_.get())); |
| + PostCoreTask(FROM_HERE, |
| + base::Bind(&CloudPrintProxyBackend::Core::DoUnregisterPrinters, |
| + core_.get())); |
| +} |
| + |
| +bool CloudPrintProxyBackend::PostCoreTask( |
| + const tracked_objects::Location& from_here, |
| + const base::Closure& task) { |
| + return core_thread_.task_runner()->PostTask(from_here, task); |
| } |
| CloudPrintProxyBackend::Core::Core( |
| @@ -238,6 +250,20 @@ CloudPrintProxyBackend::Core::Core( |
| settings_.CopyFrom(settings); |
| } |
| +bool CloudPrintProxyBackend::Core::PostFrontendTask( |
| + const tracked_objects::Location& from_here, |
| + const base::Closure& task) { |
| + return backend_->frontend_loop_->task_runner()->PostTask(from_here, task); |
| +} |
| + |
| +bool CloudPrintProxyBackend::Core::CurrentlyOnFrontendThread() const { |
| + return base::MessageLoop::current() == backend_->frontend_loop_; |
| +} |
| + |
| +bool CloudPrintProxyBackend::Core::CurrentlyOnCoreThread() const { |
| + return base::MessageLoop::current() == backend_->core_thread_.message_loop(); |
| +} |
| + |
| void CloudPrintProxyBackend::Core::CreateAuthAndConnector() { |
| if (!auth_.get()) { |
| auth_ = new CloudPrintAuth(this, settings_.server_url(), oauth_client_info_, |
| @@ -256,7 +282,7 @@ void CloudPrintProxyBackend::Core::DestroyAuthAndConnector() { |
| void CloudPrintProxyBackend::Core::DoInitializeWithToken( |
| const std::string& cloud_print_token) { |
| - DCHECK(base::MessageLoop::current() == backend_->core_thread_.message_loop()); |
| + DCHECK(CurrentlyOnCoreThread()); |
| CreateAuthAndConnector(); |
| auth_->AuthenticateWithToken(cloud_print_token); |
| } |
| @@ -264,7 +290,7 @@ void CloudPrintProxyBackend::Core::DoInitializeWithToken( |
| void CloudPrintProxyBackend::Core::DoInitializeWithRobotToken( |
| const std::string& robot_oauth_refresh_token, |
| const std::string& robot_email) { |
| - DCHECK(base::MessageLoop::current() == backend_->core_thread_.message_loop()); |
| + DCHECK(CurrentlyOnCoreThread()); |
| CreateAuthAndConnector(); |
| auth_->AuthenticateWithRobotToken(robot_oauth_refresh_token, robot_email); |
| } |
| @@ -272,7 +298,7 @@ void CloudPrintProxyBackend::Core::DoInitializeWithRobotToken( |
| void CloudPrintProxyBackend::Core::DoInitializeWithRobotAuthCode( |
| const std::string& robot_oauth_auth_code, |
| const std::string& robot_email) { |
| - DCHECK(base::MessageLoop::current() == backend_->core_thread_.message_loop()); |
| + DCHECK(CurrentlyOnCoreThread()); |
| CreateAuthAndConnector(); |
| auth_->AuthenticateWithRobotAuthCode(robot_oauth_auth_code, robot_email); |
| } |
| @@ -287,10 +313,9 @@ void CloudPrintProxyBackend::Core::OnAuthenticationComplete( |
| token_store->SetToken(access_token); |
| robot_email_ = robot_email; |
| // Let the frontend know that we have authenticated. |
| - backend_->frontend_loop_->task_runner()->PostTask( |
| - FROM_HERE, |
| - base::Bind(&Core::NotifyAuthenticated, this, robot_oauth_refresh_token, |
| - robot_email, user_email)); |
| + PostFrontendTask(FROM_HERE, base::Bind(&Core::NotifyAuthenticated, this, |
| + robot_oauth_refresh_token, robot_email, |
| + user_email)); |
| if (first_time) { |
| InitNotifications(robot_email, access_token); |
| } else { |
| @@ -302,17 +327,17 @@ void CloudPrintProxyBackend::Core::OnAuthenticationComplete( |
| if (!connector_->IsRunning()) { |
| if (!connector_->Start()) { |
| // Let the frontend know that we do not have a print system. |
| - backend_->frontend_loop_->task_runner()->PostTask( |
| - FROM_HERE, base::Bind(&Core::NotifyPrintSystemUnavailable, this)); |
| + PostFrontendTask(FROM_HERE, |
| + base::Bind(&Core::NotifyPrintSystemUnavailable, this)); |
| } |
| } |
| } |
| void CloudPrintProxyBackend::Core::OnInvalidCredentials() { |
| - DCHECK(base::MessageLoop::current() == backend_->core_thread_.message_loop()); |
| + DCHECK(CurrentlyOnCoreThread()); |
| VLOG(1) << "CP_CONNECTOR: Auth Error"; |
| - backend_->frontend_loop_->task_runner()->PostTask( |
| - FROM_HERE, base::Bind(&Core::NotifyAuthenticationFailed, this)); |
| + PostFrontendTask(FROM_HERE, |
| + base::Bind(&Core::NotifyAuthenticationFailed, this)); |
| } |
| void CloudPrintProxyBackend::Core::OnAuthFailed() { |
| @@ -328,14 +353,14 @@ void CloudPrintProxyBackend::Core::OnAuthFailed() { |
| void CloudPrintProxyBackend::Core::OnXmppPingUpdated(int ping_timeout) { |
| settings_.SetXmppPingTimeoutSec(ping_timeout); |
| - backend_->frontend_loop_->task_runner()->PostTask( |
| + PostFrontendTask( |
| FROM_HERE, base::Bind(&Core::NotifyXmppPingUpdated, this, ping_timeout)); |
| } |
| void CloudPrintProxyBackend::Core::InitNotifications( |
| const std::string& robot_email, |
| const std::string& access_token) { |
| - DCHECK(base::MessageLoop::current() == backend_->core_thread_.message_loop()); |
| + DCHECK(CurrentlyOnCoreThread()); |
| pending_xmpp_pings_ = 0; |
| notifier::NotifierOptions notifier_options; |
| @@ -357,7 +382,7 @@ void CloudPrintProxyBackend::Core::InitNotifications( |
| } |
| void CloudPrintProxyBackend::Core::DoShutdown() { |
| - DCHECK(base::MessageLoop::current() == backend_->core_thread_.message_loop()); |
| + DCHECK(CurrentlyOnCoreThread()); |
| VLOG(1) << "CP_CONNECTOR: Shutdown connector, id: " << settings_.proxy_id(); |
| if (connector_->IsRunning()) |
| @@ -376,21 +401,20 @@ void CloudPrintProxyBackend::Core::DoShutdown() { |
| } |
| void CloudPrintProxyBackend::Core::DoUnregisterPrinters() { |
| - DCHECK(base::MessageLoop::current() == backend_->core_thread_.message_loop()); |
| + DCHECK(CurrentlyOnCoreThread()); |
| std::string access_token = GetTokenStore()->token(); |
| std::list<std::string> printer_ids; |
| connector_->GetPrinterIds(&printer_ids); |
| - backend_->frontend_loop_->task_runner()->PostTask( |
| - FROM_HERE, base::Bind(&Core::NotifyUnregisterPrinters, this, access_token, |
| - printer_ids)); |
| + PostFrontendTask(FROM_HERE, base::Bind(&Core::NotifyUnregisterPrinters, this, |
| + access_token, printer_ids)); |
| } |
| void CloudPrintProxyBackend::Core::HandlePrinterNotification( |
| const std::string& notification) { |
| - DCHECK(base::MessageLoop::current() == backend_->core_thread_.message_loop()); |
| + DCHECK(CurrentlyOnCoreThread()); |
| size_t pos = notification.rfind(kNotificationUpdateSettings); |
| if (pos == std::string::npos) { |
| @@ -407,7 +431,7 @@ void CloudPrintProxyBackend::Core::HandlePrinterNotification( |
| void CloudPrintProxyBackend::Core::PollForJobs() { |
| VLOG(1) << "CP_CONNECTOR: Polling for jobs."; |
| - DCHECK(base::MessageLoop::current() == backend_->core_thread_.message_loop()); |
| + DCHECK(CurrentlyOnCoreThread()); |
| // Check all printers for jobs. |
| connector_->CheckForJobs(kJobFetchReasonPoll, std::string()); |
| @@ -477,7 +501,7 @@ void CloudPrintProxyBackend::Core::CheckXmppPingStatus() { |
| } |
| CloudPrintTokenStore* CloudPrintProxyBackend::Core::GetTokenStore() { |
| - DCHECK(base::MessageLoop::current() == backend_->core_thread_.message_loop()); |
| + DCHECK(CurrentlyOnCoreThread()); |
| if (!token_store_.get()) |
| token_store_.reset(new CloudPrintTokenStore); |
| return token_store_.get(); |
| @@ -487,35 +511,35 @@ void CloudPrintProxyBackend::Core::NotifyAuthenticated( |
| const std::string& robot_oauth_refresh_token, |
| const std::string& robot_email, |
| const std::string& user_email) { |
| - DCHECK(base::MessageLoop::current() == backend_->frontend_loop_); |
| - backend_->frontend_ |
| - ->OnAuthenticated(robot_oauth_refresh_token, robot_email, user_email); |
| + DCHECK(CurrentlyOnFrontendThread()); |
| + frontend()->OnAuthenticated(robot_oauth_refresh_token, robot_email, |
| + user_email); |
| } |
| void CloudPrintProxyBackend::Core::NotifyAuthenticationFailed() { |
| - DCHECK(base::MessageLoop::current() == backend_->frontend_loop_); |
| - backend_->frontend_->OnAuthenticationFailed(); |
| + DCHECK(CurrentlyOnFrontendThread()); |
| + frontend()->OnAuthenticationFailed(); |
| } |
| void CloudPrintProxyBackend::Core::NotifyPrintSystemUnavailable() { |
| - DCHECK(base::MessageLoop::current() == backend_->frontend_loop_); |
| - backend_->frontend_->OnPrintSystemUnavailable(); |
| + DCHECK(CurrentlyOnFrontendThread()); |
| + frontend()->OnPrintSystemUnavailable(); |
| } |
| void CloudPrintProxyBackend::Core::NotifyUnregisterPrinters( |
| const std::string& auth_token, |
| const std::list<std::string>& printer_ids) { |
| - DCHECK(base::MessageLoop::current() == backend_->frontend_loop_); |
| - backend_->frontend_->OnUnregisterPrinters(auth_token, printer_ids); |
| + DCHECK(CurrentlyOnFrontendThread()); |
| + frontend()->OnUnregisterPrinters(auth_token, printer_ids); |
| } |
| void CloudPrintProxyBackend::Core::NotifyXmppPingUpdated(int ping_timeout) { |
| - DCHECK(base::MessageLoop::current() == backend_->frontend_loop_); |
| - backend_->frontend_->OnXmppPingUpdated(ping_timeout); |
| + DCHECK(CurrentlyOnFrontendThread()); |
| + frontend()->OnXmppPingUpdated(ping_timeout); |
| } |
| void CloudPrintProxyBackend::Core::OnNotificationsEnabled() { |
| - DCHECK(base::MessageLoop::current() == backend_->core_thread_.message_loop()); |
| + DCHECK(CurrentlyOnCoreThread()); |
| notifications_enabled_ = true; |
| notifications_enabled_since_ = base::TimeTicks::Now(); |
| VLOG(1) << "Notifications for connector " << settings_.proxy_id() |
| @@ -533,7 +557,7 @@ void CloudPrintProxyBackend::Core::OnNotificationsEnabled() { |
| void CloudPrintProxyBackend::Core::OnNotificationsDisabled( |
| notifier::NotificationsDisabledReason reason) { |
| - DCHECK(base::MessageLoop::current() == backend_->core_thread_.message_loop()); |
| + DCHECK(CurrentlyOnCoreThread()); |
| notifications_enabled_ = false; |
| LOG(ERROR) << "Notifications for connector " << settings_.proxy_id() |
| << " disabled."; |
| @@ -547,11 +571,12 @@ void CloudPrintProxyBackend::Core::OnNotificationsDisabled( |
| void CloudPrintProxyBackend::Core::OnIncomingNotification( |
| const notifier::Notification& notification) { |
| + DCHECK(CurrentlyOnCoreThread()); |
| + |
| // Since we got some notification from the server, |
| // reset pending ping counter to 0. |
| pending_xmpp_pings_ = 0; |
| - DCHECK(base::MessageLoop::current() == backend_->core_thread_.message_loop()); |
| VLOG(1) << "CP_CONNECTOR: Incoming notification."; |
| if (base::EqualsCaseInsensitiveASCII(kCloudPrintPushNotificationsSource, |
| notification.channel)) |