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 @@
#include "chrome/browser/sync_file_system/local/sync_file_system_backend.h"
#include "chrome/browser/tab_contents/tab_util.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/popup_blocker_tab_helper.h"
#include "chrome/browser/ui/blocked_content/tab_under_navigation_throttle.h"
......@@ -3202,6 +3203,7 @@ void ChromeContentBrowserClient::OverrideWebkitPrefs(
}
#endif // !defined(OS_ANDROID)
web_prefs->translate_service_available = TranslateService::IsAvailable(prefs);
for (size_t i = 0; i < extra_parts_.size(); ++i)
extra_parts_[i]->OverrideWebkitPrefs(rvh, web_prefs);
}
......
......@@ -143,3 +143,8 @@ bool TranslateService::IsTranslatableURL(const GURL& url) {
#endif
!url.SchemeIs(url::kFtpScheme);
}
bool TranslateService::IsAvailable(PrefService* prefs) {
return translate::TranslateManager::IsAvailable(
ChromeTranslateClient::CreateTranslatePrefs(prefs).get());
}
......@@ -44,6 +44,9 @@ class TranslateService
// Returns true if the URL can be translated.
static bool IsTranslatableURL(const GURL& url);
// Returns true if the service is available and enabled by user preferences.
static bool IsAvailable(PrefService* prefs);
private:
TranslateService();
~TranslateService() override;
......
......@@ -558,6 +558,15 @@ void TranslateManager::SetIgnoreMissingKeyForTesting(bool 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,
const std::string& dst_lang,
const TranslatePrefs& prefs) {
......
......@@ -140,6 +140,12 @@ class TranslateManager {
// testing, set to true to offer anyway.
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
// appropriately. |event_type| must be one of the
// values defined by metrics::TranslateEventProto::EventType.
......
......@@ -239,6 +239,7 @@ IPC_STRUCT_TRAITS_BEGIN(content::WebPreferences)
IPC_STRUCT_TRAITS_MEMBER(autoplay_policy)
IPC_STRUCT_TRAITS_MEMBER(low_priority_iframes_threshold)
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_END()
......
......@@ -236,7 +236,8 @@ WebPreferences::WebPreferences()
do_not_update_selection_on_mutating_selection_range(false),
autoplay_policy(AutoplayPolicy::kDocumentUserActivationRequired),
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] =
base::ASCIIToUTF16("Times New Roman");
fixed_font_family_map[kCommonScript] = base::ASCIIToUTF16("Courier New");
......
......@@ -306,6 +306,11 @@ struct CONTENT_EXPORT WebPreferences {
// Whether Picture-in-Picture is 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
// viewport before it should start being loaded in, depending on the effective
// connection type of the current network. Blink will use the default distance
......
......@@ -997,6 +997,13 @@ void RenderView::ApplyWebPreferences(const WebPreferences& prefs,
#if defined(OS_WIN)
WebRuntimeFeatures::EnableMiddleClickAutoscroll(true);
#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*/
......
......@@ -145,6 +145,7 @@ void ApplyLayoutTestDefaultPreferences(WebPreferences* prefs) {
prefs->default_maximum_page_scale_factor = 4.f;
prefs->presentation_receiver =
command_line.HasSwitch(switches::kForcePresentationReceiverForTesting);
prefs->translate_service_available = true;
}
base::FilePath GetWebKitRootDirFilePath() {
......
......@@ -308,6 +308,7 @@ html element a
property host
property hostname
property href
property hrefTranslate
property hreflang
property name
property origin
......
......@@ -2254,6 +2254,7 @@ interface HTMLAnchorElement : HTMLElement
getter host
getter hostname
getter href
getter hrefTranslate
getter hreflang
getter name
getter origin
......@@ -2281,6 +2282,7 @@ interface HTMLAnchorElement : HTMLElement
setter host
setter hostname
setter href
setter hrefTranslate
setter hreflang
setter name
setter password
......
......@@ -203,6 +203,7 @@ class WebRuntimeFeatures {
bool);
BLINK_PLATFORM_EXPORT static void EnableAutoplayIgnoresWebAudio(bool);
BLINK_PLATFORM_EXPORT static void EnableMediaControlsExpandGesture(bool);
BLINK_PLATFORM_EXPORT static void EnableHrefTranslate(bool);
private:
WebRuntimeFeatures();
......
......@@ -397,6 +397,7 @@ void HTMLAnchorElement::HandleClick(Event& event) {
}
if (HasRel(kRelationNoOpener))
frame_request.SetShouldSetOpener(kNeverSetOpener);
frame_request.SetHrefTranslate(FastGetAttribute(hreftranslateAttr));
frame_request.SetTriggeringEventInfo(
event.isTrusted() ? WebTriggeringEventInfo::kFromTrustedEvent
: WebTriggeringEventInfo::kFromUntrustedEvent);
......
......@@ -27,6 +27,7 @@ interface HTMLAnchorElement : HTMLElement {
[CEReactions, Reflect] attribute DOMString rel;
[SameObject, PutForwards=value] readonly attribute DOMTokenList relList;
[CEReactions, Reflect] attribute DOMString hreflang;
[RuntimeEnabled=HrefTranslate, CEReactions, Reflect] attribute DOMString hrefTranslate;
[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;
......
......@@ -92,6 +92,7 @@
"high",
"href",
"hreflang",
"hreftranslate",
"hspace",
"http-equiv",
"id",
......
......@@ -109,6 +109,11 @@ struct CORE_EXPORT FrameLoadRequest {
should_set_opener_ = should_set_opener;
}
const AtomicString& HrefTranslate() { return href_translate_; }
void SetHrefTranslate(const AtomicString& translate) {
href_translate_ = translate;
}
ContentSecurityPolicyDisposition ShouldCheckMainWorldContentSecurityPolicy()
const {
return should_check_main_world_content_security_policy_;
......@@ -160,6 +165,7 @@ struct CORE_EXPORT FrameLoadRequest {
Member<Document> origin_document_;
ResourceRequest resource_request_;
AtomicString frame_name_;
AtomicString href_translate_;
SubstituteData substitute_data_;
bool replaces_current_item_;
ClientRedirectPolicy client_redirect_;
......
......@@ -568,4 +568,8 @@ void WebRuntimeFeatures::EnableMediaControlsExpandGesture(bool enable) {
RuntimeEnabledFeatures::SetMediaControlsExpandGestureEnabled(enable);
}
void WebRuntimeFeatures::EnableHrefTranslate(bool enable) {
RuntimeEnabledFeatures::SetHrefTranslateEnabled(enable);
}
} // namespace blink
......@@ -540,6 +540,11 @@
{
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.
{
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