diff options
author | vitalybuka@chromium.org <vitalybuka@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-09-22 02:13:06 +0000 |
---|---|---|
committer | vitalybuka@chromium.org <vitalybuka@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-09-22 02:13:06 +0000 |
commit | 5af5912965edda39870a892564ffceea207b0f03 (patch) | |
tree | b82497a378a45b08e9c3ca30aeed21265aea5b13 /chrome/service | |
parent | f8bd2e8cf58a8cae0e5d1f6e63dd41b2dde5f5b2 (diff) | |
download | chromium_src-5af5912965edda39870a892564ffceea207b0f03.zip chromium_src-5af5912965edda39870a892564ffceea207b0f03.tar.gz chromium_src-5af5912965edda39870a892564ffceea207b0f03.tar.bz2 |
Added ConnectorSettings class to reduce number of arguments and members in cloud print service.
BUG=137129
Review URL: https://chromiumcodereview.appspot.com/10968031
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@158149 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/service')
-rw-r--r-- | chrome/service/cloud_print/cloud_print_connector.cc | 53 | ||||
-rw-r--r-- | chrome/service/cloud_print/cloud_print_connector.h | 19 | ||||
-rw-r--r-- | chrome/service/cloud_print/cloud_print_consts.cc | 1 | ||||
-rw-r--r-- | chrome/service/cloud_print/cloud_print_consts.h | 1 | ||||
-rw-r--r-- | chrome/service/cloud_print/cloud_print_proxy.cc | 56 | ||||
-rw-r--r-- | chrome/service/cloud_print/cloud_print_proxy.h | 7 | ||||
-rw-r--r-- | chrome/service/cloud_print/cloud_print_proxy_backend.cc | 97 | ||||
-rw-r--r-- | chrome/service/cloud_print/cloud_print_proxy_backend.h | 24 | ||||
-rw-r--r-- | chrome/service/cloud_print/connector_settings.cc | 65 | ||||
-rw-r--r-- | chrome/service/cloud_print/connector_settings.h | 63 | ||||
-rw-r--r-- | chrome/service/service_process_prefs.cc | 7 | ||||
-rw-r--r-- | chrome/service/service_process_prefs.h | 6 |
12 files changed, 210 insertions, 189 deletions
diff --git a/chrome/service/cloud_print/cloud_print_connector.cc b/chrome/service/cloud_print/cloud_print_connector.cc index 9d8fc351..b20d8cb 100644 --- a/chrome/service/cloud_print/cloud_print_connector.cc +++ b/chrome/service/cloud_print/cloud_print_connector.cc @@ -19,39 +19,22 @@ #include "grit/generated_resources.h" #include "ui/base/l10n/l10n_util.h" -const char kDeleteOnEnumFail[] = "delete_on_enum_fail"; - -CloudPrintConnector::CloudPrintConnector( - Client* client, - const std::string& proxy_id, - const GURL& cloud_print_server_url, - const DictionaryValue* print_system_settings) +CloudPrintConnector::CloudPrintConnector(Client* client, + const ConnectorSettings& settings) : client_(client), - proxy_id_(proxy_id), - cloud_print_server_url_(cloud_print_server_url), - next_response_handler_(NULL), - delete_on_enum_fail_(false) { - if (print_system_settings) { - // It is possible to have no print settings specified. - print_system_settings_.reset(print_system_settings->DeepCopy()); - } + next_response_handler_(NULL) { + settings_.CopyFrom(settings); } bool CloudPrintConnector::InitPrintSystem() { if (print_system_.get()) return true; print_system_ = cloud_print::PrintSystem::CreateInstance( - print_system_settings_.get()); + settings_.print_system_settings()); if (!print_system_.get()) { NOTREACHED(); return false; // No memory. } - if (print_system_settings_.get()) { - bool delete_on_enum_fail = false; - print_system_settings_->GetBoolean(kDeleteOnEnumFail, - &delete_on_enum_fail); - delete_on_enum_fail_ = delete_on_enum_fail; - } cloud_print::PrintSystem::PrintSystemResult result = print_system_->Init(); if (!result.succeeded()) { print_system_ = NULL; @@ -64,7 +47,7 @@ bool CloudPrintConnector::InitPrintSystem() { bool CloudPrintConnector::Start() { VLOG(1) << "CP_CONNECTOR: Starting connector" - << ", proxy id: " << proxy_id_; + << ", proxy id: " << settings_.proxy_id(); pending_tasks_.clear(); @@ -82,7 +65,7 @@ bool CloudPrintConnector::Start() { void CloudPrintConnector::Stop() { VLOG(1) << "CP_CONNECTOR: Stopping connector" - << ", proxy id: " << proxy_id_; + << ", proxy id: " << settings_.proxy_id(); DCHECK(IsRunning()); // Do uninitialization here. pending_tasks_.clear(); @@ -231,13 +214,13 @@ CloudPrintConnector::HandlePrinterListResponse( // Cloud printer is not found on the local system. std::string printer_id; printer_data->GetString(kIdValue, &printer_id); - if (full_list || delete_on_enum_fail_) { + if (full_list || settings_.delete_on_enum_fail()) { // Delete if we get the full list of printers or // |delete_on_enum_fail_| is set. VLOG(1) << "CP_CONNECTOR: Deleting " << printer_name << " id: " << printer_id << " full_list: " << full_list << - " delete_on_enum_fail: " << delete_on_enum_fail_; + " delete_on_enum_fail: " << settings_.delete_on_enum_fail(); AddPendingDeleteTask(printer_id); } else { LOG(ERROR) << "CP_CONNECTOR: Printer: " << printer_name << @@ -325,7 +308,7 @@ void CloudPrintConnector::ReportUserMessage(const std::string& message_id, // Result of this request will be ignored. std::string mime_boundary; cloud_print::CreateMimeBoundaryForUpload(&mime_boundary); - GURL url = CloudPrintHelpers::GetUrlForUserMessage(cloud_print_server_url_, + GURL url = CloudPrintHelpers::GetUrlForUserMessage(settings_.server_url(), message_id); std::string post_data; cloud_print::AddMultipartValueForUpload(kMessageTextValue, failure_msg, @@ -394,7 +377,7 @@ void CloudPrintConnector::InitJobHandlerForPrinter( scoped_refptr<PrinterJobHandler> job_handler; job_handler = new PrinterJobHandler(printer_info, printer_info_cloud, - cloud_print_server_url_, + settings_.server_url(), print_system_.get(), this); job_handler_map_[printer_info_cloud.printer_id] = job_handler; @@ -467,9 +450,8 @@ void CloudPrintConnector::ContinuePendingTaskProcessing() { } void CloudPrintConnector::OnPrintersAvailable() { - GURL printer_list_url = - CloudPrintHelpers::GetUrlForPrinterList(cloud_print_server_url_, - proxy_id_); + GURL printer_list_url = CloudPrintHelpers::GetUrlForPrinterList( + settings_.server_url(), settings_.proxy_id()); StartGetRequest(printer_list_url, kCloudPrintRegisterMaxRetryCount, &CloudPrintConnector::HandlePrinterListResponse); @@ -505,9 +487,8 @@ void CloudPrintConnector::OnPrinterDelete(const std::string& printer_id) { // TODO(gene): We probably should not try indefinitely here. Just once or // twice should be enough. // Bug: http://code.google.com/p/chromium/issues/detail?id=101850 - GURL url = CloudPrintHelpers::GetUrlForPrinterDelete(cloud_print_server_url_, - printer_id, - "printer_deleted"); + GURL url = CloudPrintHelpers::GetUrlForPrinterDelete( + settings_.server_url(), printer_id, "printer_deleted"); StartGetRequest(url, kCloudPrintAPIMaxRetryCount, &CloudPrintConnector::HandlePrinterDeleteResponse); @@ -544,7 +525,7 @@ void CloudPrintConnector::OnReceivePrinterCaps( cloud_print::CreateMimeBoundaryForUpload(&mime_boundary); std::string post_data; - cloud_print::AddMultipartValueForUpload(kProxyIdValue, proxy_id_, + cloud_print::AddMultipartValueForUpload(kProxyIdValue, settings_.proxy_id(), mime_boundary, std::string(), &post_data); cloud_print::AddMultipartValueForUpload(kPrinterNameValue, info.printer_name, mime_boundary, std::string(), &post_data); @@ -576,7 +557,7 @@ void CloudPrintConnector::OnReceivePrinterCaps( mime_type += mime_boundary; GURL post_url = CloudPrintHelpers::GetUrlForPrinterRegistration( - cloud_print_server_url_); + settings_.server_url()); StartPostRequest(post_url, kCloudPrintAPIMaxRetryCount, mime_type, diff --git a/chrome/service/cloud_print/cloud_print_connector.h b/chrome/service/cloud_print/cloud_print_connector.h index 3ff55b0..5489c00 100644 --- a/chrome/service/cloud_print/cloud_print_connector.h +++ b/chrome/service/cloud_print/cloud_print_connector.h @@ -11,6 +11,7 @@ #include "base/threading/thread.h" #include "base/values.h" +#include "chrome/service/cloud_print/connector_settings.h" #include "chrome/service/cloud_print/print_system.h" #include "chrome/service/cloud_print/printer_job_handler.h" @@ -18,7 +19,7 @@ // - Matching local and cloud printers // - Registration of local printers // - Deleting cloud printers -// All tasks are posted to the commond queue (PendingTasks) and executed +// All tasks are posted to the common queue (PendingTasks) and executed // one-by-one in FIFO order. // CloudPrintConnector will notify client over Client interface. class CloudPrintConnector @@ -34,10 +35,7 @@ class CloudPrintConnector virtual ~Client() {} }; - CloudPrintConnector(Client* client, - const std::string& proxy_id, - const GURL& cloud_print_server_url, - const DictionaryValue* print_system_settings); + CloudPrintConnector(Client* client, const ConnectorSettings& settings); bool Start(); void Stop(); @@ -162,26 +160,19 @@ class CloudPrintConnector // CloudPrintConnector client. Client* client_; - // Print system settings. - scoped_ptr<DictionaryValue> print_system_settings_; + // Connector settings. + ConnectorSettings settings_; // Pointer to current print system. scoped_refptr<cloud_print::PrintSystem> print_system_; // Watcher for print system updates. scoped_refptr<cloud_print::PrintSystem::PrintServerWatcher> print_server_watcher_; - // Id of the Cloud Print proxy. - std::string proxy_id_; - // Cloud Print server url. - GURL cloud_print_server_url_; // A map of printer id to job handler. typedef std::map<std::string, scoped_refptr<PrinterJobHandler> > JobHandlerMap; JobHandlerMap job_handler_map_; // Next response handler. ResponseHandler next_response_handler_; - // If |true| printers that are not found locally will be deleted on GCP - // even if the local enumeration failed. - bool delete_on_enum_fail_; // The list of pending tasks to be done in the background. std::list<PendingTask> pending_tasks_; // The CloudPrintURLFetcher instance for the current request. diff --git a/chrome/service/cloud_print/cloud_print_consts.cc b/chrome/service/cloud_print/cloud_print_consts.cc index a0f90bf..ae4c167 100644 --- a/chrome/service/cloud_print/cloud_print_consts.cc +++ b/chrome/service/cloud_print/cloud_print_consts.cc @@ -32,7 +32,6 @@ const char kProxyTagPrefix[] = "__cp__"; const char kTagsHashTagName[] = "__cp__tagshash"; const char kTagDryRunFlag[] = "__cp__dry_run"; -const char kDefaultCloudPrintServerUrl[] = "https://www.google.com/cloudprint"; const char kCloudPrintGaiaServiceId[] = "cloudprint"; const char kProxyAuthUserAgent[] = "ChromiumBrowser"; const char kCloudPrintPushNotificationsSource[] = "cloudprint.google.com"; diff --git a/chrome/service/cloud_print/cloud_print_consts.h b/chrome/service/cloud_print/cloud_print_consts.h index c709f88..401e9bb 100644 --- a/chrome/service/cloud_print/cloud_print_consts.h +++ b/chrome/service/cloud_print/cloud_print_consts.h @@ -33,7 +33,6 @@ extern const char kOAuthCodeValue[]; extern const char kProxyTagPrefix[]; extern const char kTagsHashTagName[]; extern const char kTagDryRunFlag[]; -extern const char kDefaultCloudPrintServerUrl[]; extern const char kCloudPrintGaiaServiceId[]; extern const char kProxyAuthUserAgent[]; extern const char kCloudPrintPushNotificationsSource[]; diff --git a/chrome/service/cloud_print/cloud_print_proxy.cc b/chrome/service/cloud_print/cloud_print_proxy.cc index 89b162b..416eddb1 100644 --- a/chrome/service/cloud_print/cloud_print_proxy.cc +++ b/chrome/service/cloud_print/cloud_print_proxy.cc @@ -101,25 +101,20 @@ void CloudPrintProxy::EnableForUser(const std::string& lsid) { // 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, + backend_->InitializeWithLsid(lsid, 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_); + backend_->InitializeWithRobotToken(robot_refresh_token, robot_email); } 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_); + backend_->InitializeWithToken(cloud_print_token); } } if (client_) { @@ -136,9 +131,7 @@ void CloudPrintProxy::EnableForUserWithRobot( return; DCHECK(backend_.get()); user_email_ = user_email; - backend_->InitializeWithRobotAuthCode(robot_auth_code, - robot_email, - proxy_id_); + backend_->InitializeWithRobotAuthCode(robot_auth_code, robot_email); if (client_) { client_->OnCloudPrintProxyEnabled(true); } @@ -149,46 +142,21 @@ bool CloudPrintProxy::CreateBackend() { 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(); - } - - // Getting print system specific settings from the preferences. - const DictionaryValue* print_system_settings = NULL; - service_prefs_->GetDictionary(prefs::kCloudPrintPrintSystemSettings, - &print_system_settings); - - // Check if there is an override for the cloud print server URL. - std::string cloud_print_server_url_str; - service_prefs_->GetString(prefs::kCloudPrintServiceURL, - &cloud_print_server_url_str); - if (cloud_print_server_url_str.empty()) { - cloud_print_server_url_str = kDefaultCloudPrintServerUrl; - } + settings_.InitFrom(service_prefs_); // By default we don't poll for jobs when we lose XMPP connection. But this // behavior can be overridden by a preference. bool enable_job_poll = false; - service_prefs_->GetBoolean(prefs::kCloudPrintEnableJobPoll, - &enable_job_poll); + service_prefs_->GetBoolean(prefs::kCloudPrintEnableJobPoll, &enable_job_poll); - // TODO(sanjeevr): Allow overriding OAuthClientInfo in prefs. gaia::OAuthClientInfo oauth_client_info; oauth_client_info.client_id = - google_apis::GetOAuth2ClientID(google_apis::CLIENT_CLOUD_PRINT); + google_apis::GetOAuth2ClientID(google_apis::CLIENT_CLOUD_PRINT); oauth_client_info.client_secret = - google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_CLOUD_PRINT); - - cloud_print_server_url_ = GURL(cloud_print_server_url_str.c_str()); - DCHECK(cloud_print_server_url_.is_valid()); - backend_.reset(new CloudPrintProxyBackend(this, proxy_id_, - cloud_print_server_url_, - print_system_settings, - oauth_client_info, + google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_CLOUD_PRINT); + backend_.reset(new CloudPrintProxyBackend(this, settings_, oauth_client_info, enable_job_poll)); + return true; } @@ -219,7 +187,7 @@ void CloudPrintProxy::GetProxyInfo(cloud_print::CloudPrintProxyInfo* info) { info->email.clear(); if (enabled_) info->email = user_email(); - info->proxy_id = proxy_id_; + info->proxy_id = settings_.proxy_id(); // If the Cloud Print service is not enabled, we may need to read the old // value of proxy_id from prefs. if (info->proxy_id.empty()) @@ -281,7 +249,7 @@ void CloudPrintProxy::OnUnregisterPrinters( const std::string& auth_token, const std::list<std::string>& printer_ids) { ShutdownBackend(); - wipeout_.reset(new CloudPrintWipeout(this, cloud_print_server_url_)); + wipeout_.reset(new CloudPrintWipeout(this, settings_.server_url())); wipeout_->UnregisterPrinters(auth_token, printer_ids); } diff --git a/chrome/service/cloud_print/cloud_print_proxy.h b/chrome/service/cloud_print/cloud_print_proxy.h index 644514a..e844a9d 100644 --- a/chrome/service/cloud_print/cloud_print_proxy.h +++ b/chrome/service/cloud_print/cloud_print_proxy.h @@ -89,11 +89,8 @@ 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_; - // Cloud Print server url. - GURL cloud_print_server_url_; + // Connector settings. + ConnectorSettings settings_; // This is a cleanup class for unregistering printers on proxy disable. scoped_ptr<CloudPrintWipeout> wipeout_; diff --git a/chrome/service/cloud_print/cloud_print_proxy_backend.cc b/chrome/service/cloud_print/cloud_print_proxy_backend.cc index c0c9e98..16d7211 100644 --- a/chrome/service/cloud_print/cloud_print_proxy_backend.cc +++ b/chrome/service/cloud_print/cloud_print_proxy_backend.cc @@ -17,6 +17,7 @@ #include "chrome/service/cloud_print/cloud_print_consts.h" #include "chrome/service/cloud_print/cloud_print_helpers.h" #include "chrome/service/cloud_print/cloud_print_token_store.h" +#include "chrome/service/cloud_print/connector_settings.h" #include "chrome/service/gaia/service_gaia_authenticator.h" #include "chrome/service/net/service_url_request_context.h" #include "chrome/service/service_process.h" @@ -39,9 +40,7 @@ class CloudPrintProxyBackend::Core // It is OK for print_server_url to be empty. In this case system should // use system default (local) print server. Core(CloudPrintProxyBackend* backend, - const std::string& proxy_id, - const GURL& cloud_print_server_url, - const DictionaryValue* print_system_settings, + const ConnectorSettings& settings, const gaia::OAuthClientInfo& oauth_client_info, bool enable_job_poll); @@ -55,19 +54,15 @@ class CloudPrintProxyBackend::Core // 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, const std::string& last_robot_refresh_token, const std::string& last_robot_email, const std::string& last_user_email); - void DoInitializeWithToken(const std::string& cloud_print_token, - const std::string& proxy_id); + void DoInitializeWithToken(const std::string& cloud_print_token); void DoInitializeWithRobotToken(const std::string& robot_oauth_refresh_token, - const std::string& robot_email, - const std::string& proxy_id); + const std::string& robot_email); void DoInitializeWithRobotAuthCode(const std::string& robot_oauth_auth_code, - const std::string& robot_email, - const std::string& proxy_id); + const std::string& robot_email); // Called on the CloudPrintProxyBackend core_thread_ to perform // shutdown. @@ -135,12 +130,6 @@ class CloudPrintProxyBackend::Core // Cloud Print connector. scoped_refptr<CloudPrintConnector> connector_; - // Server URL. - GURL cloud_print_server_url_; - // Proxy Id. - std::string proxy_id_; - // Print system settings. - scoped_ptr<DictionaryValue> print_system_settings_; // OAuth client info. gaia::OAuthClientInfo oauth_client_info_; // Notification (xmpp) handler. @@ -154,6 +143,8 @@ class CloudPrintProxyBackend::Core bool job_poll_scheduled_; // Indicates whether we should poll for jobs when we lose XMPP connection. bool enable_job_poll_; + // Connector settings. + ConnectorSettings settings_; scoped_ptr<CloudPrintTokenStore> token_store_; DISALLOW_COPY_AND_ASSIGN(Core); @@ -161,21 +152,14 @@ class CloudPrintProxyBackend::Core CloudPrintProxyBackend::CloudPrintProxyBackend( CloudPrintProxyFrontend* frontend, - const std::string& proxy_id, - const GURL& cloud_print_server_url, - const DictionaryValue* print_system_settings, + const ConnectorSettings& settings, const gaia::OAuthClientInfo& oauth_client_info, bool enable_job_poll) : core_thread_("Chrome_CloudPrintProxyCoreThread"), frontend_loop_(MessageLoop::current()), frontend_(frontend) { DCHECK(frontend_); - core_ = new Core(this, - proxy_id, - cloud_print_server_url, - print_system_settings, - oauth_client_info, - enable_job_poll); + core_ = new Core(this, settings, oauth_client_info, enable_job_poll); } CloudPrintProxyBackend::~CloudPrintProxyBackend() { @@ -184,7 +168,6 @@ CloudPrintProxyBackend::~CloudPrintProxyBackend() { bool CloudPrintProxyBackend::InitializeWithLsid( const std::string& lsid, - const std::string& proxy_id, const std::string& last_robot_refresh_token, const std::string& last_robot_email, const std::string& last_user_email) { @@ -193,47 +176,43 @@ bool CloudPrintProxyBackend::InitializeWithLsid( core_thread_.message_loop()->PostTask( FROM_HERE, base::Bind(&CloudPrintProxyBackend::Core::DoInitializeWithLsid, - core_.get(), lsid, proxy_id, last_robot_refresh_token, - last_robot_email, last_user_email)); + core_.get(), lsid, last_robot_refresh_token, last_robot_email, + last_user_email)); return true; } bool CloudPrintProxyBackend::InitializeWithToken( - const std::string& cloud_print_token, - const std::string& proxy_id) { + const std::string& cloud_print_token) { if (!core_thread_.Start()) return false; core_thread_.message_loop()->PostTask( FROM_HERE, base::Bind(&CloudPrintProxyBackend::Core::DoInitializeWithToken, - core_.get(), cloud_print_token, proxy_id)); + core_.get(), cloud_print_token)); return true; } bool CloudPrintProxyBackend::InitializeWithRobotToken( const std::string& robot_oauth_refresh_token, - const std::string& robot_email, - const std::string& proxy_id) { + const std::string& robot_email) { if (!core_thread_.Start()) return false; core_thread_.message_loop()->PostTask( FROM_HERE, base::Bind(&CloudPrintProxyBackend::Core::DoInitializeWithRobotToken, - core_.get(), robot_oauth_refresh_token, robot_email, - proxy_id)); + core_.get(), robot_oauth_refresh_token, robot_email)); return true; } bool CloudPrintProxyBackend::InitializeWithRobotAuthCode( const std::string& robot_oauth_auth_code, - const std::string& robot_email, - const std::string& proxy_id) { + const std::string& robot_email) { if (!core_thread_.Start()) return false; core_thread_.message_loop()->PostTask( FROM_HERE, base::Bind(&CloudPrintProxyBackend::Core::DoInitializeWithRobotAuthCode, - core_.get(), robot_oauth_auth_code, robot_email, proxy_id)); + core_.get(), robot_oauth_auth_code, robot_email)); return true; } @@ -254,37 +233,25 @@ void CloudPrintProxyBackend::UnregisterPrinters() { CloudPrintProxyBackend::Core::Core( CloudPrintProxyBackend* backend, - const std::string& proxy_id, - const GURL& cloud_print_server_url, - const DictionaryValue* print_system_settings, + const ConnectorSettings& settings, const gaia::OAuthClientInfo& oauth_client_info, bool enable_job_poll) : backend_(backend), - cloud_print_server_url_(cloud_print_server_url), - proxy_id_(proxy_id), oauth_client_info_(oauth_client_info), notifications_enabled_(false), job_poll_scheduled_(false), enable_job_poll_(enable_job_poll) { - if (print_system_settings) { - // It is possible to have no print settings specified. - print_system_settings_.reset(print_system_settings->DeepCopy()); - } + settings_.CopyFrom(settings); } void CloudPrintProxyBackend::Core::CreateAuthAndConnector() { if (!auth_.get()) { - auth_ = new CloudPrintAuth(this, - cloud_print_server_url_, - oauth_client_info_, - proxy_id_); + auth_ = new CloudPrintAuth(this, settings_.server_url(), oauth_client_info_, + settings_.proxy_id()); } if (!connector_.get()) { - connector_ = new CloudPrintConnector(this, - proxy_id_, - cloud_print_server_url_, - print_system_settings_.get()); + connector_ = new CloudPrintConnector(this, settings_); } } @@ -295,7 +262,6 @@ void CloudPrintProxyBackend::Core::DestroyAuthAndConnector() { void CloudPrintProxyBackend::Core::DoInitializeWithLsid( const std::string& lsid, - const std::string& proxy_id, const std::string& last_robot_refresh_token, const std::string& last_robot_email, const std::string& last_user_email) { @@ -309,8 +275,7 @@ void CloudPrintProxyBackend::Core::DoInitializeWithLsid( } void CloudPrintProxyBackend::Core::DoInitializeWithToken( - const std::string& cloud_print_token, - const std::string& proxy_id) { + const std::string& cloud_print_token) { DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); CreateAuthAndConnector(); auth_->AuthenticateWithToken(cloud_print_token); @@ -318,8 +283,7 @@ void CloudPrintProxyBackend::Core::DoInitializeWithToken( void CloudPrintProxyBackend::Core::DoInitializeWithRobotToken( const std::string& robot_oauth_refresh_token, - const std::string& robot_email, - const std::string& proxy_id) { + const std::string& robot_email) { DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); CreateAuthAndConnector(); auth_->AuthenticateWithRobotToken(robot_oauth_refresh_token, robot_email); @@ -327,8 +291,7 @@ void CloudPrintProxyBackend::Core::DoInitializeWithRobotToken( void CloudPrintProxyBackend::Core::DoInitializeWithRobotAuthCode( const std::string& robot_oauth_auth_code, - const std::string& robot_email, - const std::string& proxy_id) { + const std::string& robot_email) { DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); CreateAuthAndConnector(); auth_->AuthenticateWithRobotAuthCode(robot_oauth_auth_code, robot_email); @@ -339,7 +302,7 @@ void CloudPrintProxyBackend::Core::OnAuthenticationComplete( const std::string& robot_oauth_refresh_token, const std::string& robot_email, const std::string& user_email) { - CloudPrintTokenStore* token_store = GetTokenStore(); + CloudPrintTokenStore* token_store = GetTokenStore(); bool first_time = token_store->token().empty(); token_store->SetToken(access_token); // Let the frontend know that we have authenticated. @@ -404,7 +367,7 @@ void CloudPrintProxyBackend::Core::InitNotifications( void CloudPrintProxyBackend::Core::DoShutdown() { DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); - VLOG(1) << "CP_CONNECTOR: Shutdown connector, id: " << proxy_id_; + VLOG(1) << "CP_CONNECTOR: Shutdown connector, id: " << settings_.proxy_id(); if (connector_->IsRunning()) connector_->Stop(); @@ -505,7 +468,7 @@ void CloudPrintProxyBackend::Core::OnNotificationsEnabled() { DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); notifications_enabled_ = true; notifications_enabled_since_ = base::TimeTicks::Now(); - VLOG(1) << "Notifications for connector " << proxy_id_ + VLOG(1) << "Notifications for connector " << settings_.proxy_id() << " were enabled at " << notifications_enabled_since_.ToInternalValue(); // Notifications just got re-enabled. In this case we want to schedule @@ -519,7 +482,8 @@ void CloudPrintProxyBackend::Core::OnNotificationsDisabled( notifier::NotificationsDisabledReason reason) { DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); notifications_enabled_ = false; - LOG(ERROR) << "Notifications for connector " << proxy_id_ << " disabled."; + LOG(ERROR) << "Notifications for connector " << settings_.proxy_id() + << " disabled."; notifications_enabled_since_ = base::TimeTicks(); // We just lost notifications. This this case we want to schedule a // job poll if enable_job_poll_ is true. @@ -536,3 +500,4 @@ void CloudPrintProxyBackend::Core::OnIncomingNotification( notification.channel.c_str())) HandlePrinterNotification(notification.data); } + diff --git a/chrome/service/cloud_print/cloud_print_proxy_backend.h b/chrome/service/cloud_print/cloud_print_proxy_backend.h index ee47e81..a78ce3b 100644 --- a/chrome/service/cloud_print/cloud_print_proxy_backend.h +++ b/chrome/service/cloud_print/cloud_print_proxy_backend.h @@ -9,10 +9,9 @@ #include <string> #include "base/threading/thread.h" +#include "chrome/service/cloud_print/connector_settings.h" #include "printing/backend/print_backend.h" -class GURL; - namespace base { class DictionaryValue; } @@ -56,13 +55,10 @@ class CloudPrintProxyBackend { public: // It is OK for print_system_settings to be NULL. In this case system should // use system default settings. - CloudPrintProxyBackend( - CloudPrintProxyFrontend* frontend, - const std::string& proxy_id, - const GURL& cloud_print_server_url, - const base::DictionaryValue* print_sys_settings, - const gaia::OAuthClientInfo& oauth_client_info, - bool enable_job_poll); + CloudPrintProxyBackend(CloudPrintProxyFrontend* frontend, + const ConnectorSettings& settings, + const gaia::OAuthClientInfo& oauth_client_info, + bool enable_job_poll); ~CloudPrintProxyBackend(); // Called when the user enables Google Cloud Print. @@ -70,22 +66,18 @@ class CloudPrintProxyBackend { // the previously persisted credentials if any. We will use this is the passed // in LSID belongs to the same user as |last_user_email|. bool InitializeWithLsid(const std::string& lsid, - const std::string& proxy_id, const std::string& last_robot_refresh_token, const std::string& last_robot_email, const std::string& last_user_email); // Legacy mechanism when we have saved user credentials but no saved robot // credentials. - bool InitializeWithToken(const std::string& cloud_print_token, - const std::string& proxy_id); + bool InitializeWithToken(const std::string& cloud_print_token); // Called when we have saved robot credentials. bool InitializeWithRobotToken(const std::string& robot_oauth_refresh_token, - const std::string& robot_email, - const std::string& proxy_id); + const std::string& robot_email); // 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); + const std::string& robot_email); void Shutdown(); void RegisterPrinters(const printing::PrinterList& printer_list); void UnregisterPrinters(); diff --git a/chrome/service/cloud_print/connector_settings.cc b/chrome/service/cloud_print/connector_settings.cc new file mode 100644 index 0000000..667dd5b --- /dev/null +++ b/chrome/service/cloud_print/connector_settings.cc @@ -0,0 +1,65 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/service/cloud_print/connector_settings.h" + +#include "base/values.h" +#include "chrome/common/pref_names.h" +#include "chrome/service/cloud_print/cloud_print_consts.h" +#include "chrome/service/cloud_print/print_system.h" +#include "chrome/service/service_process_prefs.h" + +namespace { + +const char kDefaultCloudPrintServerUrl[] = "https://www.google.com/cloudprint"; +const char kDeleteOnEnumFail[] = "delete_on_enum_fail"; + +} // namespace + +ConnectorSettings::ConnectorSettings() : delete_on_enum_fail_(false) { +} + +ConnectorSettings::~ConnectorSettings() { +} + +void ConnectorSettings::InitFrom(ServiceProcessPrefs* prefs) { + CopyFrom(ConnectorSettings()); + + prefs->GetString(prefs::kCloudPrintProxyId, &proxy_id_); + if (proxy_id_.empty()) { + proxy_id_ = cloud_print::PrintSystem::GenerateProxyId(); + prefs->SetString(prefs::kCloudPrintProxyId, proxy_id_); + prefs->WritePrefs(); + } + + // Getting print system specific settings from the preferences. + const base::DictionaryValue* print_system_settings = NULL; + prefs->GetDictionary(prefs::kCloudPrintPrintSystemSettings, + &print_system_settings); + if (print_system_settings) { + print_system_settings_.reset(print_system_settings->DeepCopy()); + // TODO(vitalybuka) : Consider to move option from print_system_settings. + print_system_settings_->GetBoolean(kDeleteOnEnumFail, + &delete_on_enum_fail_); + } + + // Check if there is an override for the cloud print server URL. + std::string cloud_print_server_url_str; + prefs->GetString(prefs::kCloudPrintServiceURL, + &cloud_print_server_url_str); + if (cloud_print_server_url_str.empty()) { + cloud_print_server_url_str = kDefaultCloudPrintServerUrl; + } + server_url_ = GURL(cloud_print_server_url_str.c_str()); + DCHECK(server_url_.is_valid()); +} + +void ConnectorSettings::CopyFrom(const ConnectorSettings& source) { + server_url_ = source.server_url(); + proxy_id_ = source.proxy_id(); + delete_on_enum_fail_ = source.delete_on_enum_fail(); + if (source.print_system_settings()) + print_system_settings_.reset(source.print_system_settings()->DeepCopy()); +} + diff --git a/chrome/service/cloud_print/connector_settings.h b/chrome/service/cloud_print/connector_settings.h new file mode 100644 index 0000000..50eaa82 --- /dev/null +++ b/chrome/service/cloud_print/connector_settings.h @@ -0,0 +1,63 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_SERVICE_CLOUD_PRINT_CONNECTOR_SETTINGS_H_ +#define CHROME_SERVICE_CLOUD_PRINT_CONNECTOR_SETTINGS_H_ + +#include <string> + +#include "base/memory/scoped_ptr.h" +#include "googleurl/src/gurl.h" + +class ServiceProcessPrefs; + +namespace base { + class DictionaryValue; +} + +class ConnectorSettings { + public: + ConnectorSettings(); + ~ConnectorSettings(); + + void InitFrom(ServiceProcessPrefs* prefs); + + void CopyFrom(const ConnectorSettings& source); + + const GURL& server_url() const { + return server_url_; + }; + + const std::string& proxy_id() const { + return proxy_id_; + } + + bool delete_on_enum_fail() const { + return delete_on_enum_fail_; + } + + const base::DictionaryValue* print_system_settings() const { + return print_system_settings_.get(); + }; + + private: + // Cloud Print server url. + GURL server_url_; + + // This is initialized after a successful call to one of the Enable* methods. + // It is not cleared in DisableUser. + std::string proxy_id_; + + // If |true| printers that are not found locally will be deleted on GCP + // even if the local enumeration failed. + bool delete_on_enum_fail_; + + // Print system settings. + scoped_ptr<base::DictionaryValue> print_system_settings_; + + DISALLOW_COPY_AND_ASSIGN(ConnectorSettings); +}; + +#endif // CHROME_SERVICE_CLOUD_PRINT_CONNECTOR_SETTINGS_H_ + diff --git a/chrome/service/service_process_prefs.cc b/chrome/service/service_process_prefs.cc index 511709a..b925e2b 100644 --- a/chrome/service/service_process_prefs.cc +++ b/chrome/service/service_process_prefs.cc @@ -23,7 +23,7 @@ void ServiceProcessPrefs::WritePrefs() { } void ServiceProcessPrefs::GetString(const std::string& key, - std::string* result) { + std::string* result) const { const Value* value; if (prefs_->GetValue(key, &value) == PersistentPrefStore::READ_OK) value->GetAsString(result); @@ -34,7 +34,8 @@ void ServiceProcessPrefs::SetString(const std::string& key, prefs_->SetValue(key, Value::CreateStringValue(value)); } -void ServiceProcessPrefs::GetBoolean(const std::string& key, bool* result) { +void ServiceProcessPrefs::GetBoolean(const std::string& key, + bool* result) const { const Value* value; if (prefs_->GetValue(key, &value) == PersistentPrefStore::READ_OK) value->GetAsBoolean(result); @@ -45,7 +46,7 @@ void ServiceProcessPrefs::SetBoolean(const std::string& key, bool value) { } void ServiceProcessPrefs::GetDictionary(const std::string& key, - const DictionaryValue** result) { + const DictionaryValue** result) const { const Value* value; if (prefs_->GetValue(key, &value) != PersistentPrefStore::READ_OK || !value->IsType(Value::TYPE_DICTIONARY)) { diff --git a/chrome/service/service_process_prefs.h b/chrome/service/service_process_prefs.h index 1b33eb4d..74a674d 100644 --- a/chrome/service/service_process_prefs.h +++ b/chrome/service/service_process_prefs.h @@ -30,20 +30,20 @@ class ServiceProcessPrefs { void WritePrefs(); // Get a string preference for |key| and store it in |result|. - void GetString(const std::string& key, std::string* result); + void GetString(const std::string& key, std::string* result) const; // Set a string |value| for |key|. void SetString(const std::string& key, const std::string& value); // Get a boolean preference for |key| and store it in |result|. - void GetBoolean(const std::string& key, bool* result); + void GetBoolean(const std::string& key, bool* result) const; // Set a boolean |value| for |key|. void SetBoolean(const std::string& key, bool value); // Get a dictionary preference for |key| and store it in |result|. void GetDictionary(const std::string& key, - const base::DictionaryValue** result); + const base::DictionaryValue** result) const; // Removes the pref specified by |key|. void RemovePref(const std::string& key); |