Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/OriginTrialContext.h" |
|
iclelland
2017/01/26 20:14:11
I think we can remove this one now
chasej
2017/01/27 17:22:36
Done.
| |
| 15 #include "core/origin_trials/OriginTrials.h" | |
| 15 | 16 |
| 16 namespace blink { | 17 namespace blink { |
| 17 | 18 |
| 18 void installConditionalFeaturesCore(const WrapperTypeInfo* wrapperTypeInfo, | 19 void installConditionalFeaturesCore(const WrapperTypeInfo* wrapperTypeInfo, |
| 19 const ScriptState* scriptState, | 20 const ScriptState* scriptState, |
| 20 v8::Local<v8::Object> prototypeObject, | 21 v8::Local<v8::Object> prototypeObject, |
| 21 v8::Local<v8::Function> interfaceObject) { | 22 v8::Local<v8::Function> interfaceObject) { |
| 22 // TODO(iclelland): Generate all of this logic at compile-time, based on the | 23 // TODO(iclelland): Generate all of this logic at compile-time, based on the |
| 23 // configuration of origin trial enabled attributes and interfaces in IDL | 24 // configuration of origin trial enabled attributes and interfaces in IDL |
| 24 // files. (crbug.com/615060) | 25 // files. (crbug.com/615060) |
| 25 ExecutionContext* executionContext = scriptState->getExecutionContext(); | 26 ExecutionContext* executionContext = scriptState->getExecutionContext(); |
| 26 if (!executionContext) | 27 if (!executionContext) |
| 27 return; | 28 return; |
| 28 OriginTrialContext* originTrialContext = OriginTrialContext::from( | |
| 29 executionContext, OriginTrialContext::DontCreateIfNotExists); | |
| 30 v8::Isolate* isolate = scriptState->isolate(); | 29 v8::Isolate* isolate = scriptState->isolate(); |
| 31 const DOMWrapperWorld& world = scriptState->world(); | 30 const DOMWrapperWorld& world = scriptState->world(); |
| 32 if (wrapperTypeInfo == &V8HTMLLinkElement::wrapperTypeInfo) { | 31 if (wrapperTypeInfo == &V8HTMLLinkElement::wrapperTypeInfo) { |
| 33 if (RuntimeEnabledFeatures::linkServiceWorkerEnabled() || | 32 if (OriginTrials::linkServiceWorkerEnabled(executionContext)) { |
| 34 (originTrialContext && | |
| 35 originTrialContext->isTrialEnabled("ForeignFetch"))) { | |
| 36 V8HTMLLinkElement::installLinkServiceWorker( | 33 V8HTMLLinkElement::installLinkServiceWorker( |
| 37 isolate, world, v8::Local<v8::Object>(), prototypeObject, | 34 isolate, world, v8::Local<v8::Object>(), prototypeObject, |
| 38 interfaceObject); | 35 interfaceObject); |
| 39 } | 36 } |
| 40 } else if (wrapperTypeInfo == &V8Window::wrapperTypeInfo) { | 37 } else if (wrapperTypeInfo == &V8Window::wrapperTypeInfo) { |
| 41 v8::Local<v8::Object> instanceObject = scriptState->context()->Global(); | 38 v8::Local<v8::Object> instanceObject = scriptState->context()->Global(); |
| 42 if (RuntimeEnabledFeatures::longTaskObserverEnabled() || | 39 if (OriginTrials::longTaskObserverEnabled(executionContext)) { |
| 43 (originTrialContext && | |
| 44 originTrialContext->isTrialEnabled("LongTaskObserver"))) { | |
| 45 V8Window::installLongTaskObserver(isolate, world, instanceObject, | 40 V8Window::installLongTaskObserver(isolate, world, instanceObject, |
| 46 prototypeObject, interfaceObject); | 41 prototypeObject, interfaceObject); |
| 47 } | 42 } |
| 48 } else if (wrapperTypeInfo == &V8Document::wrapperTypeInfo) { | 43 } else if (wrapperTypeInfo == &V8Document::wrapperTypeInfo) { |
| 49 if (RuntimeEnabledFeatures::setRootScrollerEnabled() || | 44 if (OriginTrials::setRootScrollerEnabled(executionContext)) { |
| 50 (originTrialContext && | |
| 51 originTrialContext->isTrialEnabled("RootScroller"))) { | |
| 52 V8Document::installRootScroller(isolate, world, v8::Local<v8::Object>(), | 45 V8Document::installRootScroller(isolate, world, v8::Local<v8::Object>(), |
| 53 prototypeObject, interfaceObject); | 46 prototypeObject, interfaceObject); |
| 54 } | 47 } |
| 55 } | 48 } |
| 56 } | 49 } |
| 57 | 50 |
| 51 void installPendingConditionalFeatureCore(const String& feature, | |
| 52 const ScriptState* scriptState) { | |
| 53 // TODO(iclelland): Generate all of this logic at compile-time, based on the | |
| 54 // configuration of origin trial enabled attributes and interfaces in IDL | |
| 55 // files. (crbug.com/615060) | |
| 56 v8::Local<v8::Object> prototypeObject; | |
| 57 v8::Local<v8::Function> interfaceObject; | |
| 58 v8::Isolate* isolate = scriptState->isolate(); | |
| 59 const DOMWrapperWorld& world = scriptState->world(); | |
| 60 if (feature == "ForeignFetch") { | |
| 61 if (getExistingConstructorAndPrototypeForType( | |
| 62 &V8HTMLLinkElement::wrapperTypeInfo, scriptState, &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 (getExistingConstructorAndPrototypeForType(&V8Document::wrapperTypeInfo, | |
| 79 scriptState, &prototypeObject, | |
| 80 &interfaceObject)) { | |
| 81 V8Document::installRootScroller(isolate, world, v8::Local<v8::Object>(), | |
| 82 prototypeObject, interfaceObject); | |
| 83 } | |
| 84 return; | |
| 85 } | |
| 86 } | |
| 87 | |
| 58 namespace { | 88 namespace { |
| 59 | 89 |
| 60 InstallConditionalFeaturesFunction s_installConditionalFeaturesFunction = | 90 InstallConditionalFeaturesFunction s_installConditionalFeaturesFunction = |
| 61 &installConditionalFeaturesCore; | 91 &installConditionalFeaturesCore; |
| 92 | |
| 93 InstallPendingConditionalFeatureFunction | |
| 94 s_installPendingConditionalFeatureFunction = | |
| 95 &installPendingConditionalFeatureCore; | |
| 62 } | 96 } |
| 63 | 97 |
| 64 InstallConditionalFeaturesFunction setInstallConditionalFeaturesFunction( | 98 InstallConditionalFeaturesFunction setInstallConditionalFeaturesFunction( |
| 65 InstallConditionalFeaturesFunction newInstallConditionalFeaturesFunction) { | 99 InstallConditionalFeaturesFunction newInstallConditionalFeaturesFunction) { |
| 66 InstallConditionalFeaturesFunction originalFunction = | 100 InstallConditionalFeaturesFunction originalFunction = |
| 67 s_installConditionalFeaturesFunction; | 101 s_installConditionalFeaturesFunction; |
| 68 s_installConditionalFeaturesFunction = newInstallConditionalFeaturesFunction; | 102 s_installConditionalFeaturesFunction = newInstallConditionalFeaturesFunction; |
| 69 return originalFunction; | 103 return originalFunction; |
| 70 } | 104 } |
| 71 | 105 |
| 106 InstallPendingConditionalFeatureFunction | |
| 107 setInstallPendingConditionalFeatureFunction( | |
| 108 InstallPendingConditionalFeatureFunction | |
| 109 newInstallPendingConditionalFeatureFunction) { | |
| 110 InstallPendingConditionalFeatureFunction originalFunction = | |
| 111 s_installPendingConditionalFeatureFunction; | |
| 112 s_installPendingConditionalFeatureFunction = | |
| 113 newInstallPendingConditionalFeatureFunction; | |
| 114 return originalFunction; | |
| 115 } | |
| 116 | |
| 72 void installConditionalFeatures(const WrapperTypeInfo* type, | 117 void installConditionalFeatures(const WrapperTypeInfo* type, |
| 73 const ScriptState* scriptState, | 118 const ScriptState* scriptState, |
| 74 v8::Local<v8::Object> prototypeObject, | 119 v8::Local<v8::Object> prototypeObject, |
| 75 v8::Local<v8::Function> interfaceObject) { | 120 v8::Local<v8::Function> interfaceObject) { |
| 76 (*s_installConditionalFeaturesFunction)(type, scriptState, prototypeObject, | 121 (*s_installConditionalFeaturesFunction)(type, scriptState, prototypeObject, |
| 77 interfaceObject); | 122 interfaceObject); |
| 78 } | 123 } |
| 79 | 124 |
| 80 void installPendingConditionalFeaturesOnWindow(const ScriptState* scriptState) { | 125 void installPendingConditionalFeaturesOnWindow(const ScriptState* scriptState) { |
| 81 DCHECK(scriptState); | 126 DCHECK(scriptState); |
| 82 DCHECK(scriptState->context() == scriptState->isolate()->GetCurrentContext()); | 127 DCHECK(scriptState->context() == scriptState->isolate()->GetCurrentContext()); |
| 83 DCHECK(scriptState->perContextData()); | 128 DCHECK(scriptState->perContextData()); |
| 84 DCHECK(scriptState->world().isMainWorld()); | 129 DCHECK(scriptState->world().isMainWorld()); |
| 85 (*s_installConditionalFeaturesFunction)(&V8Window::wrapperTypeInfo, | 130 (*s_installConditionalFeaturesFunction)(&V8Window::wrapperTypeInfo, |
| 86 scriptState, v8::Local<v8::Object>(), | 131 scriptState, v8::Local<v8::Object>(), |
| 87 v8::Local<v8::Function>()); | 132 v8::Local<v8::Function>()); |
| 88 } | 133 } |
| 89 | 134 |
| 135 void installPendingConditionalFeature(const String& feature, | |
| 136 const ScriptState* scriptState) { | |
| 137 DCHECK(scriptState); | |
| 138 DCHECK(scriptState->context() == scriptState->isolate()->GetCurrentContext()); | |
| 139 DCHECK(scriptState->perContextData()); | |
| 140 DCHECK(scriptState->world().isMainWorld()); | |
| 141 | |
| 142 (*s_installPendingConditionalFeatureFunction)(feature, scriptState); | |
| 143 } | |
| 144 | |
| 145 bool getExistingConstructorAndPrototypeForType( | |
| 146 const WrapperTypeInfo* wrapperTypeInfo, | |
| 147 const ScriptState* scriptState, | |
| 148 v8::Local<v8::Object>* prototypeObject, | |
| 149 v8::Local<v8::Function>* interfaceObject) { | |
| 150 V8PerContextData* contextData = scriptState->perContextData(); | |
| 151 *interfaceObject = contextData->constructorForTypeFromCache(wrapperTypeInfo); | |
| 152 if (interfaceObject->IsEmpty()) { | |
| 153 *prototypeObject = v8::Local<v8::Object>(); | |
| 154 return false; | |
| 155 } | |
| 156 *prototypeObject = contextData->prototypeForType(wrapperTypeInfo); | |
| 157 return true; | |
| 158 } | |
| 159 | |
| 90 bool isFeatureEnabledInFrame(const FeaturePolicy::Feature& feature, | 160 bool isFeatureEnabledInFrame(const FeaturePolicy::Feature& feature, |
| 91 const LocalFrame* frame) { | 161 const LocalFrame* frame) { |
| 92 // If there is no frame, or if feature policy is disabled, use defaults. | 162 // If there is no frame, or if feature policy is disabled, use defaults. |
| 93 bool enabledByDefault = | 163 bool enabledByDefault = |
| 94 (feature.defaultPolicy == FeaturePolicy::FeatureDefault::EnableForAll || | 164 (feature.defaultPolicy == FeaturePolicy::FeatureDefault::EnableForAll || |
| 95 (feature.defaultPolicy == FeaturePolicy::FeatureDefault::EnableForSelf && | 165 (feature.defaultPolicy == FeaturePolicy::FeatureDefault::EnableForSelf && |
| 96 !frame->isCrossOriginSubframe())); | 166 !frame->isCrossOriginSubframe())); |
| 97 if (!RuntimeEnabledFeatures::featurePolicyEnabled() || !frame) | 167 if (!RuntimeEnabledFeatures::featurePolicyEnabled() || !frame) |
| 98 return enabledByDefault; | 168 return enabledByDefault; |
| 99 FeaturePolicy* featurePolicy = frame->securityContext()->getFeaturePolicy(); | 169 FeaturePolicy* featurePolicy = frame->securityContext()->getFeaturePolicy(); |
| 100 // The policy should always be initialized before checking it to ensure we | 170 // The policy should always be initialized before checking it to ensure we |
| 101 // properly inherit the parent policy. | 171 // properly inherit the parent policy. |
| 102 DCHECK(featurePolicy); | 172 DCHECK(featurePolicy); |
| 103 | 173 |
| 104 // Otherwise, check policy. | 174 // Otherwise, check policy. |
| 105 return featurePolicy->isFeatureEnabled(feature); | 175 return featurePolicy->isFeatureEnabled(feature); |
| 106 } | 176 } |
| 107 | 177 |
| 108 } // namespace blink | 178 } // namespace blink |
| OLD | NEW |