OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2011 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
11 * notice, this list of conditions and the following disclaimer in the | 11 * notice, this list of conditions and the following disclaimer in the |
12 * documentation and/or other materials provided with the distribution. | 12 * documentation and/or other materials provided with the distribution. |
13 * | 13 * |
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY | 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 | 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 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 | 17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | 18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 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 | 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 | 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 | 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. | 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 */ | 24 */ |
25 | 25 |
26 #include "core/testing/v8/WebCoreTestSupport.h" | 26 #include "core/testing/v8/WebCoreTestSupport.h" |
27 | 27 |
28 #include "bindings/core/v8/DOMWrapperWorld.h" | |
29 #include "bindings/core/v8/V8Binding.h" | |
28 #include "bindings/core/v8/V8Internals.h" | 30 #include "bindings/core/v8/V8Internals.h" |
31 #include "bindings/core/v8/V8WorkerInternals.h" | |
29 #include "core/dom/Document.h" | 32 #include "core/dom/Document.h" |
30 #include "core/dom/ExecutionContext.h" | 33 #include "core/dom/ExecutionContext.h" |
31 #include "core/frame/LocalFrame.h" | 34 #include "core/frame/LocalFrame.h" |
35 #include "core/origin_trials/OriginTrialContext.h" | |
32 #include "core/testing/InternalSettings.h" | 36 #include "core/testing/InternalSettings.h" |
33 #include "core/testing/Internals.h" | 37 #include "core/testing/Internals.h" |
34 #include "core/testing/WorkerInternals.h" | 38 #include "core/testing/WorkerInternals.h" |
35 | 39 |
36 using namespace blink; | 40 using namespace blink; |
37 | 41 |
38 namespace WebCoreTestSupport { | 42 namespace WebCoreTestSupport { |
39 | 43 |
44 namespace { | |
45 blink::initializeOriginTrialsFunction s_originalInitializeOriginTrialsFunction = nullptr; | |
46 } | |
47 | |
40 v8::Local<v8::Value> createInternalsObject(v8::Local<v8::Context> context) | 48 v8::Local<v8::Value> createInternalsObject(v8::Local<v8::Context> context) |
41 { | 49 { |
42 ScriptState* scriptState = ScriptState::from(context); | 50 ScriptState* scriptState = ScriptState::from(context); |
43 v8::Local<v8::Object> global = scriptState->context()->Global(); | 51 v8::Local<v8::Object> global = scriptState->context()->Global(); |
44 ExecutionContext* executionContext = scriptState->getExecutionContext(); | 52 ExecutionContext* executionContext = scriptState->getExecutionContext(); |
45 if (executionContext->isDocument()) | 53 if (executionContext->isDocument()) |
46 return toV8(Internals::create(scriptState), global, scriptState->isolate ()); | 54 return toV8(Internals::create(scriptState), global, scriptState->isolate ()); |
47 if (executionContext->isWorkerGlobalScope()) | 55 if (executionContext->isWorkerGlobalScope()) |
48 return toV8(WorkerInternals::create(scriptState), global, scriptState->i solate()); | 56 return toV8(WorkerInternals::create(scriptState), global, scriptState->i solate()); |
49 return v8::Local<v8::Value>(); | 57 return v8::Local<v8::Value>(); |
50 } | 58 } |
51 | 59 |
52 void injectInternalsObject(v8::Local<v8::Context> context) | 60 void injectInternalsObject(v8::Local<v8::Context> context) |
53 { | 61 { |
62 if (!s_originalInitializeOriginTrialsFunction) { | |
63 s_originalInitializeOriginTrialsFunction = setInitializeOriginTrialsFunc tion(initializeOriginTrialsForTests); | |
64 } | |
65 | |
54 ScriptState* scriptState = ScriptState::from(context); | 66 ScriptState* scriptState = ScriptState::from(context); |
55 ScriptState::Scope scope(scriptState); | 67 ScriptState::Scope scope(scriptState); |
56 v8::Local<v8::Object> global = scriptState->context()->Global(); | 68 v8::Local<v8::Object> global = scriptState->context()->Global(); |
57 v8::Local<v8::Value> internals = createInternalsObject(context); | 69 v8::Local<v8::Value> internals = createInternalsObject(context); |
58 if (internals.IsEmpty()) | 70 if (internals.IsEmpty()) |
59 return; | 71 return; |
72 | |
60 v8CallOrCrash(global->Set(scriptState->context(), v8AtomicString(scriptState ->isolate(), Internals::internalsId), internals)); | 73 v8CallOrCrash(global->Set(scriptState->context(), v8AtomicString(scriptState ->isolate(), Internals::internalsId), internals)); |
74 | |
75 // If Origin Trials have been registered before the internals object was rea dy, | |
76 // then inject them into the context now | |
77 ExecutionContext* executionContext = toExecutionContext(context); | |
78 if (executionContext) { | |
79 OriginTrialContext* originTrialContext = OriginTrialContext::from(execut ionContext); | |
80 if (originTrialContext) | |
81 originTrialContext->initializePendingTrials(); | |
82 } | |
83 } | |
84 | |
85 void initializeOriginTrialsForTests(ScriptState* scriptState) | |
86 { | |
87 (*s_originalInitializeOriginTrialsFunction)(scriptState); | |
88 | |
89 v8::Local<v8::Context> context = scriptState->context(); | |
90 ExecutionContext* executionContext = toExecutionContext(scriptState->context ()); | |
91 if (!executionContext->isDocument() && !executionContext->isWorkerGlobalScop e()) | |
92 return; | |
93 OriginTrialContext* originTrialContext = OriginTrialContext::from(executionC ontext, OriginTrialContext::DontCreateIfNotExists); | |
94 if (!originTrialContext) | |
95 return; | |
96 | |
97 ScriptState::Scope scope(scriptState); | |
98 v8::Local<v8::Object> global = context->Global(); | |
99 v8::Isolate* isolate = scriptState->isolate(); | |
100 const DOMWrapperWorld& world = scriptState->world(); | |
101 // TODO(iclelland): This is a placeholder for the real interface object, | |
102 // which we don't try to obtain at this time. (An assertion in the bindings | |
103 // generation code prevents it from actually being used.) It should be | |
104 // replaced with the actual interface if possible, in order to allow origin | |
105 // trials on static attributes. (crbug.com/614352) | |
106 v8::Local<v8::Function> interface = v8::Function::New(context, nullptr).ToLo calChecked(); | |
107 | |
108 v8::Local<v8::String> internalsName = v8::String::NewFromUtf8(isolate, "inte rnals", v8::NewStringType::kNormal).ToLocalChecked(); | |
109 v8::Local<v8::Value> v8Internals = global->Get(context, internalsName).ToLoc alChecked(); | |
110 if (v8Internals->IsObject()) { | |
111 v8::Local<v8::Object> internals = v8Internals->ToObject(); | |
112 if (!originTrialContext->featureBindingsInstalled("Frobulate") && origin TrialContext->isFeatureEnabled("Frobulate", nullptr)) { | |
haraken
2016/05/27 22:25:17
It would be nicer if we generate the if check insi
iclelland
2016/05/30 12:15:35
We'd have to pass the OriginTrialContext into the
| |
113 if (executionContext->isDocument()) | |
114 V8Internals::installOriginTrialsSampleAPI(isolate, world, intern als, interface); | |
115 else if (executionContext->isWorkerGlobalScope()) | |
116 V8WorkerInternals::installOriginTrialsSampleAPI(isolate, world, internals, interface); | |
117 originTrialContext->setFeatureBindingsInstalled("Frobulate"); | |
118 } | |
119 } | |
61 } | 120 } |
62 | 121 |
63 void resetInternalsObject(v8::Local<v8::Context> context) | 122 void resetInternalsObject(v8::Local<v8::Context> context) |
64 { | 123 { |
65 // This can happen if JavaScript is disabled in the main frame. | 124 // This can happen if JavaScript is disabled in the main frame. |
66 if (context.IsEmpty()) | 125 if (context.IsEmpty()) |
67 return; | 126 return; |
68 | 127 |
69 ScriptState* scriptState = ScriptState::from(context); | 128 ScriptState* scriptState = ScriptState::from(context); |
70 ScriptState::Scope scope(scriptState); | 129 ScriptState::Scope scope(scriptState); |
71 Document* document = toDocument(scriptState->getExecutionContext()); | 130 Document* document = toDocument(scriptState->getExecutionContext()); |
72 ASSERT(document); | 131 ASSERT(document); |
73 LocalFrame* frame = document->frame(); | 132 LocalFrame* frame = document->frame(); |
74 // Should the document have been detached, the page is assumed being destroy ed (=> no reset required.) | 133 // Should the document have been detached, the page is assumed being destroy ed (=> no reset required.) |
75 if (!frame) | 134 if (!frame) |
76 return; | 135 return; |
77 Page* page = frame->page(); | 136 Page* page = frame->page(); |
78 ASSERT(page); | 137 ASSERT(page); |
79 Internals::resetToConsistentState(page); | 138 Internals::resetToConsistentState(page); |
80 InternalSettings::from(*page)->resetToConsistentState(); | 139 InternalSettings::from(*page)->resetToConsistentState(); |
81 } | 140 } |
82 | 141 |
83 } // namespace WebCoreTestSupport | 142 } // namespace WebCoreTestSupport |
OLD | NEW |