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