diff options
author | jcivelli@google.com <jcivelli@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-06 22:21:02 +0000 |
---|---|---|
committer | jcivelli@google.com <jcivelli@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-06 22:21:02 +0000 |
commit | 85d252e22b6bec161873a0b5656d59c8ebe04e30 (patch) | |
tree | 937f8add8eac344f93d6a8ec6604796f371c761c /chrome/renderer/translate_helper.h | |
parent | d41661b5e24c8d774acea07c05ef2e5896587e56 (diff) | |
download | chromium_src-85d252e22b6bec161873a0b5656d59c8ebe04e30.zip chromium_src-85d252e22b6bec161873a0b5656d59c8ebe04e30.tar.gz chromium_src-85d252e22b6bec161873a0b5656d59c8ebe04e30.tar.bz2 |
Changing the translate back-end to use the Google Translate element.
When the user indicates that a page should be translated, the browser first fetches the Google Translate Element JS code.
It then sends it to the renderer, which injects the script in the page, waits for the Translate element to be initialized and then calls the translate method on it.
The TranslationService class previously used to translate text chunks is now unused and has been removed. Some of its static methods that are still used have been moved to the TranslateManager class.
This CL also implements the "revert" translation behavior.
BUG=35474,37778,35553,39375
TEST=Test the translation feature extensively.
Review URL: http://codereview.chromium.org/1599016
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@43768 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/renderer/translate_helper.h')
-rw-r--r-- | chrome/renderer/translate_helper.h | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/chrome/renderer/translate_helper.h b/chrome/renderer/translate_helper.h new file mode 100644 index 0000000..efd5a72 --- /dev/null +++ b/chrome/renderer/translate_helper.h @@ -0,0 +1,103 @@ +// Copyright (c) 2010 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_RENDERER_TRANSLATE_HELPER_H_ +#define CHROME_RENDERER_TRANSLATE_HELPER_H_ + +#include <string> + +#include "base/task.h" +#include "chrome/common/translate_errors.h" + +class RenderView; + +// This class deals with page translation. +// There is one TranslateHelper per RenderView. + +class TranslateHelper { + public: + explicit TranslateHelper(RenderView* render_view); + virtual ~TranslateHelper() {} + + // Translates the page contents from |source_lang| to |target_lang|. + // Does nothing if |page_id| is not the current page id. + // If the library is not ready, it will post a task to try again after 50ms. + void TranslatePage(int page_id, + const std::string& source_lang, + const std::string& target_lang, + const std::string& translate_script); + + // Reverts the page's text to its original contents. + void RevertTranslation(int page_id); + + protected: + // The following methods are protected so they can be overridden in + // unit-tests. + + // Returns true if the translate library is available, meaning the JavaScript + // has already been injected in that page. + virtual bool IsTranslateLibAvailable(); + + // Returns true if the translate library has been initialized successfully. + virtual bool IsTranslateLibReady(); + + // Returns true if the translation script has finished translating the page. + virtual bool HasTranslationFinished(); + + // Returns true if the translation script has reported an error performing the + // translation. + virtual bool HasTranslationFailed(); + + // Starts the translation by calling the translate library. This method + // should only be called when the translate script has been injected in the + // page. Returns false if the call failed immediately. + virtual bool StartTranslation(const std::string& original_lang, + const std::string& target_lang); + + // Used in unit-tests. Makes the various tasks be posted immediately so that + // the tests don't have to wait before checking states. + virtual bool DontDelayTasks() { return false; } + + private: + // Checks if the current running page translation is finished or errored and + // notifies the browser accordingly. If the translation has not terminated, + // posts a task to check again later. + void CheckTranslateStatus(int page_id, + const std::string& source_lang, + const std::string& target_lang); + + // Executes the JavaScript code in |script| in the main frame of + // |render_view_host_|. + // Returns true if the code was executed successfully. + bool ExecuteScript(const std::string& script); + + // Executes the JavaScript code in |script| in the main frame of + // |render_view_host_|, and sets |value| to the boolean returned by the script + // evaluation. Returns true if the script was run successfully and returned + // a boolean, false otherwise + bool ExecuteScriptAndGetBoolResult(const std::string& script, bool* value); + + // Called by TranslatePage to do the actual translation. |count| is used to + // limit the number of retries. + void TranslatePageImpl(int page_id, + const std::string& source_lang, + const std::string& target_lang, + int count); + + // Sends a message to the browser to notify it that the translation failed + // with |error|. + void NotifyBrowserTranslationFailed(const std::string& original_lang, + const std::string& target_lang, + TranslateErrors::Type error); + + // The RenderView we are performing translations for. + RenderView* render_view_; + + // Method factory used to make calls to TranslatePageImpl. + ScopedRunnableMethodFactory<TranslateHelper> method_factory_; + + DISALLOW_COPY_AND_ASSIGN(TranslateHelper); +}; + +#endif // CHROME_RENDERER_TRANSLATE_HELPER_H_ |