summaryrefslogtreecommitdiffstats
path: root/chrome/service/cloud_print
diff options
context:
space:
mode:
authorsanjeevr@chromium.org <sanjeevr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-05-13 22:38:40 +0000
committersanjeevr@chromium.org <sanjeevr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-05-13 22:38:40 +0000
commitb6f4df1aab6caf8d40c24887caf2ebb55dc03326 (patch)
tree26e515a24135d1159935ab0280b92659e976c132 /chrome/service/cloud_print
parent4f2429689545ae627fcce169dd0f2a500d647cd8 (diff)
downloadchromium_src-b6f4df1aab6caf8d40c24887caf2ebb55dc03326.zip
chromium_src-b6f4df1aab6caf8d40c24887caf2ebb55dc03326.tar.gz
chromium_src-b6f4df1aab6caf8d40c24887caf2ebb55dc03326.tar.bz2
Created an IPC method to enable the cloud print proxy using robot OAuth2 credentials.
Used this method in the browser. This will be used by the new login scheme for Cloud Print. BUG=None TEST=None Review URL: http://codereview.chromium.org/6969055 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@85337 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/service/cloud_print')
-rw-r--r--chrome/service/cloud_print/cloud_print_proxy.cc116
-rw-r--r--chrome/service/cloud_print/cloud_print_proxy.h8
-rw-r--r--chrome/service/cloud_print/cloud_print_proxy_backend.cc37
-rw-r--r--chrome/service/cloud_print/cloud_print_proxy_backend.h4
4 files changed, 117 insertions, 48 deletions
diff --git a/chrome/service/cloud_print/cloud_print_proxy.cc b/chrome/service/cloud_print/cloud_print_proxy.cc
index 39ffffc..2ade634 100644
--- a/chrome/service/cloud_print/cloud_print_proxy.cc
+++ b/chrome/service/cloud_print/cloud_print_proxy.cc
@@ -61,14 +61,74 @@ void CloudPrintProxy::Initialize(ServiceProcessPrefs* service_prefs,
void CloudPrintProxy::EnableForUser(const std::string& lsid) {
DCHECK(CalledOnValidThread());
- if (backend_.get())
+ if (!CreateBackend())
+ return;
+ DCHECK(backend_.get());
+ // Read persisted robot credentials because we may decide to reuse it if the
+ // passed in LSID belongs the same user.
+ std::string robot_refresh_token;
+ service_prefs_->GetString(prefs::kCloudPrintRobotRefreshToken,
+ &robot_refresh_token);
+ std::string robot_email;
+ service_prefs_->GetString(prefs::kCloudPrintRobotEmail,
+ &robot_email);
+ service_prefs_->GetString(prefs::kCloudPrintEmail, &user_email_);
+
+ // If we have been passed in an LSID, we want to use this to authenticate.
+ // Else we will try and retrieve the last used auth tokens from prefs.
+ if (!lsid.empty()) {
+ backend_->InitializeWithLsid(lsid,
+ proxy_id_,
+ robot_refresh_token,
+ robot_email,
+ user_email_);
+ } else {
+ // See if we have persisted robot credentials.
+ if (!robot_refresh_token.empty()) {
+ DCHECK(!robot_email.empty());
+ backend_->InitializeWithRobotToken(robot_refresh_token,
+ robot_email,
+ proxy_id_);
+ } else {
+ // Finally see if we have persisted user credentials (legacy case).
+ std::string cloud_print_token;
+ service_prefs_->GetString(prefs::kCloudPrintAuthToken,
+ &cloud_print_token);
+ DCHECK(!cloud_print_token.empty());
+ backend_->InitializeWithToken(cloud_print_token, proxy_id_);
+ }
+ }
+ if (client_) {
+ client_->OnCloudPrintProxyEnabled(true);
+ }
+}
+
+void CloudPrintProxy::EnableForUserWithRobot(
+ const std::string& robot_auth_code,
+ const std::string& robot_email,
+ const std::string& user_email) {
+ DCHECK(CalledOnValidThread());
+ if (!CreateBackend())
return;
+ DCHECK(backend_.get());
+ user_email_ = user_email;
+ backend_->InitializeWithRobotAuthCode(robot_auth_code,
+ robot_email,
+ proxy_id_);
+ if (client_) {
+ client_->OnCloudPrintProxyEnabled(true);
+ }
+}
- std::string proxy_id;
- service_prefs_->GetString(prefs::kCloudPrintProxyId, &proxy_id);
- if (proxy_id.empty()) {
- proxy_id = cloud_print::PrintSystem::GenerateProxyId();
- service_prefs_->SetString(prefs::kCloudPrintProxyId, proxy_id);
+bool CloudPrintProxy::CreateBackend() {
+ DCHECK(CalledOnValidThread());
+ if (backend_.get())
+ return false;
+
+ service_prefs_->GetString(prefs::kCloudPrintProxyId, &proxy_id_);
+ if (proxy_id_.empty()) {
+ proxy_id_ = cloud_print::PrintSystem::GenerateProxyId();
+ service_prefs_->SetString(prefs::kCloudPrintProxyId, proxy_id_);
service_prefs_->WritePrefs();
}
@@ -84,7 +144,6 @@ void CloudPrintProxy::EnableForUser(const std::string& lsid) {
if (cloud_print_server_url_str.empty()) {
cloud_print_server_url_str = kDefaultCloudPrintServerUrl;
}
- service_prefs_->GetString(prefs::kCloudPrintEmail, &user_email_);
// By default we don't poll for jobs when we lose XMPP connection. But this
// behavior can be overridden by a preference.
@@ -103,42 +162,7 @@ void CloudPrintProxy::EnableForUser(const std::string& lsid) {
print_system_settings,
oauth_client_info,
enable_job_poll));
- // Read persisted robot credentials because we may decide to reuse it if the
- // passed in LSID belongs the same user.
- std::string robot_refresh_token;
- service_prefs_->GetString(prefs::kCloudPrintRobotRefreshToken,
- &robot_refresh_token);
- std::string robot_email;
- service_prefs_->GetString(prefs::kCloudPrintRobotEmail,
- &robot_email);
-
- // If we have been passed in an LSID, we want to use this to authenticate.
- // Else we will try and retrieve the last used auth tokens from prefs.
- if (!lsid.empty()) {
- backend_->InitializeWithLsid(lsid,
- proxy_id,
- robot_refresh_token,
- robot_email,
- user_email_);
- } else {
- // See if we have persisted robot credentials.
- if (!robot_refresh_token.empty()) {
- DCHECK(!robot_email.empty());
- backend_->InitializeWithRobotToken(robot_refresh_token,
- robot_email,
- proxy_id);
- } else {
- // Finally see if we have persisted user credentials (legacy case).
- std::string cloud_print_token;
- service_prefs_->GetString(prefs::kCloudPrintAuthToken,
- &cloud_print_token);
- DCHECK(!cloud_print_token.empty());
- backend_->InitializeWithToken(cloud_print_token, proxy_id);
- }
- }
- if (client_) {
- client_->OnCloudPrintProxyEnabled(true);
- }
+ return true;
}
void CloudPrintProxy::DisableForUser() {
@@ -154,11 +178,9 @@ void CloudPrintProxy::DisableForUser() {
void CloudPrintProxy::GetProxyInfo(cloud_print::CloudPrintProxyInfo* info) {
info->enabled = enabled_;
info->email.clear();
- info->proxy_id.clear();
if (enabled_)
info->email = user_email();
- if (service_prefs_)
- service_prefs_->GetString(prefs::kCloudPrintProxyId, &info->proxy_id);
+ info->proxy_id = proxy_id_;
}
// Notification methods from the backend. Called on UI thread.
@@ -182,8 +204,8 @@ void CloudPrintProxy::OnAuthenticated(
// If authenticating from a robot, the user email will be empty.
if (!user_email.empty()) {
user_email_ = user_email;
- service_prefs_->SetString(prefs::kCloudPrintEmail, user_email);
}
+ service_prefs_->SetString(prefs::kCloudPrintEmail, user_email_);
enabled_ = true;
DCHECK(!user_email_.empty());
service_prefs_->WritePrefs();
diff --git a/chrome/service/cloud_print/cloud_print_proxy.h b/chrome/service/cloud_print/cloud_print_proxy.h
index 8510a5b..f9e80ef 100644
--- a/chrome/service/cloud_print/cloud_print_proxy.h
+++ b/chrome/service/cloud_print/cloud_print_proxy.h
@@ -39,6 +39,10 @@ class CloudPrintProxy : public CloudPrintProxyFrontend,
// Enables/disables cloud printing for the user
void EnableForUser(const std::string& lsid);
+ void EnableForUserWithRobot(
+ const std::string& robot_auth_code,
+ const std::string& robot_email,
+ const std::string& user_email);
void DisableForUser();
// Returns the proxy info.
void GetProxyInfo(cloud_print::CloudPrintProxyInfo* info);
@@ -58,6 +62,7 @@ class CloudPrintProxy : public CloudPrintProxyFrontend,
protected:
void Shutdown();
+ bool CreateBackend();
// Our asynchronous backend to communicate with sync components living on
// other threads.
@@ -74,6 +79,9 @@ class CloudPrintProxy : public CloudPrintProxyFrontend,
// This is set to true when the Cloud Print proxy is enabled and after
// successful authentication with the Cloud Print service.
bool enabled_;
+ // This is initialized after a successful call to one of the Enable* methods.
+ // It is not cleared in DisableUser.
+ std::string proxy_id_;
DISALLOW_COPY_AND_ASSIGN(CloudPrintProxy);
};
diff --git a/chrome/service/cloud_print/cloud_print_proxy_backend.cc b/chrome/service/cloud_print/cloud_print_proxy_backend.cc
index ea9e09f..a1399eb 100644
--- a/chrome/service/cloud_print/cloud_print_proxy_backend.cc
+++ b/chrome/service/cloud_print/cloud_print_proxy_backend.cc
@@ -69,6 +69,9 @@ class CloudPrintProxyBackend::Core
void DoInitializeWithRobotToken(const std::string& robot_oauth_refresh_token,
const std::string& robot_email,
const std::string& proxy_id);
+ void DoInitializeWithRobotAuthCode(const std::string& robot_oauth_auth_code,
+ const std::string& robot_email,
+ const std::string& proxy_id);
// Called on the CloudPrintProxyBackend core_thread_ to perform
// shutdown.
@@ -340,6 +343,21 @@ bool CloudPrintProxyBackend::InitializeWithRobotToken(
return true;
}
+bool CloudPrintProxyBackend::InitializeWithRobotAuthCode(
+ const std::string& robot_oauth_auth_code,
+ const std::string& robot_email,
+ const std::string& proxy_id) {
+ if (!core_thread_.Start())
+ return false;
+ core_thread_.message_loop()->PostTask(FROM_HERE,
+ NewRunnableMethod(
+ core_.get(),
+ &CloudPrintProxyBackend::Core::DoInitializeWithRobotAuthCode,
+ robot_oauth_auth_code,
+ robot_email,
+ proxy_id));
+ return true;
+}
void CloudPrintProxyBackend::Shutdown() {
core_thread_.message_loop()->PostTask(FROM_HERE,
@@ -449,6 +467,22 @@ void CloudPrintProxyBackend::Core::DoInitializeWithRobotToken(
RefreshAccessToken();
}
+void CloudPrintProxyBackend::Core::DoInitializeWithRobotAuthCode(
+ const std::string& robot_oauth_auth_code,
+ const std::string& robot_email,
+ const std::string& proxy_id) {
+ robot_email_ = robot_email;
+ proxy_id_ = proxy_id;
+ // Now that we have an auth code we need to get the refresh and access tokens.
+ oauth_client_.reset(new gaia::GaiaOAuthClient(
+ gaia::kGaiaOAuth2Url,
+ g_service_process->GetServiceURLRequestContextGetter()));
+ oauth_client_->GetTokensFromAuthCode(oauth_client_info_,
+ robot_oauth_auth_code,
+ kCloudPrintAPIMaxRetryCount,
+ this);
+}
+
void CloudPrintProxyBackend::Core::PostAuthInitialization() {
DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop());
// Now we can get down to registering printers.
@@ -539,7 +573,8 @@ void CloudPrintProxyBackend::Core::DoShutdown() {
index->second->Shutdown();
}
// Important to delete the TalkMediator on this thread.
- talk_mediator_->Logout();
+ if (talk_mediator_.get())
+ talk_mediator_->Logout();
talk_mediator_.reset();
notifications_enabled_ = false;
notifications_enabled_since_ = base::TimeTicks();
diff --git a/chrome/service/cloud_print/cloud_print_proxy_backend.h b/chrome/service/cloud_print/cloud_print_proxy_backend.h
index 71cf92f..603d4cd 100644
--- a/chrome/service/cloud_print/cloud_print_proxy_backend.h
+++ b/chrome/service/cloud_print/cloud_print_proxy_backend.h
@@ -78,6 +78,10 @@ class CloudPrintProxyBackend {
bool InitializeWithRobotToken(const std::string& robot_oauth_refresh_token,
const std::string& robot_email,
const std::string& proxy_id);
+ // Called when an external entity passed in the auth code for the robot.
+ bool InitializeWithRobotAuthCode(const std::string& robot_oauth_auth_code,
+ const std::string& robot_email,
+ const std::string& proxy_id);
void Shutdown();
void RegisterPrinters(const printing::PrinterList& printer_list);