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