diff options
author | droger@chromium.org <droger@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-07 08:18:09 +0000 |
---|---|---|
committer | droger@chromium.org <droger@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-07 08:18:09 +0000 |
commit | 8bb49f306e67620b70e8a8114223879a3b6521ea (patch) | |
tree | 1ff60b9d87b7c323d0fc201888bc98d4d6b55168 | |
parent | d9a7cf7a118a3369c17095277b3f605bd9c36d04 (diff) | |
download | chromium_src-8bb49f306e67620b70e8a8114223879a3b6521ea.zip chromium_src-8bb49f306e67620b70e8a8114223879a3b6521ea.tar.gz chromium_src-8bb49f306e67620b70e8a8114223879a3b6521ea.tar.bz2 |
Move the translate script to the Translate component
This CL moves the TranslateScript class and the translate.js script.
A new translate_resources target is created.
BUG=335074
TBR=jochen
Review URL: https://codereview.chromium.org/138933006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@249620 0039d316-1c4b-4281-b951-d872f2087c98
38 files changed, 161 insertions, 130 deletions
diff --git a/chrome/browser/browser_resources.grd b/chrome/browser/browser_resources.grd index 51cc3ae..f41b156 100644 --- a/chrome/browser/browser_resources.grd +++ b/chrome/browser/browser_resources.grd @@ -276,7 +276,6 @@ <include name="IDR_SUGGESTIONS_PAGE_CSS" file="resources\ntp4\suggestions_page.css" type="BINDATA" /> <include name="IDR_SUGGESTIONS_PAGE_JS" file="resources\ntp4\suggestions_page.js" type="BINDATA" /> </if> - <include name="IDR_TRANSLATE_JS" file="resources\translate.js" type="BINDATA" /> <include name="IDR_UBER_HTML" file="resources\uber\uber.html" flattenhtml="true" type="BINDATA" /> <include name="IDR_UBER_JS" file="resources\uber\uber.js" type="BINDATA" /> <include name="IDR_UBER_FRAME_HTML" file="resources\uber\uber_frame.html" flattenhtml="true" type="BINDATA" /> diff --git a/chrome/browser/translate/translate_browsertest.cc b/chrome/browser/translate/translate_browsertest.cc index 600fd13..2e7f7ad 100644 --- a/chrome/browser/translate/translate_browsertest.cc +++ b/chrome/browser/translate/translate_browsertest.cc @@ -11,12 +11,12 @@ #include "chrome/browser/infobars/infobar_service.h" #include "chrome/browser/translate/translate_infobar_delegate.h" #include "chrome/browser/translate/translate_manager.h" -#include "chrome/browser/translate/translate_script.h" #include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/tabs/tab_strip_model.h" #include "chrome/test/base/in_process_browser_test.h" #include "chrome/test/base/test_switches.h" #include "chrome/test/base/ui_test_utils.h" +#include "components/translate/core/browser/translate_script.h" #include "components/translate/core/common/translate_switches.h" #include "content/public/browser/notification_service.h" #include "content/public/test/browser_test_utils.h" diff --git a/chrome/browser/translate/translate_manager.cc b/chrome/browser/translate/translate_manager.cc index 9f425e1..ddb7597 100644 --- a/chrome/browser/translate/translate_manager.cc +++ b/chrome/browser/translate/translate_manager.cc @@ -20,7 +20,6 @@ #include "chrome/browser/translate/translate_accept_languages.h" #include "chrome/browser/translate/translate_infobar_delegate.h" #include "chrome/browser/translate/translate_prefs.h" -#include "chrome/browser/translate/translate_script.h" #include "chrome/browser/translate/translate_tab_helper.h" #include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser_finder.h" @@ -38,6 +37,7 @@ #include "components/translate/core/browser/translate_download_manager.h" #include "components/translate/core/browser/translate_error_details.h" #include "components/translate/core/browser/translate_language_list.h" +#include "components/translate/core/browser/translate_script.h" #include "components/translate/core/browser/translate_url_util.h" #include "components/translate/core/common/language_detection_details.h" #include "components/translate/core/common/translate_constants.h" @@ -124,14 +124,6 @@ bool TranslateManager::IsAcceptLanguage(Profile* profile, return false; } -void TranslateManager::SetTranslateScriptExpirationDelay(int delay_ms) { - if (script_.get() == NULL) { - NOTREACHED(); - return; - } - script_->set_expiration_delay(delay_ms); -} - void TranslateManager::Observe(int type, const content::NotificationSource& source, const content::NotificationDetails& details) { @@ -258,7 +250,6 @@ TranslateManager::TranslateManager() notification_registrar_.Add(this, chrome::NOTIFICATION_PAGE_TRANSLATED, content::NotificationService::AllSources()); accept_languages_.reset(new TranslateAcceptLanguages); - script_.reset(new TranslateScript); } void TranslateManager::InitiateTranslation(WebContents* web_contents, @@ -449,11 +440,12 @@ void TranslateManager::TranslatePage(WebContents* web_contents, ShortcutConfig()); } - DCHECK(script_.get() != NULL); + TranslateScript* script = TranslateDownloadManager::GetInstance()->script(); + DCHECK(script != NULL); - const std::string& translate_script = script_->data(); - if (!translate_script.empty()) { - DoTranslatePage(web_contents, translate_script, source_lang, target_lang); + const std::string& script_data = script->data(); + if (!script_data.empty()) { + DoTranslatePage(web_contents, script_data, source_lang, target_lang); return; } @@ -468,10 +460,10 @@ void TranslateManager::TranslatePage(WebContents* web_contents, request.target_lang = target_lang; pending_requests_.push_back(request); - if (script_->HasPendingRequest()) + if (script->HasPendingRequest()) return; - script_->Request( + script->Request( base::Bind(&TranslateManager::OnTranslateScriptFetchComplete, base::Unretained(this))); } @@ -522,14 +514,6 @@ void TranslateManager::ReportLanguageDetectionError(WebContents* web_contents) { content::PAGE_TRANSITION_AUTO_BOOKMARK); } -void TranslateManager::ClearTranslateScript() { - if (script_.get() == NULL) { - NOTREACHED(); - return; - } - script_->Clear(); -} - void TranslateManager::DoTranslatePage(WebContents* web_contents, const std::string& translate_script, const std::string& source_lang, @@ -588,10 +572,6 @@ void TranslateManager::PageTranslated(WebContents* web_contents, } } -void TranslateManager::CleanupPendingUlrFetcher() { - script_.reset(); -} - void TranslateManager::OnTranslateScriptFetchComplete( bool success, const std::string& data) { std::vector<PendingRequest>::const_iterator iter; @@ -613,8 +593,10 @@ void TranslateManager::OnTranslateScriptFetchComplete( if (success) { // Translate the page. - const std::string& translate_script = script_->data(); - DoTranslatePage(web_contents, translate_script, + TranslateScript* translate_script = + TranslateDownloadManager::GetInstance()->script(); + DCHECK(translate_script); + DoTranslatePage(web_contents, translate_script->data(), request.source_lang, request.target_lang); } else { if (IsTranslateBubbleEnabled()) { diff --git a/chrome/browser/translate/translate_manager.h b/chrome/browser/translate/translate_manager.h index eb1cc65..d981ba7 100644 --- a/chrome/browser/translate/translate_manager.h +++ b/chrome/browser/translate/translate_manager.h @@ -28,7 +28,6 @@ struct ShortcutConfiguration; class TranslateAcceptLanguages; struct TranslateErrorDetails; class TranslateInfoBarDelegate; -class TranslateScript; namespace content { class WebContents; @@ -76,10 +75,6 @@ class TranslateManager : public content::NotificationObserver { // used only for tests. static void SetUseInfobar(bool value); - // Allows caller to cleanup pending URLFetcher objects to make sure they - // get released in the appropriate thread... Mainly for tests. - void CleanupPendingUlrFetcher(); - // Translates the page contents from |source_lang| to |target_lang|. // The actual translation might be performed asynchronously if the translate // script is not yet available. @@ -96,19 +91,11 @@ class TranslateManager : public content::NotificationObserver { // under options in the translate infobar. void ReportLanguageDetectionError(content::WebContents* web_contents); - // Clears the translate script, so it will be fetched next time we translate. - void ClearTranslateScript(); - // content::NotificationObserver implementation: virtual void Observe(int type, const content::NotificationSource& source, const content::NotificationDetails& details) OVERRIDE; - // Used by unit-tests to override some defaults: - // Delay after which the translate script is fetched again from the - // translation server. - void SetTranslateScriptExpirationDelay(int delay_ms); - // Number of attempts before waiting for a page to be fully reloaded. void set_translate_max_reload_attemps(int attempts) { max_reload_check_attempts_ = attempts; @@ -198,10 +185,6 @@ class TranslateManager : public content::NotificationObserver { // List of registered observers. ObserverList<Observer> observer_list_; - // An instance of TranslateScript which manages JavaScript source for - // Translate. - scoped_ptr<TranslateScript> script_; - // An instance of TranslateAcceptLanguages which manages Accept languages of // each profiles. scoped_ptr<TranslateAcceptLanguages> accept_languages_; diff --git a/chrome/browser/translate/translate_manager_browsertest.cc b/chrome/browser/translate/translate_manager_browsertest.cc index fa76014..ed9aafb 100644 --- a/chrome/browser/translate/translate_manager_browsertest.cc +++ b/chrome/browser/translate/translate_manager_browsertest.cc @@ -21,7 +21,6 @@ #include "chrome/browser/translate/translate_infobar_delegate.h" #include "chrome/browser/translate/translate_manager.h" #include "chrome/browser/translate/translate_prefs.h" -#include "chrome/browser/translate/translate_script.h" #include "chrome/browser/translate/translate_service.h" #include "chrome/browser/translate/translate_tab_helper.h" #include "chrome/browser/ui/browser.h" @@ -40,6 +39,7 @@ #include "chrome/test/base/ui_test_utils.h" #include "components/translate/core/browser/translate_download_manager.h" #include "components/translate/core/browser/translate_language_list.h" +#include "components/translate/core/browser/translate_script.h" #include "components/translate/core/common/language_detection_details.h" #include "components/translate/core/common/translate_pref_names.h" #include "content/public/browser/navigation_details.h" @@ -179,7 +179,8 @@ class TranslateManagerBrowserTest : public ChromeRenderViewHostTestHarness, } void ExpireTranslateScriptImmediately() { - TranslateManager::GetInstance()->SetTranslateScriptExpirationDelay(0); + TranslateDownloadManager::GetInstance()->SetTranslateScriptExpirationDelay( + 0); } // If there is 1 infobar and it is a translate infobar, deny translation and @@ -237,9 +238,10 @@ class TranslateManagerBrowserTest : public ChromeRenderViewHostTestHarness, // everytime and sets the expiration delay to a large value by default (in // case it was zeroed in a previous test). TranslateService::Initialize(); - TranslateManager::GetInstance()->ClearTranslateScript(); - TranslateManager::GetInstance()-> - SetTranslateScriptExpirationDelay(60 * 60 * 1000); + TranslateDownloadManager* download_manager = + TranslateDownloadManager::GetInstance(); + download_manager->ClearTranslateScriptForTesting(); + download_manager->SetTranslateScriptExpirationDelay(60 * 60 * 1000); TranslateManager::GetInstance()->set_translate_max_reload_attemps(0); TranslateManager::SetUseInfobar(true); diff --git a/chrome/browser/translate/translate_service.cc b/chrome/browser/translate/translate_service.cc index cfd2edc..a7625a9 100644 --- a/chrome/browser/translate/translate_service.cc +++ b/chrome/browser/translate/translate_service.cc @@ -42,7 +42,6 @@ void TranslateService::Shutdown(bool cleanup_pending_fetcher) { TranslateDownloadManager* download_manager = TranslateDownloadManager::GetInstance(); if (cleanup_pending_fetcher) { - TranslateManager::GetInstance()->CleanupPendingUlrFetcher(); download_manager->Shutdown(); } else { // This path is only used by tests. diff --git a/chrome/browser/translate/translate_service.h b/chrome/browser/translate/translate_service.h index 96a353a..b544b5b 100644 --- a/chrome/browser/translate/translate_service.h +++ b/chrome/browser/translate/translate_service.h @@ -7,6 +7,8 @@ #include "chrome/browser/web_resource/resource_request_allowed_notifier.h" +class PrefService; + // Singleton managing the resources required for Translate. class TranslateService : public ResourceRequestAllowedNotifier::Observer { public: @@ -16,6 +18,11 @@ class TranslateService : public ResourceRequestAllowedNotifier::Observer { // Must be called to shut down the Translate feature. static void Shutdown(bool cleanup_pending_fetcher); + // Let the caller decide if and when we should fetch the language list from + // the translate server. This is a NOOP if switches::kDisableTranslate is set + // or if prefs::kEnableTranslate is set to false. + static void FetchLanguageListFromTranslateServer(PrefService* prefs); + private: TranslateService(); ~TranslateService(); diff --git a/chrome/browser/ui/startup/bad_flags_prompt.cc b/chrome/browser/ui/startup/bad_flags_prompt.cc index a40cdc9..a3333e7 100644 --- a/chrome/browser/ui/startup/bad_flags_prompt.cc +++ b/chrome/browser/ui/startup/bad_flags_prompt.cc @@ -36,7 +36,7 @@ void ShowBadFlagsPrompt(Browser* browser) { switches::kEnableBrowserPluginForAllViewTypes, extensions::switches::kExtensionsOnChromeURLs, // This parameter should be used only for server side developments. - switches::kTranslateScriptURL, + translate::switches::kTranslateScriptURL, translate::switches::kTranslateSecurityOrigin, #if defined(ENABLE_WEBRTC) // This flag disables security of media packets in WebRTC. diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index fcf6b46..30e15d7 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -2535,8 +2535,6 @@ 'browser/translate/translate_manager.h', 'browser/translate/translate_prefs.cc', 'browser/translate/translate_prefs.h', - 'browser/translate/translate_script.cc', - 'browser/translate/translate_script.h', 'browser/translate/translate_service.cc', 'browser/translate/translate_service.h', 'browser/translate/translate_tab_helper.cc', diff --git a/chrome/chrome_repack_resources.gypi b/chrome/chrome_repack_resources.gypi index 20d9ab9..688b504 100644 --- a/chrome/chrome_repack_resources.gypi +++ b/chrome/chrome_repack_resources.gypi @@ -6,7 +6,7 @@ 'variables': { 'pak_inputs': [ '<(SHARED_INTERMEDIATE_DIR)/chrome/chrome_unscaled_resources.pak', - '<(SHARED_INTERMEDIATE_DIR)/components/dom_distiller_resources.pak', + '<(SHARED_INTERMEDIATE_DIR)/components/component_resources.pak', '<(SHARED_INTERMEDIATE_DIR)/net/net_resources.pak', '<(SHARED_INTERMEDIATE_DIR)/ui/ui_resources/webui_resources.pak', '<(grit_out_dir)/browser_resources.pak', diff --git a/chrome/chrome_resources.gyp b/chrome/chrome_resources.gyp index 608b2bd..20d8e23 100644 --- a/chrome/chrome_resources.gyp +++ b/chrome/chrome_resources.gyp @@ -30,7 +30,7 @@ }, 'includes': [ '../build/grit_action.gypi' ], }, - { + { 'action_name': 'invalidations_resources', 'variables': { 'grit_grd_file': 'browser/resources/invalidations_resources.grd', @@ -63,7 +63,7 @@ 'conditions': [ ['OS != "ios"', { 'dependencies': [ - '../components/components.gyp:dom_distiller_resources', + '../components/component_resources.gyp:component_resources', '../content/browser/devtools/devtools_resources.gyp:devtools_resources', '../content/browser/tracing/tracing_resources.gyp:tracing_resources', ], diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index 797c491..c0f25c7 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -920,6 +920,8 @@ '../components/components.gyp:autofill_content_test_support', '../components/components.gyp:dom_distiller_test_support', '../components/components.gyp:dom_distiller_content', + '../components/components.gyp:translate_core_common', + '../components/component_resources.gyp:component_resources', '../components/component_strings.gyp:component_strings', '../device/bluetooth/bluetooth.gyp:device_bluetooth_mocks', '../google_apis/google_apis.gyp:google_apis_test_support', diff --git a/chrome/chrome_tests_unit.gypi b/chrome/chrome_tests_unit.gypi index 38dc94d..c3adfc5 100644 --- a/chrome/chrome_tests_unit.gypi +++ b/chrome/chrome_tests_unit.gypi @@ -1417,7 +1417,6 @@ 'browser/thumbnails/thumbnail_service_unittest.cc', 'browser/translate/translate_manager_unittest.cc', 'browser/translate/translate_prefs_unittest.cc', - 'browser/translate/translate_script_unittest.cc', 'browser/ui/android/tab_model/tab_model_unittest.cc', 'browser/ui/ash/event_rewriter_unittest.cc', 'browser/ui/ash/ime_controller_chromeos_unittest.cc', diff --git a/chrome/common/chrome_switches.cc b/chrome/common/chrome_switches.cc index 300604c..7e9abf9 100644 --- a/chrome/common/chrome_switches.cc +++ b/chrome/common/chrome_switches.cc @@ -1457,9 +1457,6 @@ const char kTestType[] = "test-type"; // channel with the given ID. const char kTestingChannelID[] = "testing-channel"; -// Overrides the default server used for Google Translate. -const char kTranslateScriptURL[] = "translate-script-url"; - // Disables same-origin check on HTTP resources pushed via a SPDY proxy. // The value is the host:port of the trusted proxy. const char kTrustedSpdyProxy[] = "trusted-spdy-proxy"; diff --git a/chrome/common/chrome_switches.h b/chrome/common/chrome_switches.h index 730d14e..e074f23 100644 --- a/chrome/common/chrome_switches.h +++ b/chrome/common/chrome_switches.h @@ -390,7 +390,6 @@ extern const char kTabCapture[]; extern const char kTestName[]; extern const char kTestType[]; extern const char kTestingChannelID[]; -extern const char kTranslateScriptURL[]; extern const char kTrustedSpdyProxy[]; extern const char kTryChromeAgain[]; extern const char kUninstallExtension[]; diff --git a/chrome/renderer/translate/translate_script_browsertest.cc b/chrome/renderer/translate/translate_script_browsertest.cc index b218664..f7b846e 100644 --- a/chrome/renderer/translate/translate_script_browsertest.cc +++ b/chrome/renderer/translate/translate_script_browsertest.cc @@ -6,7 +6,7 @@ #include "base/strings/utf_string_conversions.h" #include "chrome/test/base/chrome_render_view_test.h" #include "components/translate/core/common/translate_errors.h" -#include "grit/browser_resources.h" +#include "grit/component_resources.h" #include "third_party/WebKit/public/web/WebFrame.h" #include "third_party/WebKit/public/web/WebScriptSource.h" #include "ui/base/resource/resource_bundle.h" diff --git a/components/component_resources.grd b/components/component_resources.grd new file mode 100644 index 0000000..a996489 --- /dev/null +++ b/components/component_resources.grd @@ -0,0 +1,16 @@ +<?xml version="1.0" encoding="UTF-8"?> +<grit latest_public_release="0" current_release="1"> + <outputs> + <output filename="grit/component_resources.h" type="rc_header"> + <emit emit_type='prepend'></emit> + </output> + <output filename="component_resources.pak" type="data_package" /> + <output filename="component_resources.rc" type="rc_all" /> + </outputs> + <release seq="1"> + <includes> + <part file="dom_distiller_resources.grdp" /> + <part file="translate_resources.grdp" /> + </includes> + </release> +</grit> diff --git a/components/component_resources.gyp b/components/component_resources.gyp new file mode 100644 index 0000000..a626c00 --- /dev/null +++ b/components/component_resources.gyp @@ -0,0 +1,25 @@ +# 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. + +{ + 'targets': [ + { + 'target_name': 'component_resources', + 'type': 'none', + 'variables': { + 'grit_out_dir': '<(SHARED_INTERMEDIATE_DIR)/components', + }, + 'actions': [ + { + 'action_name': 'component_resources', + 'variables': { + 'grit_grd_file': 'component_resources.grd', + }, + 'includes': [ '../build/grit_action.gypi' ], + }, + ], + 'includes': [ '../build/grit_target.gypi' ], + }, + ], +} diff --git a/components/components_tests.gyp b/components/components_tests.gyp index ac5db07..9bebaa2 100644 --- a/components/components_tests.gyp +++ b/components/components_tests.gyp @@ -50,6 +50,7 @@ 'test/run_all_unittests.cc', 'translate/core/browser/language_state_unittest.cc', 'translate/core/browser/translate_browser_metrics_unittest.cc', + 'translate/core/browser/translate_script_unittest.cc', 'translate/core/common/translate_metrics_unittest.cc', 'translate/core/common/translate_util_unittest.cc', 'translate/language_detection/language_detection_util_unittest.cc', diff --git a/components/dom_distiller.gypi b/components/dom_distiller.gypi index c22d5c0..78bda80 100644 --- a/components/dom_distiller.gypi +++ b/components/dom_distiller.gypi @@ -10,10 +10,10 @@ 'target_name': 'dom_distiller_webui', 'type': 'static_library', 'dependencies': [ + 'component_resources.gyp:component_resources', 'component_strings.gyp:component_strings', 'distilled_page_proto', 'dom_distiller_core', - 'dom_distiller_resources', '../base/base.gyp:base', '../content/content.gyp:content_browser', '../skia/skia.gyp:skia', @@ -30,28 +30,11 @@ ], }, { - 'target_name': 'dom_distiller_resources', - 'type': 'none', - 'variables': { - 'grit_out_dir': '<(SHARED_INTERMEDIATE_DIR)/components', - }, - 'actions': [ - { - 'action_name': 'dom_distiller_resources', - 'variables': { - 'grit_grd_file': 'dom_distiller_resources.grd', - }, - 'includes': [ '../build/grit_action.gypi' ], - }, - ], - 'includes': [ '../build/grit_target.gypi' ], - }, - { 'target_name': 'dom_distiller_core', 'type': 'static_library', 'dependencies': [ + 'component_resources.gyp:component_resources', 'distilled_page_proto', - 'dom_distiller_resources', '../base/base.gyp:base', '../skia/skia.gyp:skia', '../sync/sync.gyp:sync', @@ -131,9 +114,9 @@ 'target_name': 'dom_distiller_content', 'type': 'static_library', 'dependencies': [ + 'component_resources.gyp:component_resources', 'component_strings.gyp:component_strings', 'dom_distiller_core', - 'dom_distiller_resources', '../net/net.gyp:net', '../skia/skia.gyp:skia', '../sync/sync.gyp:sync', diff --git a/components/dom_distiller/content/dom_distiller_viewer_source.cc b/components/dom_distiller/content/dom_distiller_viewer_source.cc index 8e913d5..375a542 100644 --- a/components/dom_distiller/content/dom_distiller_viewer_source.cc +++ b/components/dom_distiller/content/dom_distiller_viewer_source.cc @@ -18,8 +18,8 @@ #include "components/dom_distiller/core/task_tracker.h" #include "content/public/browser/render_frame_host.h" #include "content/public/browser/render_view_host.h" +#include "grit/component_resources.h" #include "grit/component_strings.h" -#include "grit/dom_distiller_resources.h" #include "net/base/escape.h" #include "net/url_request/url_request.h" #include "ui/base/l10n/l10n_util.h" diff --git a/components/dom_distiller/core/page_distiller.cc b/components/dom_distiller/core/page_distiller.cc index 2d47bd1..d0af644 100644 --- a/components/dom_distiller/core/page_distiller.cc +++ b/components/dom_distiller/core/page_distiller.cc @@ -12,7 +12,7 @@ #include "base/values.h" #include "components/dom_distiller/core/distiller_page.h" #include "components/dom_distiller/core/distiller_url_fetcher.h" -#include "grit/dom_distiller_resources.h" +#include "grit/component_resources.h" #include "net/url_request/url_request_context_getter.h" #include "ui/base/resource/resource_bundle.h" #include "url/gurl.h" diff --git a/components/dom_distiller/webui/dom_distiller_ui.cc b/components/dom_distiller/webui/dom_distiller_ui.cc index 807a6b3..4e3550c 100644 --- a/components/dom_distiller/webui/dom_distiller_ui.cc +++ b/components/dom_distiller/webui/dom_distiller_ui.cc @@ -11,8 +11,8 @@ #include "content/public/browser/web_contents.h" #include "content/public/browser/web_ui.h" #include "content/public/browser/web_ui_data_source.h" +#include "grit/component_resources.h" #include "grit/component_strings.h" -#include "grit/dom_distiller_resources.h" namespace dom_distiller { diff --git a/components/dom_distiller_resources.grd b/components/dom_distiller_resources.grd deleted file mode 100644 index 7e9ec0c..0000000 --- a/components/dom_distiller_resources.grd +++ /dev/null @@ -1,20 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<grit latest_public_release="0" current_release="1"> - <outputs> - <output filename="grit/dom_distiller_resources.h" type="rc_header"> - <emit emit_type='prepend'></emit> - </output> - <output filename="dom_distiller_resources.pak" type="data_package" /> - <output filename="dom_distiller_resources.rc" type="rc_all" /> - </outputs> - <release seq="1"> - <includes> - <include name="IDR_ABOUT_DOM_DISTILLER_HTML" file="dom_distiller/webui/resources/about_dom_distiller.html" flattenhtml="true" allowexternalscript="true" type="BINDATA" /> - <include name="IDR_ABOUT_DOM_DISTILLER_CSS" file="dom_distiller/webui/resources/about_dom_distiller.css" type="BINDATA" /> - <include name="IDR_ABOUT_DOM_DISTILLER_JS" file="dom_distiller/webui/resources/about_dom_distiller.js" type="BINDATA" /> - <include name="IDR_DOM_DISTILLER_VIEWER_HTML" file="dom_distiller/content/resources/dom_distiller_viewer.html" type="BINDATA" /> - <include name="IDR_DISTILLER_JS" file="../third_party/readability/js/readability.js" type="BINDATA" /> - <include name="IDR_DISTILLER_CSS" file="../third_party/readability/css/readability.css" type="BINDATA" /> - </includes> - </release> -</grit> diff --git a/components/dom_distiller_resources.grdp b/components/dom_distiller_resources.grdp new file mode 100644 index 0000000..bb2092e --- /dev/null +++ b/components/dom_distiller_resources.grdp @@ -0,0 +1,9 @@ +<?xml version="1.0" encoding="utf-8"?> +<grit-part> + <include name="IDR_ABOUT_DOM_DISTILLER_HTML" file="dom_distiller/webui/resources/about_dom_distiller.html" flattenhtml="true" allowexternalscript="true" type="BINDATA" /> + <include name="IDR_ABOUT_DOM_DISTILLER_CSS" file="dom_distiller/webui/resources/about_dom_distiller.css" type="BINDATA" /> + <include name="IDR_ABOUT_DOM_DISTILLER_JS" file="dom_distiller/webui/resources/about_dom_distiller.js" type="BINDATA" /> + <include name="IDR_DOM_DISTILLER_VIEWER_HTML" file="dom_distiller/content/resources/dom_distiller_viewer.html" type="BINDATA" /> + <include name="IDR_DISTILLER_JS" file="../third_party/readability/js/readability.js" type="BINDATA" /> + <include name="IDR_DISTILLER_CSS" file="../third_party/readability/css/readability.css" type="BINDATA" /> +</grit-part> diff --git a/components/translate.gypi b/components/translate.gypi index d38d2b3..81afa62 100644 --- a/components/translate.gypi +++ b/components/translate.gypi @@ -8,6 +8,7 @@ 'target_name': 'translate_core_browser', 'type': 'static_library', 'dependencies': [ + 'component_resources.gyp:component_resources', 'language_usage_metrics', 'translate_core_common', '../base/base.gyp:base', @@ -33,6 +34,8 @@ 'translate/core/browser/translate_event_details.h', 'translate/core/browser/translate_language_list.cc', 'translate/core/browser/translate_language_list.h', + 'translate/core/browser/translate_script.cc', + 'translate/core/browser/translate_script.h', 'translate/core/browser/translate_url_fetcher.cc', 'translate/core/browser/translate_url_fetcher.h', 'translate/core/browser/translate_url_util.cc', diff --git a/components/translate/DEPS b/components/translate/DEPS index 64ab5b8..21d3161 100644 --- a/components/translate/DEPS +++ b/components/translate/DEPS @@ -1,6 +1,7 @@ include_rules = [ "+components/language_usage_metrics", "+google_apis", + "+grit", # For generated headers. "+net", "+ui", diff --git a/chrome/browser/resources/translate.js b/components/translate/core/browser/resources/translate.js index 9d389ff..fb59e66 100644 --- a/chrome/browser/resources/translate.js +++ b/components/translate/core/browser/resources/translate.js @@ -1,4 +1,4 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// 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. diff --git a/components/translate/core/browser/translate_download_manager.cc b/components/translate/core/browser/translate_download_manager.cc index 4542d20..9d1898c 100644 --- a/components/translate/core/browser/translate_download_manager.cc +++ b/components/translate/core/browser/translate_download_manager.cc @@ -16,12 +16,14 @@ TranslateDownloadManager* TranslateDownloadManager::GetInstance() { } TranslateDownloadManager::TranslateDownloadManager() - : language_list_(new TranslateLanguageList) {} + : language_list_(new TranslateLanguageList), + script_(new TranslateScript) {} TranslateDownloadManager::~TranslateDownloadManager() {} void TranslateDownloadManager::Shutdown() { language_list_.reset(); + script_.reset(); request_context_ = NULL; } @@ -106,3 +108,19 @@ bool TranslateDownloadManager::IsAlphaLanguage(const std::string& language) { return language_list->IsAlphaLanguage(language); } + +void TranslateDownloadManager::ClearTranslateScriptForTesting() { + if (script_.get() == NULL) { + NOTREACHED(); + return; + } + script_->Clear(); +} + +void TranslateDownloadManager::SetTranslateScriptExpirationDelay(int delay_ms) { + if (script_.get() == NULL) { + NOTREACHED(); + return; + } + script_->set_expiration_delay(delay_ms); +} diff --git a/components/translate/core/browser/translate_download_manager.h b/components/translate/core/browser/translate_download_manager.h index 6cffd27..f8dcae5 100644 --- a/components/translate/core/browser/translate_download_manager.h +++ b/components/translate/core/browser/translate_download_manager.h @@ -11,6 +11,7 @@ #include "base/memory/ref_counted.h" #include "base/memory/scoped_ptr.h" #include "components/translate/core/browser/translate_language_list.h" +#include "components/translate/core/browser/translate_script.h" #include "net/url_request/url_request_context_getter.h" template <typename T> struct DefaultSingletonTraits; @@ -19,8 +20,6 @@ class PrefService; // Manages the downloaded resources for Translate, such as the translate script // and the language list. -// TODO(droger): TranslateDownloadManager should own TranslateScript. -// See http://crbug.com/335074. class TranslateDownloadManager { public: // Returns the singleton instance. @@ -46,6 +45,9 @@ class TranslateDownloadManager { // The language list. TranslateLanguageList* language_list() { return language_list_.get(); } + // The translate script. + TranslateScript* script() { return script_.get(); } + // Let the caller decide if and when we should fetch the language list from // the translate server. This is a NOOP if switches::kDisableTranslate is set // or if prefs::kEnableTranslate is set to false. @@ -77,12 +79,25 @@ class TranslateDownloadManager { // Must be called to shut Translate down. Cancels any pending fetches. void Shutdown(); + // Clears the translate script, so it will be fetched next time we translate. + void ClearTranslateScriptForTesting(); + + // Used by unit-tests to override some defaults: + // Delay after which the translate script is fetched again from the + // translation server. + void SetTranslateScriptExpirationDelay(int delay_ms); + private: friend struct DefaultSingletonTraits<TranslateDownloadManager>; TranslateDownloadManager(); virtual ~TranslateDownloadManager(); scoped_ptr<TranslateLanguageList> language_list_; + + // An instance of TranslateScript which manages JavaScript source for + // Translate. + scoped_ptr<TranslateScript> script_; + std::string application_locale_; scoped_refptr<net::URLRequestContextGetter> request_context_; }; diff --git a/chrome/browser/translate/translate_script.cc b/components/translate/core/browser/translate_script.cc index c622f4d..a83f499 100644 --- a/chrome/browser/translate/translate_script.cc +++ b/components/translate/core/browser/translate_script.cc @@ -1,8 +1,8 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. +// 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 "chrome/browser/translate/translate_script.h" +#include "components/translate/core/browser/translate_script.h" #include "base/bind.h" #include "base/command_line.h" @@ -11,12 +11,12 @@ #include "base/strings/string_piece.h" #include "base/strings/string_util.h" #include "base/strings/stringprintf.h" -#include "chrome/common/chrome_switches.h" #include "components/translate/core/browser/translate_url_fetcher.h" #include "components/translate/core/browser/translate_url_util.h" +#include "components/translate/core/common/translate_switches.h" #include "components/translate/core/common/translate_util.h" #include "google_apis/google_api_keys.h" -#include "grit/browser_resources.h" +#include "grit/component_resources.h" #include "net/base/escape.h" #include "net/base/url_util.h" #include "ui/base/resource/resource_bundle.h" @@ -62,9 +62,9 @@ void TranslateScript::Request(const Callback& callback) { GURL translate_script_url; // Check if command-line contains an alternative URL for translate service. const CommandLine& command_line = *CommandLine::ForCurrentProcess(); - if (command_line.HasSwitch(switches::kTranslateScriptURL)) { - translate_script_url = GURL( - command_line.GetSwitchValueASCII(switches::kTranslateScriptURL)); + if (command_line.HasSwitch(translate::switches::kTranslateScriptURL)) { + translate_script_url = GURL(command_line.GetSwitchValueASCII( + translate::switches::kTranslateScriptURL)); if (!translate_script_url.is_valid() || !translate_script_url.query().empty()) { LOG(WARNING) << "The following translate URL specified at the " diff --git a/chrome/browser/translate/translate_script.h b/components/translate/core/browser/translate_script.h index 3a529f5..552daee 100644 --- a/chrome/browser/translate/translate_script.h +++ b/components/translate/core/browser/translate_script.h @@ -1,9 +1,9 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. +// 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 CHROME_BROWSER_TRANSLATE_TRANSLATE_SCRIPT_H_ -#define CHROME_BROWSER_TRANSLATE_TRANSLATE_SCRIPT_H_ +#ifndef COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_SCRIPT_H_ +#define COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_SCRIPT_H_ #include <string> @@ -91,4 +91,4 @@ class TranslateScript { DISALLOW_COPY_AND_ASSIGN(TranslateScript); }; -#endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_SCRIPT_H_ +#endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_SCRIPT_H_ diff --git a/chrome/browser/translate/translate_script_unittest.cc b/components/translate/core/browser/translate_script_unittest.cc index fe67818..c8e3316 100644 --- a/chrome/browser/translate/translate_script_unittest.cc +++ b/components/translate/core/browser/translate_script_unittest.cc @@ -1,14 +1,14 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. +// 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 "chrome/browser/translate/translate_script.h" +#include "components/translate/core/browser/translate_script.h" #include "base/bind.h" #include "base/command_line.h" #include "base/strings/stringprintf.h" -#include "chrome/common/chrome_switches.h" #include "components/translate/core/browser/translate_download_manager.h" +#include "components/translate/core/common/translate_switches.h" #include "net/base/load_flags.h" #include "net/base/url_util.h" #include "net/http/http_request_headers.h" @@ -84,6 +84,8 @@ TEST_F(TranslateScriptTest, CheckScriptParameters) { url, TranslateScript::kCallbackQueryName, &callback); EXPECT_EQ(std::string(TranslateScript::kCallbackQueryValue), callback); +#if !defined(OS_IOS) + // iOS does not have specific loaders for the isolated world. std::string css_loader_callback; net::GetValueForKeyInQuery( url, TranslateScript::kCssLoaderCallbackQueryName, &css_loader_callback); @@ -97,12 +99,13 @@ TEST_F(TranslateScriptTest, CheckScriptParameters) { &javascript_loader_callback); EXPECT_EQ(std::string(TranslateScript::kJavascriptLoaderCallbackQueryValue), javascript_loader_callback); +#endif // !defined(OS_IOS) } TEST_F(TranslateScriptTest, CheckScriptURL) { const std::string script_url("http://www.tamurayukari.com/mero-n.js"); CommandLine* command_line = CommandLine::ForCurrentProcess(); - command_line->AppendSwitchASCII(switches::kTranslateScriptURL, + command_line->AppendSwitchASCII(translate::switches::kTranslateScriptURL, script_url); Request(); diff --git a/components/translate/core/common/translate_switches.cc b/components/translate/core/common/translate_switches.cc index 71fdf12..1ac6f33 100644 --- a/components/translate/core/common/translate_switches.cc +++ b/components/translate/core/common/translate_switches.cc @@ -12,6 +12,9 @@ namespace switches { // disable translate with the preference. const char kDisableTranslate[] = "disable-translate"; +// Overrides the default server used for Google Translate. +const char kTranslateScriptURL[] = "translate-script-url"; + // Overrides security-origin with which Translate runs in an isolated world. const char kTranslateSecurityOrigin[] = "translate-security-origin"; diff --git a/components/translate/core/common/translate_switches.h b/components/translate/core/common/translate_switches.h index 4ab971f..ace4f3a 100644 --- a/components/translate/core/common/translate_switches.h +++ b/components/translate/core/common/translate_switches.h @@ -9,6 +9,7 @@ namespace translate { namespace switches { extern const char kDisableTranslate[]; +extern const char kTranslateScriptURL[]; extern const char kTranslateSecurityOrigin[]; } // namespace switches diff --git a/components/translate_resources.grdp b/components/translate_resources.grdp new file mode 100644 index 0000000..5185e3a --- /dev/null +++ b/components/translate_resources.grdp @@ -0,0 +1,4 @@ +<?xml version="1.0" encoding="utf-8"?> +<grit-part> + <include name="IDR_TRANSLATE_JS" file="translate/core/browser/resources/translate.js" type="BINDATA" /> +</grit-part> diff --git a/tools/gn/secondary/components/translate/BUILD.gn b/tools/gn/secondary/components/translate/BUILD.gn index ad55687..de090b9 100644 --- a/tools/gn/secondary/components/translate/BUILD.gn +++ b/tools/gn/secondary/components/translate/BUILD.gn @@ -19,6 +19,8 @@ static_library("translate_core_browser") { "core/browser/translate_event_details.h", "core/browser/translate_language_list.cc", "core/browser/translate_language_list.h", + "core/browser/translate_script.cc", + "core/browser/translate_script.h", "core/browser/translate_url_fetcher.cc", "core/browser/translate_url_fetcher.h", "core/browser/translate_url_util.cc", diff --git a/tools/gritsettings/resource_ids b/tools/gritsettings/resource_ids index d6d898a..4c754a7 100644 --- a/tools/gritsettings/resource_ids +++ b/tools/gritsettings/resource_ids @@ -194,7 +194,7 @@ "components/component_strings.grd": { "messages": [30000], }, - "components/dom_distiller_resources.grd": { + "components/component_resources.grd": { "includes": [30250], }, "third_party/WebKit/public/blink_resources.grd": { |