diff options
author | sanjeevr@chromium.org <sanjeevr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-21 19:09:55 +0000 |
---|---|---|
committer | sanjeevr@chromium.org <sanjeevr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-21 19:09:55 +0000 |
commit | 1e6c016987770754320842057097e2be9266011d (patch) | |
tree | e34f1122652e8d5fbe44df8108f2e873a0494d47 /chrome/service/cloud_print | |
parent | bbd8993d6edff252033397a88da1b47c770d0c11 (diff) | |
download | chromium_src-1e6c016987770754320842057097e2be9266011d.zip chromium_src-1e6c016987770754320842057097e2be9266011d.tar.gz chromium_src-1e6c016987770754320842057097e2be9266011d.tar.bz2 |
Added the ability in the service process to save and read state from a prefs file. Used this to save and read the cloud print proxy auth token and proxy id.
BUG=None.
TEST=Test Cloud Print Proxy (run it second time without LSID and ProxyId command-line args)
Review URL: http://codereview.chromium.org/2066018
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@47945 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/service/cloud_print')
-rw-r--r-- | chrome/service/cloud_print/cloud_print_proxy.cc | 70 | ||||
-rw-r--r-- | chrome/service/cloud_print/cloud_print_proxy.h | 26 | ||||
-rw-r--r-- | chrome/service/cloud_print/cloud_print_proxy_backend.cc | 113 | ||||
-rw-r--r-- | chrome/service/cloud_print/cloud_print_proxy_backend.h | 11 |
4 files changed, 155 insertions, 65 deletions
diff --git a/chrome/service/cloud_print/cloud_print_proxy.cc b/chrome/service/cloud_print/cloud_print_proxy.cc index aff851a..4a34177 100644 --- a/chrome/service/cloud_print/cloud_print_proxy.cc +++ b/chrome/service/cloud_print/cloud_print_proxy.cc @@ -4,37 +4,65 @@ #include "chrome/service/cloud_print/cloud_print_proxy.h" +#include "base/values.h" +#include "chrome/common/pref_names.h" +#include "chrome/common/json_pref_store.h" + CloudPrintProxy::CloudPrintProxy() { } CloudPrintProxy::~CloudPrintProxy() { + DCHECK(CalledOnValidThread()); Shutdown(); } -void CloudPrintProxy::Initialize() { +void CloudPrintProxy::Initialize(JsonPrefStore* service_prefs) { + DCHECK(CalledOnValidThread()); + service_prefs_ = service_prefs; } - -void CloudPrintProxy::EnableForUser(const std::string& lsid, - const std::string& proxy_id) { +void CloudPrintProxy::EnableForUser(const std::string& lsid) { + DCHECK(CalledOnValidThread()); if (backend_.get()) return; backend_.reset(new CloudPrintProxyBackend(this)); - backend_->Initialize(lsid, proxy_id); + std::string proxy_id; + service_prefs_->prefs()->GetString(prefs::kCloudPrintProxyId, &proxy_id); + if (proxy_id.empty()) { + proxy_id = cloud_print::GenerateProxyId(); + service_prefs_->prefs()->SetString(prefs::kCloudPrintProxyId, proxy_id); + service_prefs_->WritePrefs(); + } + // 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); + } else { + std::string cloud_print_token; + service_prefs_->prefs()->GetString(prefs::kCloudPrintAuthToken, + &cloud_print_token); + DCHECK(!cloud_print_token.empty()); + std::string cloud_print_xmpp_token; + service_prefs_->prefs()->GetString(prefs::kCloudPrintXMPPAuthToken, + &cloud_print_xmpp_token); + DCHECK(!cloud_print_xmpp_token.empty()); + std::string cloud_print_email; + service_prefs_->prefs()->GetString(prefs::kCloudPrintEmail, + &cloud_print_email); + DCHECK(!cloud_print_email.empty()); + backend_->InitializeWithToken(cloud_print_token, cloud_print_xmpp_token, + cloud_print_email, proxy_id); + } } void CloudPrintProxy::DisableForUser() { + DCHECK(CalledOnValidThread()); Shutdown(); } -void CloudPrintProxy::HandlePrinterNotification( - const std::string& printer_id) { - if (backend_.get()) - backend_->HandlePrinterNotification(printer_id); -} - void CloudPrintProxy::Shutdown() { + DCHECK(CalledOnValidThread()); if (backend_.get()) backend_->Shutdown(); backend_.reset(); @@ -43,10 +71,22 @@ void CloudPrintProxy::Shutdown() { // Notification methods from the backend. Called on UI thread. void CloudPrintProxy::OnPrinterListAvailable( const cloud_print::PrinterList& printer_list) { - // Here we will trim the list to eliminate printers already registered. - // If there are any more printers left in the list after trimming, we will - // show the print selection UI. Any printers left in the list after the user - // selection process will then be registered. + DCHECK(CalledOnValidThread()); + // We could potentially show UI here allowing the user to select which + // printers to register. For now, we just register all. backend_->RegisterPrinters(printer_list); } +void CloudPrintProxy::OnAuthenticated( + const std::string& cloud_print_token, + const std::string& cloud_print_xmpp_token, + const std::string& email) { + DCHECK(CalledOnValidThread()); + service_prefs_->prefs()->SetString(prefs::kCloudPrintAuthToken, + cloud_print_token); + service_prefs_->prefs()->SetString(prefs::kCloudPrintXMPPAuthToken, + cloud_print_xmpp_token); + service_prefs_->prefs()->SetString(prefs::kCloudPrintEmail, email); + service_prefs_->WritePrefs(); +} + diff --git a/chrome/service/cloud_print/cloud_print_proxy.h b/chrome/service/cloud_print/cloud_print_proxy.h index e76f0d9..4603d13 100644 --- a/chrome/service/cloud_print/cloud_print_proxy.h +++ b/chrome/service/cloud_print/cloud_print_proxy.h @@ -8,31 +8,34 @@ #include <string> #include "base/basictypes.h" +#include "base/non_thread_safe.h" #include "base/scoped_ptr.h" #include "chrome/service/cloud_print/cloud_print_proxy_backend.h" +class JsonPrefStore; + // CloudPrintProxy is the layer between the service process UI thread // and the cloud print proxy backend. -class CloudPrintProxy : public CloudPrintProxyFrontend { +class CloudPrintProxy : public CloudPrintProxyFrontend, + public NonThreadSafe { public: - explicit CloudPrintProxy(); + CloudPrintProxy(); virtual ~CloudPrintProxy(); // Initializes the object. This should be called every time an object of this // class is constructed. - void Initialize(); + void Initialize(JsonPrefStore* service_prefs); // Enables/disables cloud printing for the user - virtual void EnableForUser(const std::string& lsid, - const std::string& proxy_id); + virtual void EnableForUser(const std::string& lsid); virtual void DisableForUser(); - // Notification received from the server for a particular printer. - // We need to inform the backend to look for jobs for this printer. - void HandlePrinterNotification(const std::string& printer_id); - // Notification methods from the backend. Called on UI thread. - void OnPrinterListAvailable(const cloud_print::PrinterList& printer_list); + virtual void OnPrinterListAvailable( + const cloud_print::PrinterList& printer_list); + virtual void OnAuthenticated(const std::string& cloud_print_token, + const std::string& cloud_print_xmpp_token, + const std::string& email); protected: void Shutdown(); @@ -40,6 +43,9 @@ class CloudPrintProxy : public CloudPrintProxyFrontend { // Our asynchronous backend to communicate with sync components living on // other threads. scoped_ptr<CloudPrintProxyBackend> backend_; + // This class does not own this. It is guaranteed to remain valid for the + // lifetime of this class. + JsonPrefStore* service_prefs_; 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 eff9bcf..c3dd5ac 100644 --- a/chrome/service/cloud_print/cloud_print_proxy_backend.cc +++ b/chrome/service/cloud_print/cloud_print_proxy_backend.cc @@ -36,8 +36,15 @@ class CloudPrintProxyBackend::Core // (and potentially blocking) syncapi operations. // // Called on the CloudPrintProxyBackend core_thread_ to perform - // initialization - void DoInitialize(const std::string& lsid, const std::string& proxy_id); + // initialization. When we are passed in an LSID we authenticate using that + // and retrieve new auth tokens. + void DoInitializeWithLsid(const std::string& lsid, + const std::string& proxy_id); + void DoInitializeWithToken(const std::string cloud_print_token, + const std::string cloud_print_xmpp_token, + const std::string email, + const std::string& proxy_id); + // Called on the CloudPrintProxyBackend core_thread_ to perform // shutdown. void DoShutdown(); @@ -64,12 +71,6 @@ class CloudPrintProxyBackend::Core const std::string& printer_id); protected: - // FrontendNotification defines parameters for NotifyFrontend. Each enum - // value corresponds to the one CloudPrintProcyService method that - // NotifyFrontend should invoke. - enum FrontendNotification { - PRINTER_LIST_AVAILABLE, // OnPrinterListAvailable - }; // Prototype for a response handler. typedef void (CloudPrintProxyBackend::Core::*ResponseHandler)( const URLFetcher* source, const GURL& url, @@ -89,9 +90,14 @@ class CloudPrintProxyBackend::Core const std::string& data); // End response handlers - // NotifyFrontend is how the Core communicates with the frontend across + // NotifyXXX is how the Core communicates with the frontend across // threads. - void NotifyFrontend(FrontendNotification notification); + void NotifyPrinterListAvailable( + const cloud_print::PrinterList& printer_list); + void NotifyAuthenticated( + const std::string& cloud_print_token, + const std::string& cloud_print_xmpp_token, + const std::string& email); // Starts a new printer registration process. void StartRegistration(); // Ends the printer registration process. @@ -164,17 +170,31 @@ CloudPrintProxyBackend::~CloudPrintProxyBackend() { DCHECK(!core_); } -bool CloudPrintProxyBackend::Initialize(const std::string& lsid, - const std::string& proxy_id) { +bool CloudPrintProxyBackend::InitializeWithLsid(const std::string& lsid, + const std::string& proxy_id) { if (!core_thread_.Start()) return false; core_thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( - core_.get(), &CloudPrintProxyBackend::Core::DoInitialize, lsid, + core_.get(), &CloudPrintProxyBackend::Core::DoInitializeWithLsid, lsid, proxy_id)); return true; } +bool CloudPrintProxyBackend::InitializeWithToken( + const std::string cloud_print_token, + const std::string cloud_print_xmpp_token, + const std::string email, + const std::string& proxy_id) { + if (!core_thread_.Start()) + return false; + core_thread_.message_loop()->PostTask(FROM_HERE, + NewRunnableMethod( + core_.get(), &CloudPrintProxyBackend::Core::DoInitializeWithToken, + cloud_print_token, cloud_print_xmpp_token, email, proxy_id)); + return true; +} + void CloudPrintProxyBackend::Shutdown() { core_thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod(core_.get(), @@ -206,8 +226,8 @@ CloudPrintProxyBackend::Core::Core(CloudPrintProxyBackend* backend) next_response_handler_(NULL), new_printers_available_(false) { } -void CloudPrintProxyBackend::Core::DoInitialize(const std::string& lsid, - const std::string& proxy_id) { +void CloudPrintProxyBackend::Core::DoInitializeWithLsid( + const std::string& lsid, const std::string& proxy_id) { DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); // Since Talk does not accept a Cloud Print token, for now, we make 2 auth // requests, one for the chromiumsync service and another for print. This is @@ -230,21 +250,35 @@ void CloudPrintProxyBackend::Core::DoInitialize(const std::string& lsid, g_service_process->io_thread()->message_loop_proxy()); gaia_auth_for_print->set_message_loop(MessageLoop::current()); if (gaia_auth_for_print->AuthenticateWithLsid(lsid, true)) { - auth_token_ = gaia_auth_for_print->auth_token(); - talk_mediator_.reset(new notifier::TalkMediatorImpl( - g_service_process->network_change_notifier_thread(), false)); - talk_mediator_->AddSubscribedServiceUrl(kCloudPrintTalkServiceUrl); - talk_mediator_hookup_.reset( - NewEventListenerHookup( - talk_mediator_->channel(), - this, - &CloudPrintProxyBackend::Core::HandleTalkMediatorEvent)); - talk_mediator_->SetAuthToken(gaia_auth_for_talk->email(), - gaia_auth_for_talk->auth_token(), - kSyncGaiaServiceId); - talk_mediator_->Login(); + // Let the frontend know that we have authenticated. + backend_->frontend_loop_->PostTask(FROM_HERE, NewRunnableMethod(this, + &Core::NotifyAuthenticated, gaia_auth_for_print->auth_token(), + gaia_auth_for_talk->auth_token(), gaia_auth_for_talk->email())); + DoInitializeWithToken(gaia_auth_for_print->auth_token(), + gaia_auth_for_talk->auth_token(), + gaia_auth_for_talk->email(), + proxy_id); } } +} + +void CloudPrintProxyBackend::Core::DoInitializeWithToken( + const std::string cloud_print_token, + const std::string cloud_print_xmpp_token, + const std::string email, const std::string& proxy_id) { + DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); + // TODO(sanjeevr): Validate the tokens. + auth_token_ = cloud_print_token; + talk_mediator_.reset(new notifier::TalkMediatorImpl( + g_service_process->network_change_notifier_thread(), false)); + talk_mediator_->AddSubscribedServiceUrl(kCloudPrintTalkServiceUrl); + talk_mediator_hookup_.reset( + NewEventListenerHookup( + talk_mediator_->channel(), this, + &CloudPrintProxyBackend::Core::HandleTalkMediatorEvent)); + talk_mediator_->SetAuthToken(email, cloud_print_xmpp_token, + kSyncGaiaServiceId); + talk_mediator_->Login(); printer_change_notifier_.StartWatching(std::string(), this); proxy_id_ = proxy_id; StartRegistration(); @@ -381,16 +415,17 @@ void CloudPrintProxyBackend::Core::OnURLFetchComplete( cookies, data); } -void CloudPrintProxyBackend::Core::NotifyFrontend( - FrontendNotification notification) { - switch (notification) { - case PRINTER_LIST_AVAILABLE: - backend_->frontend_->OnPrinterListAvailable(printer_list_); - break; - default: - NOTREACHED(); - break; - } +void CloudPrintProxyBackend::Core::NotifyPrinterListAvailable( + const cloud_print::PrinterList& printer_list) { + backend_->frontend_->OnPrinterListAvailable(printer_list); +} + +void CloudPrintProxyBackend::Core::NotifyAuthenticated( + const std::string& cloud_print_token, + const std::string& cloud_print_xmpp_token, + const std::string& email) { + backend_->frontend_->OnAuthenticated(cloud_print_token, + cloud_print_xmpp_token, email); } void CloudPrintProxyBackend::Core::HandlePrinterListResponse( @@ -431,7 +466,7 @@ void CloudPrintProxyBackend::Core::HandlePrinterListResponse( if (!printer_list_.empty()) { // Let the frontend know that we have a list of printers available. backend_->frontend_loop_->PostTask(FROM_HERE, NewRunnableMethod(this, - &Core::NotifyFrontend, PRINTER_LIST_AVAILABLE)); + &Core::NotifyPrinterListAvailable, printer_list_)); } else { // No more work to be done here. MessageLoop::current()->PostTask( diff --git a/chrome/service/cloud_print/cloud_print_proxy_backend.h b/chrome/service/cloud_print/cloud_print_proxy_backend.h index d622ccc..f78e34c 100644 --- a/chrome/service/cloud_print/cloud_print_proxy_backend.h +++ b/chrome/service/cloud_print/cloud_print_proxy_backend.h @@ -27,6 +27,11 @@ class CloudPrintProxyFrontend { // There is a list of printers available that can be registered. virtual void OnPrinterListAvailable( const cloud_print::PrinterList& printer_list) = 0; + // We successfully authenticated with the cloud print server. This callback + // allows the frontend to persist the tokens. + virtual void OnAuthenticated(const std::string& cloud_print_token, + const std::string& cloud_print_xmpp_token, + const std::string& email) = 0; protected: // Don't delete through SyncFrontend interface. @@ -41,7 +46,11 @@ class CloudPrintProxyBackend { explicit CloudPrintProxyBackend(CloudPrintProxyFrontend* frontend); ~CloudPrintProxyBackend(); - bool Initialize(const std::string& lsid, const std::string& proxy_id); + bool InitializeWithLsid(const std::string& lsid, const std::string& proxy_id); + bool InitializeWithToken(const std::string cloud_print_token, + const std::string cloud_print_xmpp_token, + const std::string email, + const std::string& proxy_id); void Shutdown(); void RegisterPrinters(const cloud_print::PrinterList& printer_list); void HandlePrinterNotification(const std::string& printer_id); |