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::InstallOriginTrialsFunction s_originalInstallOriginTrialsFunction = nullp
tr; |
| 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_originalInstallOriginTrialsFunction) { |
| 63 s_originalInstallOriginTrialsFunction = setInstallOriginTrialsFunction(i
nstallOriginTrialsForTests); |
| 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 installOriginTrialsForTests(ScriptState* scriptState) |
| 86 { |
| 87 (*s_originalInstallOriginTrialsFunction)(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 |
| 101 v8::Local<v8::String> internalsName = v8::String::NewFromOneByte(isolate, re
interpret_cast<const uint8_t*>("internals"), v8::NewStringType::kNormal).ToLocal
Checked(); |
| 102 v8::Local<v8::Value> v8Internals = global->Get(context, internalsName).ToLoc
alChecked(); |
| 103 if (v8Internals->IsObject()) { |
| 104 v8::Local<v8::Object> internals = v8Internals->ToObject(); |
| 105 if (!originTrialContext->featureBindingsInstalled("Frobulate") && origin
TrialContext->isFeatureEnabled("Frobulate", nullptr)) { |
| 106 if (executionContext->isDocument()) |
| 107 V8Internals::installOriginTrialsSampleAPI(scriptState, internals
); |
| 108 else if (executionContext->isWorkerGlobalScope()) |
| 109 V8WorkerInternals::installOriginTrialsSampleAPI(scriptState, int
ernals); |
| 110 originTrialContext->setFeatureBindingsInstalled("Frobulate"); |
| 111 } |
| 112 } |
61 } | 113 } |
62 | 114 |
63 void resetInternalsObject(v8::Local<v8::Context> context) | 115 void resetInternalsObject(v8::Local<v8::Context> context) |
64 { | 116 { |
65 // This can happen if JavaScript is disabled in the main frame. | 117 // This can happen if JavaScript is disabled in the main frame. |
66 if (context.IsEmpty()) | 118 if (context.IsEmpty()) |
67 return; | 119 return; |
68 | 120 |
69 ScriptState* scriptState = ScriptState::from(context); | 121 ScriptState* scriptState = ScriptState::from(context); |
70 ScriptState::Scope scope(scriptState); | 122 ScriptState::Scope scope(scriptState); |
71 Document* document = toDocument(scriptState->getExecutionContext()); | 123 Document* document = toDocument(scriptState->getExecutionContext()); |
72 ASSERT(document); | 124 ASSERT(document); |
73 LocalFrame* frame = document->frame(); | 125 LocalFrame* frame = document->frame(); |
74 // Should the document have been detached, the page is assumed being destroy
ed (=> no reset required.) | 126 // Should the document have been detached, the page is assumed being destroy
ed (=> no reset required.) |
75 if (!frame) | 127 if (!frame) |
76 return; | 128 return; |
77 Page* page = frame->page(); | 129 Page* page = frame->page(); |
78 ASSERT(page); | 130 ASSERT(page); |
79 Internals::resetToConsistentState(page); | 131 Internals::resetToConsistentState(page); |
80 InternalSettings::from(*page)->resetToConsistentState(); | 132 InternalSettings::from(*page)->resetToConsistentState(); |
81 } | 133 } |
82 | 134 |
83 } // namespace WebCoreTestSupport | 135 } // namespace WebCoreTestSupport |
OLD | NEW |