Commit c6627e18 authored by Dave Tapuska's avatar Dave Tapuska Committed by Commit Bot

Add experimental support for the hrefTranslate attribute.

This adds the plumbing that exposes the attribute dynamically. So that
it is only bound when a translate service is available. The translate
team will hook into the frame load request to grab the attribute
from the anchor.

Doc: https://github.com/dtapuska/html-translate
Intent to Implement: https://groups.google.com/a/chromium.org/forum/#!msg/blink-dev/hmASm1yhi5s/7vqmt7CwDgAJ

BUG=872777

Change-Id: I0004fe33afeaf3255031bc87f47c7f4d1209ffc8
Reviewed-on: https://chromium-review.googlesource.com/1175031
Commit-Queue: Dave Tapuska <dtapuska@chromium.org>
Reviewed-by: default avatarNasko Oskov <nasko@chromium.org>
Reviewed-by: default avataranthonyvd <anthonyvd@chromium.org>
Reviewed-by: default avatarRick Byers <rbyers@chromium.org>
Reviewed-by: default avatarTakashi Toyoshima <toyoshim@chromium.org>
Cr-Commit-Position: refs/heads/master@{#587580}
parent 42dbd1c4
...@@ -119,6 +119,7 @@ ...@@ -119,6 +119,7 @@
#include "chrome/browser/sync_file_system/local/sync_file_system_backend.h" #include "chrome/browser/sync_file_system/local/sync_file_system_backend.h"
#include "chrome/browser/tab_contents/tab_util.h" #include "chrome/browser/tab_contents/tab_util.h"
#include "chrome/browser/tracing/chrome_tracing_delegate.h" #include "chrome/browser/tracing/chrome_tracing_delegate.h"
#include "chrome/browser/translate/translate_service.h"
#include "chrome/browser/ui/blocked_content/blocked_window_params.h" #include "chrome/browser/ui/blocked_content/blocked_window_params.h"
#include "chrome/browser/ui/blocked_content/popup_blocker_tab_helper.h" #include "chrome/browser/ui/blocked_content/popup_blocker_tab_helper.h"
#include "chrome/browser/ui/blocked_content/tab_under_navigation_throttle.h" #include "chrome/browser/ui/blocked_content/tab_under_navigation_throttle.h"
...@@ -3202,6 +3203,7 @@ void ChromeContentBrowserClient::OverrideWebkitPrefs( ...@@ -3202,6 +3203,7 @@ void ChromeContentBrowserClient::OverrideWebkitPrefs(
} }
#endif // !defined(OS_ANDROID) #endif // !defined(OS_ANDROID)
web_prefs->translate_service_available = TranslateService::IsAvailable(prefs);
for (size_t i = 0; i < extra_parts_.size(); ++i) for (size_t i = 0; i < extra_parts_.size(); ++i)
extra_parts_[i]->OverrideWebkitPrefs(rvh, web_prefs); extra_parts_[i]->OverrideWebkitPrefs(rvh, web_prefs);
} }
......
...@@ -143,3 +143,8 @@ bool TranslateService::IsTranslatableURL(const GURL& url) { ...@@ -143,3 +143,8 @@ bool TranslateService::IsTranslatableURL(const GURL& url) {
#endif #endif
!url.SchemeIs(url::kFtpScheme); !url.SchemeIs(url::kFtpScheme);
} }
bool TranslateService::IsAvailable(PrefService* prefs) {
return translate::TranslateManager::IsAvailable(
ChromeTranslateClient::CreateTranslatePrefs(prefs).get());
}
...@@ -44,6 +44,9 @@ class TranslateService ...@@ -44,6 +44,9 @@ class TranslateService
// Returns true if the URL can be translated. // Returns true if the URL can be translated.
static bool IsTranslatableURL(const GURL& url); static bool IsTranslatableURL(const GURL& url);
// Returns true if the service is available and enabled by user preferences.
static bool IsAvailable(PrefService* prefs);
private: private:
TranslateService(); TranslateService();
~TranslateService() override; ~TranslateService() override;
......
...@@ -558,6 +558,15 @@ void TranslateManager::SetIgnoreMissingKeyForTesting(bool ignore) { ...@@ -558,6 +558,15 @@ void TranslateManager::SetIgnoreMissingKeyForTesting(bool ignore) {
ignore_missing_key_for_testing_ = ignore; ignore_missing_key_for_testing_ = ignore;
} }
// static
bool TranslateManager::IsAvailable(const TranslatePrefs* prefs) {
// These conditions mirror the conditions in InitiateTranslation.
return base::FeatureList::IsEnabled(translate::kTranslateUI) &&
(ignore_missing_key_for_testing_ ||
::google_apis::HasAPIKeyConfigured()) &&
prefs->IsOfferTranslateEnabled();
}
void TranslateManager::InitTranslateEvent(const std::string& src_lang, void TranslateManager::InitTranslateEvent(const std::string& src_lang,
const std::string& dst_lang, const std::string& dst_lang,
const TranslatePrefs& prefs) { const TranslatePrefs& prefs) {
......
...@@ -140,6 +140,12 @@ class TranslateManager { ...@@ -140,6 +140,12 @@ class TranslateManager {
// testing, set to true to offer anyway. // testing, set to true to offer anyway.
static void SetIgnoreMissingKeyForTesting(bool ignore); static void SetIgnoreMissingKeyForTesting(bool ignore);
// Returns true if the TranslateManager is available and enabled by user
// preferences. It is not available for builds without API keys.
// blink's hrefTranslate attribute existence relies on the result.
// See https://github.com/dtapuska/html-translate
static bool IsAvailable(const TranslatePrefs* prefs);
// Returns true if the decision should be overridden and logs the event // Returns true if the decision should be overridden and logs the event
// appropriately. |event_type| must be one of the // appropriately. |event_type| must be one of the
// values defined by metrics::TranslateEventProto::EventType. // values defined by metrics::TranslateEventProto::EventType.
......
...@@ -239,6 +239,7 @@ IPC_STRUCT_TRAITS_BEGIN(content::WebPreferences) ...@@ -239,6 +239,7 @@ IPC_STRUCT_TRAITS_BEGIN(content::WebPreferences)
IPC_STRUCT_TRAITS_MEMBER(autoplay_policy) IPC_STRUCT_TRAITS_MEMBER(autoplay_policy)
IPC_STRUCT_TRAITS_MEMBER(low_priority_iframes_threshold) IPC_STRUCT_TRAITS_MEMBER(low_priority_iframes_threshold)
IPC_STRUCT_TRAITS_MEMBER(picture_in_picture_enabled) IPC_STRUCT_TRAITS_MEMBER(picture_in_picture_enabled)
IPC_STRUCT_TRAITS_MEMBER(translate_service_available)
IPC_STRUCT_TRAITS_MEMBER(lazy_frame_loading_distance_thresholds_px) IPC_STRUCT_TRAITS_MEMBER(lazy_frame_loading_distance_thresholds_px)
IPC_STRUCT_TRAITS_END() IPC_STRUCT_TRAITS_END()
......
...@@ -236,7 +236,8 @@ WebPreferences::WebPreferences() ...@@ -236,7 +236,8 @@ WebPreferences::WebPreferences()
do_not_update_selection_on_mutating_selection_range(false), do_not_update_selection_on_mutating_selection_range(false),
autoplay_policy(AutoplayPolicy::kDocumentUserActivationRequired), autoplay_policy(AutoplayPolicy::kDocumentUserActivationRequired),
low_priority_iframes_threshold(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN), low_priority_iframes_threshold(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN),
picture_in_picture_enabled(true) { picture_in_picture_enabled(true),
translate_service_available(false) {
standard_font_family_map[kCommonScript] = standard_font_family_map[kCommonScript] =
base::ASCIIToUTF16("Times New Roman"); base::ASCIIToUTF16("Times New Roman");
fixed_font_family_map[kCommonScript] = base::ASCIIToUTF16("Courier New"); fixed_font_family_map[kCommonScript] = base::ASCIIToUTF16("Courier New");
......
...@@ -306,6 +306,11 @@ struct CONTENT_EXPORT WebPreferences { ...@@ -306,6 +306,11 @@ struct CONTENT_EXPORT WebPreferences {
// Whether Picture-in-Picture is enabled. // Whether Picture-in-Picture is enabled.
bool picture_in_picture_enabled; bool picture_in_picture_enabled;
// Whether a translate service is available.
// blink's hrefTranslate attribute existence relies on the result.
// See https://github.com/dtapuska/html-translate
bool translate_service_available;
// Specifies how close a lazily loaded iframe or image should be from the // Specifies how close a lazily loaded iframe or image should be from the
// viewport before it should start being loaded in, depending on the effective // viewport before it should start being loaded in, depending on the effective
// connection type of the current network. Blink will use the default distance // connection type of the current network. Blink will use the default distance
......
...@@ -997,6 +997,13 @@ void RenderView::ApplyWebPreferences(const WebPreferences& prefs, ...@@ -997,6 +997,13 @@ void RenderView::ApplyWebPreferences(const WebPreferences& prefs,
#if defined(OS_WIN) #if defined(OS_WIN)
WebRuntimeFeatures::EnableMiddleClickAutoscroll(true); WebRuntimeFeatures::EnableMiddleClickAutoscroll(true);
#endif #endif
// Only enable href translate if we have the experimental web platform
// flag on and the translation service is available.
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kEnableExperimentalWebPlatformFeatures)) {
WebRuntimeFeatures::EnableHrefTranslate(prefs.translate_service_available);
}
} }
/*static*/ /*static*/
......
...@@ -145,6 +145,7 @@ void ApplyLayoutTestDefaultPreferences(WebPreferences* prefs) { ...@@ -145,6 +145,7 @@ void ApplyLayoutTestDefaultPreferences(WebPreferences* prefs) {
prefs->default_maximum_page_scale_factor = 4.f; prefs->default_maximum_page_scale_factor = 4.f;
prefs->presentation_receiver = prefs->presentation_receiver =
command_line.HasSwitch(switches::kForcePresentationReceiverForTesting); command_line.HasSwitch(switches::kForcePresentationReceiverForTesting);
prefs->translate_service_available = true;
} }
base::FilePath GetWebKitRootDirFilePath() { base::FilePath GetWebKitRootDirFilePath() {
......
...@@ -308,6 +308,7 @@ html element a ...@@ -308,6 +308,7 @@ html element a
property host property host
property hostname property hostname
property href property href
property hrefTranslate
property hreflang property hreflang
property name property name
property origin property origin
......
...@@ -2254,6 +2254,7 @@ interface HTMLAnchorElement : HTMLElement ...@@ -2254,6 +2254,7 @@ interface HTMLAnchorElement : HTMLElement
getter host getter host
getter hostname getter hostname
getter href getter href
getter hrefTranslate
getter hreflang getter hreflang
getter name getter name
getter origin getter origin
...@@ -2281,6 +2282,7 @@ interface HTMLAnchorElement : HTMLElement ...@@ -2281,6 +2282,7 @@ interface HTMLAnchorElement : HTMLElement
setter host setter host
setter hostname setter hostname
setter href setter href
setter hrefTranslate
setter hreflang setter hreflang
setter name setter name
setter password setter password
......
...@@ -203,6 +203,7 @@ class WebRuntimeFeatures { ...@@ -203,6 +203,7 @@ class WebRuntimeFeatures {
bool); bool);
BLINK_PLATFORM_EXPORT static void EnableAutoplayIgnoresWebAudio(bool); BLINK_PLATFORM_EXPORT static void EnableAutoplayIgnoresWebAudio(bool);
BLINK_PLATFORM_EXPORT static void EnableMediaControlsExpandGesture(bool); BLINK_PLATFORM_EXPORT static void EnableMediaControlsExpandGesture(bool);
BLINK_PLATFORM_EXPORT static void EnableHrefTranslate(bool);
private: private:
WebRuntimeFeatures(); WebRuntimeFeatures();
......
...@@ -397,6 +397,7 @@ void HTMLAnchorElement::HandleClick(Event& event) { ...@@ -397,6 +397,7 @@ void HTMLAnchorElement::HandleClick(Event& event) {
} }
if (HasRel(kRelationNoOpener)) if (HasRel(kRelationNoOpener))
frame_request.SetShouldSetOpener(kNeverSetOpener); frame_request.SetShouldSetOpener(kNeverSetOpener);
frame_request.SetHrefTranslate(FastGetAttribute(hreftranslateAttr));
frame_request.SetTriggeringEventInfo( frame_request.SetTriggeringEventInfo(
event.isTrusted() ? WebTriggeringEventInfo::kFromTrustedEvent event.isTrusted() ? WebTriggeringEventInfo::kFromTrustedEvent
: WebTriggeringEventInfo::kFromUntrustedEvent); : WebTriggeringEventInfo::kFromUntrustedEvent);
......
...@@ -27,6 +27,7 @@ interface HTMLAnchorElement : HTMLElement { ...@@ -27,6 +27,7 @@ interface HTMLAnchorElement : HTMLElement {
[CEReactions, Reflect] attribute DOMString rel; [CEReactions, Reflect] attribute DOMString rel;
[SameObject, PutForwards=value] readonly attribute DOMTokenList relList; [SameObject, PutForwards=value] readonly attribute DOMTokenList relList;
[CEReactions, Reflect] attribute DOMString hreflang; [CEReactions, Reflect] attribute DOMString hreflang;
[RuntimeEnabled=HrefTranslate, CEReactions, Reflect] attribute DOMString hrefTranslate;
[CEReactions, Reflect] attribute DOMString type; [CEReactions, Reflect] attribute DOMString type;
[CEReactions, Reflect, ReflectOnly=("","no-referrer","origin","no-referrer-when-downgrade","origin-when-cross-origin","unsafe-url"), ReflectMissing="", ReflectInvalid=""] attribute DOMString referrerPolicy; [CEReactions, Reflect, ReflectOnly=("","no-referrer","origin","no-referrer-when-downgrade","origin-when-cross-origin","unsafe-url"), ReflectMissing="", ReflectInvalid=""] attribute DOMString referrerPolicy;
......
...@@ -92,6 +92,7 @@ ...@@ -92,6 +92,7 @@
"high", "high",
"href", "href",
"hreflang", "hreflang",
"hreftranslate",
"hspace", "hspace",
"http-equiv", "http-equiv",
"id", "id",
......
...@@ -109,6 +109,11 @@ struct CORE_EXPORT FrameLoadRequest { ...@@ -109,6 +109,11 @@ struct CORE_EXPORT FrameLoadRequest {
should_set_opener_ = should_set_opener; should_set_opener_ = should_set_opener;
} }
const AtomicString& HrefTranslate() { return href_translate_; }
void SetHrefTranslate(const AtomicString& translate) {
href_translate_ = translate;
}
ContentSecurityPolicyDisposition ShouldCheckMainWorldContentSecurityPolicy() ContentSecurityPolicyDisposition ShouldCheckMainWorldContentSecurityPolicy()
const { const {
return should_check_main_world_content_security_policy_; return should_check_main_world_content_security_policy_;
...@@ -160,6 +165,7 @@ struct CORE_EXPORT FrameLoadRequest { ...@@ -160,6 +165,7 @@ struct CORE_EXPORT FrameLoadRequest {
Member<Document> origin_document_; Member<Document> origin_document_;
ResourceRequest resource_request_; ResourceRequest resource_request_;
AtomicString frame_name_; AtomicString frame_name_;
AtomicString href_translate_;
SubstituteData substitute_data_; SubstituteData substitute_data_;
bool replaces_current_item_; bool replaces_current_item_;
ClientRedirectPolicy client_redirect_; ClientRedirectPolicy client_redirect_;
......
...@@ -568,4 +568,8 @@ void WebRuntimeFeatures::EnableMediaControlsExpandGesture(bool enable) { ...@@ -568,4 +568,8 @@ void WebRuntimeFeatures::EnableMediaControlsExpandGesture(bool enable) {
RuntimeEnabledFeatures::SetMediaControlsExpandGestureEnabled(enable); RuntimeEnabledFeatures::SetMediaControlsExpandGestureEnabled(enable);
} }
void WebRuntimeFeatures::EnableHrefTranslate(bool enable) {
RuntimeEnabledFeatures::SetHrefTranslateEnabled(enable);
}
} // namespace blink } // namespace blink
...@@ -540,6 +540,11 @@ ...@@ -540,6 +540,11 @@
{ {
name: "HeapUnifiedGarbageCollection", name: "HeapUnifiedGarbageCollection",
}, },
// Only exposed when a translation engine is available
// and experimental features are enabled.
{
name: "HrefTranslate",
},
// https://crbug.com/766694 for testing disabling the feature. // https://crbug.com/766694 for testing disabling the feature.
{ {
name: "HTMLImports", name: "HTMLImports",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment