Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(189)

Side by Side Diff: third_party/WebKit/Source/core/testing/v8/WebCoreTestSupport.cpp

Issue 2005433002: [Origin Trials] Install origin trial bindings on V8 context conditionally (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@track-ef-install
Patch Set: Clean up Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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(v8::Local<v8::Context> context, const blink: :DOMWrapperWorld& world)
haraken 2016/05/27 00:01:42 Hmm, this adds a couple of complexity. Alternatel
iclelland 2016/05/27 03:19:43 That's what we do -- see LayoutTests/http/origin_t
haraken 2016/05/27 22:25:17 I'm not sure if it's too late. In common cases, a
iclelland 2016/05/30 15:39:28 That's true, but it's not window that is a problem
86 {
87 DCHECK(!context.IsEmpty());
88 (*s_originalInitializeOriginTrialsFunction)(context, world);
89 ExecutionContext* executionContext = toExecutionContext(context);
90 if (!executionContext->isDocument() && !executionContext->isWorkerGlobalScop e())
91 return;
92 OriginTrialContext* originTrialContext = OriginTrialContext::from(executionC ontext, OriginTrialContext::DontCreateIfNotExists);
93 if (!originTrialContext)
94 return;
95
96 context->Enter();
97 v8::Isolate* isolate = context->GetIsolate();
98 v8::Local<v8::Object> global = context->Global();
99 // TODO(iclelland): This is a placeholder for the real interface object,
100 // which we don't try to obtain at this time. (An assertion in the bindings
101 // generation code prevents it from actually being used.) It should be
102 // replaced with the actual interface if possible, in order to allow origin
103 // trials on static attributes. (crbug.com/614352)
104 v8::Local<v8::Function> interface = v8::Function::New(context, nullptr).ToLo calChecked();
105
106 v8::Local<v8::String> internalsName = v8::String::NewFromUtf8(isolate, "inte rnals", v8::NewStringType::kNormal).ToLocalChecked();
107 v8::Local<v8::Value> v8Internals = global->Get(context, internalsName).ToLoc alChecked();
108 if (v8Internals->IsObject()) {
109 v8::Local<v8::Object> internals = v8Internals->ToObject();
110 if (!originTrialContext->featureBindingsInstalled("Frobulate") && origin TrialContext->isFeatureEnabled("Frobulate", nullptr)) {
111 if (executionContext->isDocument())
112 V8Internals::installOriginTrialsSampleAPI(isolate, world, intern als, interface);
113 else if (executionContext->isWorkerGlobalScope())
114 V8WorkerInternals::installOriginTrialsSampleAPI(isolate, world, internals, interface);
115 originTrialContext->setFeatureBindingsInstalled("Frobulate");
116 }
117 }
118 context->Exit();
61 } 119 }
62 120
63 void resetInternalsObject(v8::Local<v8::Context> context) 121 void resetInternalsObject(v8::Local<v8::Context> context)
64 { 122 {
65 // This can happen if JavaScript is disabled in the main frame. 123 // This can happen if JavaScript is disabled in the main frame.
66 if (context.IsEmpty()) 124 if (context.IsEmpty())
67 return; 125 return;
68 126
69 ScriptState* scriptState = ScriptState::from(context); 127 ScriptState* scriptState = ScriptState::from(context);
70 ScriptState::Scope scope(scriptState); 128 ScriptState::Scope scope(scriptState);
71 Document* document = toDocument(scriptState->getExecutionContext()); 129 Document* document = toDocument(scriptState->getExecutionContext());
72 ASSERT(document); 130 ASSERT(document);
73 LocalFrame* frame = document->frame(); 131 LocalFrame* frame = document->frame();
74 // Should the document have been detached, the page is assumed being destroy ed (=> no reset required.) 132 // Should the document have been detached, the page is assumed being destroy ed (=> no reset required.)
75 if (!frame) 133 if (!frame)
76 return; 134 return;
77 Page* page = frame->page(); 135 Page* page = frame->page();
78 ASSERT(page); 136 ASSERT(page);
79 Internals::resetToConsistentState(page); 137 Internals::resetToConsistentState(page);
80 InternalSettings::from(*page)->resetToConsistentState(); 138 InternalSettings::from(*page)->resetToConsistentState();
81 } 139 }
82 140
83 } // namespace WebCoreTestSupport 141 } // namespace WebCoreTestSupport
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698