Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(699)

Side by Side Diff: third_party/WebKit/Source/bindings/core/v8/ConditionalFeatures.cpp

Issue 2640823004: Allow origin trials to be enabled by script (Closed)
Patch Set: Address nit Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "bindings/core/v8/ConditionalFeatures.h" 5 #include "bindings/core/v8/ConditionalFeatures.h"
6 6
7 #include "bindings/core/v8/ScriptState.h" 7 #include "bindings/core/v8/ScriptState.h"
8 #include "bindings/core/v8/V8Document.h" 8 #include "bindings/core/v8/V8Document.h"
9 #include "bindings/core/v8/V8HTMLLinkElement.h" 9 #include "bindings/core/v8/V8HTMLLinkElement.h"
10 #include "bindings/core/v8/V8Navigator.h" 10 #include "bindings/core/v8/V8Navigator.h"
11 #include "bindings/core/v8/V8Window.h" 11 #include "bindings/core/v8/V8Window.h"
12 #include "core/dom/ExecutionContext.h" 12 #include "core/dom/ExecutionContext.h"
13 #include "core/frame/LocalFrame.h" 13 #include "core/frame/LocalFrame.h"
14 #include "core/origin_trials/OriginTrialContext.h" 14 #include "core/origin_trials/OriginTrials.h"
15 15
16 namespace blink { 16 namespace blink {
17 17
18 void installConditionalFeaturesCore(const WrapperTypeInfo* wrapperTypeInfo, 18 void installConditionalFeaturesCore(const WrapperTypeInfo* wrapperTypeInfo,
19 const ScriptState* scriptState, 19 const ScriptState* scriptState,
20 v8::Local<v8::Object> prototypeObject, 20 v8::Local<v8::Object> prototypeObject,
21 v8::Local<v8::Function> interfaceObject) { 21 v8::Local<v8::Function> interfaceObject) {
22 // TODO(iclelland): Generate all of this logic at compile-time, based on the 22 // TODO(iclelland): Generate all of this logic at compile-time, based on the
23 // configuration of origin trial enabled attributes and interfaces in IDL 23 // configuration of origin trial enabled attributes and interfaces in IDL
24 // files. (crbug.com/615060) 24 // files. (crbug.com/615060)
25 ExecutionContext* executionContext = scriptState->getExecutionContext(); 25 ExecutionContext* executionContext = scriptState->getExecutionContext();
26 if (!executionContext) 26 if (!executionContext)
27 return; 27 return;
28 OriginTrialContext* originTrialContext = OriginTrialContext::from(
29 executionContext, OriginTrialContext::DontCreateIfNotExists);
30 v8::Isolate* isolate = scriptState->isolate(); 28 v8::Isolate* isolate = scriptState->isolate();
31 const DOMWrapperWorld& world = scriptState->world(); 29 const DOMWrapperWorld& world = scriptState->world();
32 if (wrapperTypeInfo == &V8HTMLLinkElement::wrapperTypeInfo) { 30 if (wrapperTypeInfo == &V8HTMLLinkElement::wrapperTypeInfo) {
33 if (RuntimeEnabledFeatures::linkServiceWorkerEnabled() || 31 if (OriginTrials::linkServiceWorkerEnabled(executionContext)) {
34 (originTrialContext &&
35 originTrialContext->isTrialEnabled("ForeignFetch"))) {
36 V8HTMLLinkElement::installLinkServiceWorker( 32 V8HTMLLinkElement::installLinkServiceWorker(
37 isolate, world, v8::Local<v8::Object>(), prototypeObject, 33 isolate, world, v8::Local<v8::Object>(), prototypeObject,
38 interfaceObject); 34 interfaceObject);
39 } 35 }
40 } else if (wrapperTypeInfo == &V8Window::wrapperTypeInfo) { 36 } else if (wrapperTypeInfo == &V8Window::wrapperTypeInfo) {
41 v8::Local<v8::Object> instanceObject = scriptState->context()->Global(); 37 v8::Local<v8::Object> instanceObject = scriptState->context()->Global();
42 if (RuntimeEnabledFeatures::longTaskObserverEnabled() || 38 if (OriginTrials::longTaskObserverEnabled(executionContext)) {
43 (originTrialContext &&
44 originTrialContext->isTrialEnabled("LongTaskObserver"))) {
45 V8Window::installLongTaskObserver(isolate, world, instanceObject, 39 V8Window::installLongTaskObserver(isolate, world, instanceObject,
46 prototypeObject, interfaceObject); 40 prototypeObject, interfaceObject);
47 } 41 }
48 } else if (wrapperTypeInfo == &V8Document::wrapperTypeInfo) { 42 } else if (wrapperTypeInfo == &V8Document::wrapperTypeInfo) {
49 if (RuntimeEnabledFeatures::setRootScrollerEnabled() || 43 if (OriginTrials::setRootScrollerEnabled(executionContext)) {
50 (originTrialContext &&
51 originTrialContext->isTrialEnabled("RootScroller"))) {
52 V8Document::installRootScroller(isolate, world, v8::Local<v8::Object>(), 44 V8Document::installRootScroller(isolate, world, v8::Local<v8::Object>(),
53 prototypeObject, interfaceObject); 45 prototypeObject, interfaceObject);
54 } 46 }
55 } 47 }
56 } 48 }
57 49
50 void installPendingConditionalFeatureCore(const String& feature,
51 const ScriptState* scriptState) {
52 // TODO(iclelland): Generate all of this logic at compile-time, based on the
53 // configuration of origin trial enabled attributes and interfaces in IDL
54 // files. (crbug.com/615060)
55 v8::Local<v8::Object> prototypeObject;
56 v8::Local<v8::Function> interfaceObject;
57 v8::Isolate* isolate = scriptState->isolate();
58 const DOMWrapperWorld& world = scriptState->world();
59 V8PerContextData* contextData = scriptState->perContextData();
60 if (feature == "ForeignFetch") {
61 if (contextData->getExistingConstructorAndPrototypeForType(
62 &V8HTMLLinkElement::wrapperTypeInfo, &prototypeObject,
63 &interfaceObject)) {
64 V8HTMLLinkElement::installLinkServiceWorker(
65 isolate, world, v8::Local<v8::Object>(), prototypeObject,
66 interfaceObject);
67 }
68 return;
69 }
70 if (feature == "LongTaskObserver") {
71 v8::Local<v8::Object> instanceObject = scriptState->context()->Global();
72 V8Window::installLongTaskObserver(isolate, world, instanceObject,
73 v8::Local<v8::Object>(),
74 v8::Local<v8::Function>());
75 return;
76 }
77 if (feature == "RootScroller") {
78 if (contextData->getExistingConstructorAndPrototypeForType(
79 &V8Document::wrapperTypeInfo, &prototypeObject, &interfaceObject)) {
80 V8Document::installRootScroller(isolate, world, v8::Local<v8::Object>(),
81 prototypeObject, interfaceObject);
82 }
83 return;
84 }
85 }
86
58 namespace { 87 namespace {
59 88
60 InstallConditionalFeaturesFunction s_installConditionalFeaturesFunction = 89 InstallConditionalFeaturesFunction s_installConditionalFeaturesFunction =
61 &installConditionalFeaturesCore; 90 &installConditionalFeaturesCore;
91
92 InstallPendingConditionalFeatureFunction
93 s_installPendingConditionalFeatureFunction =
94 &installPendingConditionalFeatureCore;
62 } 95 }
63 96
64 InstallConditionalFeaturesFunction setInstallConditionalFeaturesFunction( 97 InstallConditionalFeaturesFunction setInstallConditionalFeaturesFunction(
65 InstallConditionalFeaturesFunction newInstallConditionalFeaturesFunction) { 98 InstallConditionalFeaturesFunction newInstallConditionalFeaturesFunction) {
66 InstallConditionalFeaturesFunction originalFunction = 99 InstallConditionalFeaturesFunction originalFunction =
67 s_installConditionalFeaturesFunction; 100 s_installConditionalFeaturesFunction;
68 s_installConditionalFeaturesFunction = newInstallConditionalFeaturesFunction; 101 s_installConditionalFeaturesFunction = newInstallConditionalFeaturesFunction;
69 return originalFunction; 102 return originalFunction;
70 } 103 }
71 104
105 InstallPendingConditionalFeatureFunction
106 setInstallPendingConditionalFeatureFunction(
107 InstallPendingConditionalFeatureFunction
108 newInstallPendingConditionalFeatureFunction) {
109 InstallPendingConditionalFeatureFunction originalFunction =
110 s_installPendingConditionalFeatureFunction;
111 s_installPendingConditionalFeatureFunction =
112 newInstallPendingConditionalFeatureFunction;
113 return originalFunction;
114 }
115
72 void installConditionalFeatures(const WrapperTypeInfo* type, 116 void installConditionalFeatures(const WrapperTypeInfo* type,
73 const ScriptState* scriptState, 117 const ScriptState* scriptState,
74 v8::Local<v8::Object> prototypeObject, 118 v8::Local<v8::Object> prototypeObject,
75 v8::Local<v8::Function> interfaceObject) { 119 v8::Local<v8::Function> interfaceObject) {
76 (*s_installConditionalFeaturesFunction)(type, scriptState, prototypeObject, 120 (*s_installConditionalFeaturesFunction)(type, scriptState, prototypeObject,
77 interfaceObject); 121 interfaceObject);
78 } 122 }
79 123
80 void installPendingConditionalFeaturesOnWindow(const ScriptState* scriptState) { 124 void installConditionalFeaturesOnWindow(const ScriptState* scriptState) {
81 DCHECK(scriptState); 125 DCHECK(scriptState);
82 DCHECK(scriptState->context() == scriptState->isolate()->GetCurrentContext()); 126 DCHECK(scriptState->context() == scriptState->isolate()->GetCurrentContext());
83 DCHECK(scriptState->perContextData()); 127 DCHECK(scriptState->perContextData());
84 DCHECK(scriptState->world().isMainWorld()); 128 DCHECK(scriptState->world().isMainWorld());
85 (*s_installConditionalFeaturesFunction)(&V8Window::wrapperTypeInfo, 129 (*s_installConditionalFeaturesFunction)(&V8Window::wrapperTypeInfo,
86 scriptState, v8::Local<v8::Object>(), 130 scriptState, v8::Local<v8::Object>(),
87 v8::Local<v8::Function>()); 131 v8::Local<v8::Function>());
88 } 132 }
89 133
134 void installPendingConditionalFeature(const String& feature,
135 const ScriptState* scriptState) {
136 DCHECK(scriptState);
137 DCHECK(scriptState->context() == scriptState->isolate()->GetCurrentContext());
138 DCHECK(scriptState->perContextData());
139 DCHECK(scriptState->world().isMainWorld());
140
141 (*s_installPendingConditionalFeatureFunction)(feature, scriptState);
142 }
143
90 bool isFeatureEnabledInFrame(const FeaturePolicy::Feature& feature, 144 bool isFeatureEnabledInFrame(const FeaturePolicy::Feature& feature,
91 const LocalFrame* frame) { 145 const LocalFrame* frame) {
92 // If there is no frame, or if feature policy is disabled, use defaults. 146 // If there is no frame, or if feature policy is disabled, use defaults.
93 bool enabledByDefault = 147 bool enabledByDefault =
94 (feature.defaultPolicy == FeaturePolicy::FeatureDefault::EnableForAll || 148 (feature.defaultPolicy == FeaturePolicy::FeatureDefault::EnableForAll ||
95 (feature.defaultPolicy == FeaturePolicy::FeatureDefault::EnableForSelf && 149 (feature.defaultPolicy == FeaturePolicy::FeatureDefault::EnableForSelf &&
96 !frame->isCrossOriginSubframe())); 150 !frame->isCrossOriginSubframe()));
97 if (!RuntimeEnabledFeatures::featurePolicyEnabled() || !frame) 151 if (!RuntimeEnabledFeatures::featurePolicyEnabled() || !frame)
98 return enabledByDefault; 152 return enabledByDefault;
99 FeaturePolicy* featurePolicy = frame->securityContext()->getFeaturePolicy(); 153 FeaturePolicy* featurePolicy = frame->securityContext()->getFeaturePolicy();
100 // The policy should always be initialized before checking it to ensure we 154 // The policy should always be initialized before checking it to ensure we
101 // properly inherit the parent policy. 155 // properly inherit the parent policy.
102 DCHECK(featurePolicy); 156 DCHECK(featurePolicy);
103 157
104 // Otherwise, check policy. 158 // Otherwise, check policy.
105 return featurePolicy->isFeatureEnabled(feature); 159 return featurePolicy->isFeatureEnabled(feature);
106 } 160 }
107 161
108 } // namespace blink 162 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698