diff options
author | droger@chromium.org <droger@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-01-29 18:22:18 +0000 |
---|---|---|
committer | droger@chromium.org <droger@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-01-29 18:22:18 +0000 |
commit | bb1c86cd1d143efafdab77f9470c763401c3bf40 (patch) | |
tree | 6f3cccdcd04267861f527f02903e107e33008101 /components | |
parent | 4c318d058d0451a3166849b4e580ecb09d489341 (diff) | |
download | chromium_src-bb1c86cd1d143efafdab77f9470c763401c3bf40.zip chromium_src-bb1c86cd1d143efafdab77f9470c763401c3bf40.tar.gz chromium_src-bb1c86cd1d143efafdab77f9470c763401c3bf40.tar.bz2 |
Introduce TranslateService and TranslateDownloadManager
This CL removes the TranslateDelegate interface and introduces
TranslateService and TranslateDownloadManager.
This will enable (in a future CL) to move TranslateScript and
TranslateLanguageList in the Translate component, and
TranslateDownloadManager will own them.
BUG=332736
Review URL: https://codereview.chromium.org/145023015
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@247724 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'components')
6 files changed, 79 insertions, 36 deletions
diff --git a/components/translate.gypi b/components/translate.gypi index 926eacc..a61b836 100644 --- a/components/translate.gypi +++ b/components/translate.gypi @@ -11,6 +11,7 @@ 'language_usage_metrics', 'translate_core_common', '../base/base.gyp:base', + '../net/net.gyp:net', '../url/url.gyp:url_lib', ], 'include_dirs': [ @@ -22,7 +23,8 @@ 'translate/core/browser/page_translated_details.h', 'translate/core/browser/translate_browser_metrics.cc', 'translate/core/browser/translate_browser_metrics.h', - 'translate/core/browser/translate_delegate.h', + 'translate/core/browser/translate_download_manager.cc', + 'translate/core/browser/translate_download_manager.h', 'translate/core/browser/translate_driver.h', 'translate/core/browser/translate_error_details.h', 'translate/core/browser/translate_event_details.cc', diff --git a/components/translate/core/browser/translate_delegate.h b/components/translate/core/browser/translate_delegate.h deleted file mode 100644 index 2e5735c..0000000 --- a/components/translate/core/browser/translate_delegate.h +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright 2014 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 COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_DELEGATE_H_ -#define COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_DELEGATE_H_ - -namespace net { -class URLRequestContextGetter; -} - -// A concrete instance of TranslateDelegate has to be provided by the embedder -// of the Translate component. -class TranslateDelegate { - public: - //Returns the URL request context in which the Translate component should make - //requests. - virtual net::URLRequestContextGetter* GetURLRequestContext() = 0; -}; - -#endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_DELEGATE_H_ diff --git a/components/translate/core/browser/translate_download_manager.cc b/components/translate/core/browser/translate_download_manager.cc new file mode 100644 index 0000000..c688d21 --- /dev/null +++ b/components/translate/core/browser/translate_download_manager.cc @@ -0,0 +1,16 @@ +// Copyright 2014 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 "components/translate/core/browser/translate_download_manager.h" + +#include "base/memory/singleton.h" + +// static +TranslateDownloadManager* TranslateDownloadManager::GetInstance() { + return Singleton<TranslateDownloadManager>::get(); +} + +TranslateDownloadManager::TranslateDownloadManager() {} + +TranslateDownloadManager::~TranslateDownloadManager() {} diff --git a/components/translate/core/browser/translate_download_manager.h b/components/translate/core/browser/translate_download_manager.h new file mode 100644 index 0000000..a36ae23 --- /dev/null +++ b/components/translate/core/browser/translate_download_manager.h @@ -0,0 +1,51 @@ +// Copyright 2014 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 COMPONENTS_TRANSLATE_CORE_BROWER_TRANSLATE_DOWNLOAD_MANAGER_H_ +#define COMPONENTS_TRANSLATE_CORE_BROWER_TRANSLATE_DOWNLOAD_MANAGER_H_ + +#include <string> + +#include "base/logging.h" +#include "base/memory/ref_counted.h" +#include "net/url_request/url_request_context_getter.h" + +template <typename T> struct DefaultSingletonTraits; + +// Manages the downloaded resources for Translate, such as the translate script +// and the language list. +// TODO(droger): TranslateDownloadManager should own TranslateLanguageList and +// TranslateScript. See http://crbug.com/335074 and http://crbug.com/335077. +class TranslateDownloadManager { + public: + // Returns the singleton instance. + static TranslateDownloadManager* GetInstance(); + + // The request context used to download the resources. + // Should be set before this class can be used. + net::URLRequestContextGetter* request_context() { return request_context_; } + void set_request_context(net::URLRequestContextGetter* context) { + request_context_ = context; + } + + // The application locale. + // Should be set before this class can be used. + const std::string& application_locale() { + DCHECK(!application_locale_.empty()); + return application_locale_; + } + void set_application_locale(const std::string& locale) { + application_locale_ = locale; + } + + private: + friend struct DefaultSingletonTraits<TranslateDownloadManager>; + TranslateDownloadManager(); + virtual ~TranslateDownloadManager(); + + std::string application_locale_; + scoped_refptr<net::URLRequestContextGetter> request_context_; +}; + +#endif // COMPONENTS_TRANSLATE_CORE_BROWER_TRANSLATE_DOWNLOAD_MANAGER_H_ diff --git a/components/translate/core/browser/translate_url_fetcher.cc b/components/translate/core/browser/translate_url_fetcher.cc index a029369..85988a9 100644 --- a/components/translate/core/browser/translate_url_fetcher.cc +++ b/components/translate/core/browser/translate_url_fetcher.cc @@ -4,7 +4,7 @@ #include "components/translate/core/browser/translate_url_fetcher.h" -#include "components/translate/core/browser/translate_delegate.h" +#include "components/translate/core/browser/translate_download_manager.h" #include "net/base/load_flags.h" #include "net/http/http_status_code.h" #include "net/url_request/url_fetcher.h" @@ -17,11 +17,9 @@ const int kMaxRetry = 16; } // namespace -TranslateURLFetcher::TranslateURLFetcher(int id, TranslateDelegate* delegate) - : id_(id), - translate_delegate_(delegate), - state_(IDLE), - retry_count_(0) { +TranslateURLFetcher::TranslateURLFetcher(int id) : id_(id), + state_(IDLE), + retry_count_(0) { } TranslateURLFetcher::~TranslateURLFetcher() { @@ -52,7 +50,8 @@ bool TranslateURLFetcher::Request( this)); fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES); - fetcher_->SetRequestContext(translate_delegate_->GetURLRequestContext()); + fetcher_->SetRequestContext( + TranslateDownloadManager::GetInstance()->request_context()); // Set retry parameter for HTTP status code 5xx. This doesn't work against // 106 (net::ERR_INTERNET_DISCONNECTED) and so on. // TranslateLanguageList handles network status, and implements retry. diff --git a/components/translate/core/browser/translate_url_fetcher.h b/components/translate/core/browser/translate_url_fetcher.h index a702e33..9643613 100644 --- a/components/translate/core/browser/translate_url_fetcher.h +++ b/components/translate/core/browser/translate_url_fetcher.h @@ -10,8 +10,8 @@ #include "net/url_request/url_fetcher_delegate.h" #include "url/gurl.h" -class TranslateDelegate; - +// Downloads raw Translate data such as the Translate script and the language +// list. class TranslateURLFetcher : public net::URLFetcherDelegate { public: // Callback type for Request(). @@ -25,8 +25,7 @@ class TranslateURLFetcher : public net::URLFetcherDelegate { FAILED, // The last fetch request was finished with a failure. }; - // |delegate| is expected to outlive the TranslateURLFetcher. - explicit TranslateURLFetcher(int id, TranslateDelegate* delegate); + explicit TranslateURLFetcher(int id); virtual ~TranslateURLFetcher(); int max_retry_on_5xx() { @@ -62,9 +61,6 @@ class TranslateURLFetcher : public net::URLFetcherDelegate { // ID which is assigned to the URLFetcher. const int id_; - // Used to get information from the embedder of Translate. - TranslateDelegate* translate_delegate_; - // Internal state. enum State state_; |