1 /*
2  * Copyright (C) 2011 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include "third_party/blink/renderer/core/testing/v8/web_core_test_support.h"
27 
28 #include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_core.h"
29 #include "third_party/blink/renderer/bindings/core/v8/v8_origin_trials_test.h"
30 #include "third_party/blink/renderer/core/dom/document.h"
31 #include "third_party/blink/renderer/core/execution_context/execution_context.h"
32 #include "third_party/blink/renderer/core/frame/local_dom_window.h"
33 #include "third_party/blink/renderer/core/frame/local_frame.h"
34 #include "third_party/blink/renderer/core/testing/internal_settings.h"
35 #include "third_party/blink/renderer/core/testing/internals.h"
36 #include "third_party/blink/renderer/core/testing/worker_internals.h"
37 #include "third_party/blink/renderer/platform/bindings/dom_wrapper_world.h"
38 #include "third_party/blink/renderer/platform/bindings/origin_trial_features.h"
39 #include "third_party/blink/renderer/platform/bindings/v8_per_context_data.h"
40 #include "third_party/blink/renderer/platform/runtime_enabled_features.h"
41 
42 namespace blink {
43 
44 namespace web_core_test_support {
45 
46 namespace {
47 
48 InstallOriginTrialFeaturesFunction
49     s_original_install_origin_trial_features_function = nullptr;
50 InstallPendingOriginTrialFeatureFunction
51     s_original_install_pending_origin_trial_feature_function = nullptr;
52 
CreateInternalsObject(v8::Local<v8::Context> context)53 v8::Local<v8::Value> CreateInternalsObject(v8::Local<v8::Context> context) {
54   ScriptState* script_state = ScriptState::From(context);
55   v8::Local<v8::Object> global = script_state->GetContext()->Global();
56   ExecutionContext* execution_context = ExecutionContext::From(script_state);
57   if (execution_context->IsDocument()) {
58     return ToV8(MakeGarbageCollected<Internals>(execution_context), global,
59                 script_state->GetIsolate());
60   }
61   if (execution_context->IsWorkerGlobalScope()) {
62     return ToV8(MakeGarbageCollected<WorkerInternals>(), global,
63                 script_state->GetIsolate());
64   }
65   return v8::Local<v8::Value>();
66 }
67 
68 }  // namespace
69 
InjectInternalsObject(v8::Local<v8::Context> context)70 void InjectInternalsObject(v8::Local<v8::Context> context) {
71   RegisterInstallOriginTrialFeaturesForTesting();
72 
73   ScriptState* script_state = ScriptState::From(context);
74   ScriptState::Scope scope(script_state);
75   v8::Local<v8::Object> global = script_state->GetContext()->Global();
76   v8::Local<v8::Value> internals = CreateInternalsObject(context);
77   if (internals.IsEmpty())
78     return;
79 
80   global
81       ->CreateDataProperty(
82           script_state->GetContext(),
83           V8AtomicString(script_state->GetIsolate(), "internals"), internals)
84       .ToChecked();
85 }
86 
InstallOriginTrialFeaturesForTesting(const WrapperTypeInfo * type,const ScriptState * script_state,v8::Local<v8::Object> prototype_object,v8::Local<v8::Function> interface_object)87 void InstallOriginTrialFeaturesForTesting(
88     const WrapperTypeInfo* type,
89     const ScriptState* script_state,
90     v8::Local<v8::Object> prototype_object,
91     v8::Local<v8::Function> interface_object) {
92   (*s_original_install_origin_trial_features_function)(
93       type, script_state, prototype_object, interface_object);
94 
95   ExecutionContext* execution_context = ExecutionContext::From(script_state);
96 
97   if (type == V8OriginTrialsTest::GetWrapperTypeInfo()) {
98     if (RuntimeEnabledFeatures::OriginTrialsSampleAPIEnabled(
99             execution_context)) {
100       V8OriginTrialsTest::InstallOriginTrialsSampleAPI(
101           script_state->GetIsolate(), script_state->World(),
102           v8::Local<v8::Object>(), prototype_object, interface_object);
103     }
104     if (RuntimeEnabledFeatures::OriginTrialsSampleAPIDeprecationEnabled(
105             execution_context)) {
106       V8OriginTrialsTest::InstallOriginTrialsSampleAPIDeprecation(
107           script_state->GetIsolate(), script_state->World(),
108           v8::Local<v8::Object>(), prototype_object, interface_object);
109     }
110     if (RuntimeEnabledFeatures::OriginTrialsSampleAPIImpliedEnabled(
111             execution_context)) {
112       V8OriginTrialsTest::InstallOriginTrialsSampleAPIImplied(
113           script_state->GetIsolate(), script_state->World(),
114           v8::Local<v8::Object>(), prototype_object, interface_object);
115     }
116     if (RuntimeEnabledFeatures::OriginTrialsSampleAPINavigationEnabled(
117             execution_context)) {
118       V8OriginTrialsTest::InstallOriginTrialsSampleAPINavigation(
119           script_state->GetIsolate(), script_state->World(),
120           v8::Local<v8::Object>(), prototype_object, interface_object);
121     }
122   }
123 }
124 
ResetInternalsObject(v8::Local<v8::Context> context)125 void ResetInternalsObject(v8::Local<v8::Context> context) {
126   // This can happen if JavaScript is disabled in the main frame.
127   if (context.IsEmpty())
128     return;
129 
130   ScriptState* script_state = ScriptState::From(context);
131   ScriptState::Scope scope(script_state);
132   LocalFrame* frame = LocalDOMWindow::From(script_state)->GetFrame();
133   // Should the frame have been detached, the page is assumed being destroyed
134   // (=> no reset required.)
135   if (!frame)
136     return;
137   Page* page = frame->GetPage();
138   DCHECK(page);
139   Internals::ResetToConsistentState(page);
140   InternalSettings::From(*page)->ResetToConsistentState();
141 }
142 
InstallPendingOriginTrialFeatureForTesting(OriginTrialFeature feature,const ScriptState * script_state)143 void InstallPendingOriginTrialFeatureForTesting(
144     OriginTrialFeature feature,
145     const ScriptState* script_state) {
146   (*s_original_install_pending_origin_trial_feature_function)(feature,
147                                                               script_state);
148   v8::Local<v8::Object> prototype_object;
149   v8::Local<v8::Function> interface_object;
150   switch (feature) {
151     case OriginTrialFeature::kOriginTrialsSampleAPI: {
152       if (script_state->PerContextData()
153               ->GetExistingConstructorAndPrototypeForType(
154                   V8OriginTrialsTest::GetWrapperTypeInfo(), &prototype_object,
155                   &interface_object)) {
156         V8OriginTrialsTest::InstallOriginTrialsSampleAPI(
157             script_state->GetIsolate(), script_state->World(),
158             v8::Local<v8::Object>(), prototype_object, interface_object);
159       }
160       break;
161     }
162     case OriginTrialFeature::kOriginTrialsSampleAPIDeprecation: {
163       if (script_state->PerContextData()
164               ->GetExistingConstructorAndPrototypeForType(
165                   V8OriginTrialsTest::GetWrapperTypeInfo(), &prototype_object,
166                   &interface_object)) {
167         V8OriginTrialsTest::InstallOriginTrialsSampleAPIDeprecation(
168             script_state->GetIsolate(), script_state->World(),
169             v8::Local<v8::Object>(), prototype_object, interface_object);
170       }
171       break;
172     }
173     case OriginTrialFeature::kOriginTrialsSampleAPIImplied: {
174       if (script_state->PerContextData()
175               ->GetExistingConstructorAndPrototypeForType(
176                   V8OriginTrialsTest::GetWrapperTypeInfo(), &prototype_object,
177                   &interface_object)) {
178         V8OriginTrialsTest::InstallOriginTrialsSampleAPIImplied(
179             script_state->GetIsolate(), script_state->World(),
180             v8::Local<v8::Object>(), prototype_object, interface_object);
181       }
182       break;
183     }
184     case OriginTrialFeature::kOriginTrialsSampleAPINavigation: {
185       if (script_state->PerContextData()
186               ->GetExistingConstructorAndPrototypeForType(
187                   V8OriginTrialsTest::GetWrapperTypeInfo(), &prototype_object,
188                   &interface_object)) {
189         V8OriginTrialsTest::InstallOriginTrialsSampleAPINavigation(
190             script_state->GetIsolate(), script_state->World(),
191             v8::Local<v8::Object>(), prototype_object, interface_object);
192       }
193       break;
194     }
195     default:
196       break;
197   }
198 }
199 
RegisterInstallOriginTrialFeaturesForTesting()200 void RegisterInstallOriginTrialFeaturesForTesting() {
201   if (!s_original_install_origin_trial_features_function) {
202     s_original_install_origin_trial_features_function =
203         SetInstallOriginTrialFeaturesFunction(
204             InstallOriginTrialFeaturesForTesting);
205   }
206   if (!s_original_install_pending_origin_trial_feature_function) {
207     s_original_install_pending_origin_trial_feature_function =
208         SetInstallPendingOriginTrialFeatureFunction(
209             &InstallPendingOriginTrialFeatureForTesting);
210   }
211 }
212 
213 }  // namespace web_core_test_support
214 
215 }  // namespace blink
216