diff options
author | droger@chromium.org <droger@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-17 13:02:14 +0000 |
---|---|---|
committer | droger@chromium.org <droger@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-17 13:02:14 +0000 |
commit | 3465db274cd777aeec47e5b0103cb985b7d75ff5 (patch) | |
tree | 418efac142616231733fe6d5dd2ddaa7a1dbd25b /components | |
parent | 58adce554ffeae228e18020914a6bcc51a5ff81d (diff) | |
download | chromium_src-3465db274cd777aeec47e5b0103cb985b7d75ff5.zip chromium_src-3465db274cd777aeec47e5b0103cb985b7d75ff5.tar.gz chromium_src-3465db274cd777aeec47e5b0103cb985b7d75ff5.tar.bz2 |
Move the translate script callbacks from TranslateManager to TranslateScript
This CL refactors the way callbacks for TranslateScript are handled.
Previously, only one callback was allowed, and thus TranslateManager was
managing a global list of pending script request to be able to merge all these
requests into one callback.
As we want to move away from TranslateManager being a singleton, the management
of TranslateScript requests is moved to TranslateScript (which is also cleaner).
TranslateScript now supports multiple callbacks.
BUG=332736
Review URL: https://codereview.chromium.org/166553003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@251666 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'components')
-rw-r--r-- | components/translate/core/browser/translate_script.cc | 17 | ||||
-rw-r--r-- | components/translate/core/browser/translate_script.h | 15 |
2 files changed, 19 insertions, 13 deletions
diff --git a/components/translate/core/browser/translate_script.cc b/components/translate/core/browser/translate_script.cc index a83f499..68b5cff 100644 --- a/components/translate/core/browser/translate_script.cc +++ b/components/translate/core/browser/translate_script.cc @@ -51,14 +51,16 @@ TranslateScript::TranslateScript() TranslateScript::~TranslateScript() { } -void TranslateScript::Request(const Callback& callback) { +void TranslateScript::Request(const RequestCallback& callback) { + DCHECK(data_.empty()) << "Do not fetch the script if it is already fetched"; + callback_list_.push_back(callback); + if (fetcher_.get() != NULL) { - NOTREACHED(); + // If there is already a request in progress, do nothing. |callback| will be + // run on completion. return; } - callback_ = callback; - GURL translate_script_url; // Check if command-line contains an alternative URL for translate service. const CommandLine& command_line = *CommandLine::ForCurrentProcess(); @@ -145,5 +147,10 @@ void TranslateScript::OnScriptFetchComplete( expiration_delay_); } - callback_.Run(success, data); + for (RequestCallbackList::iterator it = callback_list_.begin(); + it != callback_list_.end(); + ++it) { + it->Run(success, data); + } + callback_list_.clear(); } diff --git a/components/translate/core/browser/translate_script.h b/components/translate/core/browser/translate_script.h index 552daee..156d3a5 100644 --- a/components/translate/core/browser/translate_script.h +++ b/components/translate/core/browser/translate_script.h @@ -6,8 +6,9 @@ #define COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_SCRIPT_H_ #include <string> +#include <vector> -#include "base/callback.h" +#include "base/callback_forward.h" #include "base/gtest_prod_util.h" #include "base/memory/scoped_ptr.h" #include "base/memory/weak_ptr.h" @@ -18,7 +19,7 @@ class TranslateURLFetcher; class TranslateScript { public: - typedef base::Callback<void(bool, const std::string&)> Callback; + typedef base::Callback<void(bool, const std::string&)> RequestCallback; static const int kFetcherId = 0; @@ -40,10 +41,7 @@ class TranslateScript { // Fetches the JS translate script (the script that is injected in the page // to translate it). - void Request(const Callback& callback); - - // Returns true if this has a pending request. - bool HasPendingRequest() const { return fetcher_.get() != NULL; } + void Request(const RequestCallback& callback); private: friend class TranslateScriptTest; @@ -83,8 +81,9 @@ class TranslateScript { // server. base::TimeDelta expiration_delay_; - // The callback called when the server sends a response. - Callback callback_; + // The callbacks called when the server sends a response. + typedef std::vector<RequestCallback> RequestCallbackList; + RequestCallbackList callback_list_; base::WeakPtrFactory<TranslateScript> weak_method_factory_; |