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

Side by Side Diff: Source/WebCore/bindings/dart/DartController.cpp

Issue 9188009: Things to unfork. (Closed) Base URL: svn://svn.chromium.org/multivm/trunk/webkit
Patch Set: Created 8 years, 11 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2009, Google Inc.
2 // 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 are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 #include "config.h"
31 #include "DartController.h"
32
33 #include "DOMTimer.h"
34 #include "DOMWindow.h"
35 #include "DartApplicationLoader.h"
36 #include "DartBuiltinLibrarySource.h"
37 #include "DartClassInfo.h"
38 #include "DartDOMWindow.h"
39 #include "DartDOMWrapper.h"
40 #include "DartIsolateState.h"
41 #include "DartUtilities.h"
42 #include "Document.h"
43 #include "Frame.h"
44 #include "HTMLNames.h"
45 #include "HTMLScriptElement.h"
46 #include "IDBPendingTransactionMonitor.h"
47 #include "NodeList.h"
48 #include "Page.h"
49 #include "PageGroup.h"
50 #include "ScheduledAction.h"
51 #include "ScriptExecutionContext.h"
52 #include "Settings.h"
53 #include "StorageNamespace.h"
54
55 #include "npruntime_impl.h"
56 #include <bindings/npruntime.h>
57
58 namespace WebCore {
59
60 static void initDOMIsolate()
61 {
62 DartApiScope dartApiScope;
63
64 // Fix the dom library.
65 Dart_Handle dom = Dart_LookupLibrary(Dart_NewString(DartUtilities::domLibrar yName));
66
67 // Inject builtin library to forward core functionality to dom.
68 // FIXME: We need to provide something for non-DOM isolates as well.
69 Dart_Handle core = Dart_LookupLibrary(Dart_NewString("dart:core"));
70 Dart_LoadSource(core, Dart_NewString("dart:builtin"), Dart_NewString(dartBui ltinLibrarySource));
71 Dart_LibraryImportLibrary(core, dom);
72
73 Dart_Handle domimpl = Dart_LookupLibrary(Dart_NewString("dart:domimpl"));
74 ASSERT(!Dart_IsError(domimpl));
75 Dart_SetNativeResolver(domimpl, domResolver);
76 }
77
78 DartPerScriptState::DartPerScriptState(Document* document, PassRefPtr<DartApplic ationLoader> applicationLoader)
79 : m_dartApplicationLoader(applicationLoader)
80 , m_isolate(DartIsolateState::create(document, m_dartApplicationLoader.get() ))
81 {
82 initDOMIsolate();
83 isolateToDartApplicationLoaderMap().set(m_isolate, m_dartApplicationLoader.g et());
84 // DartIsolateState::create pushes newly create isolate, undo it.
85 DartIsolateState::pop();
86 }
87
88 DartPerScriptState::~DartPerScriptState()
89 {
90 *DartUtilities::recursionForIsolate(m_isolate) = 0;
91 isolateToDartApplicationLoaderMap().remove(m_isolate);
92 DartIsolateState::shutdown(m_isolate);
93 }
94
95 DartController::DartController(Frame* frame)
96 : m_frame(frame)
97 , m_scriptsLoaded(false)
98 , m_states()
99 , m_npObjectMap()
100 {
101 // The DartController's constructor must be called in the Frame's
102 // constructor, so it can properly maintain the unit of related
103 // browsing contexts.
104
105 // The DartController must be created after the frame's loader and
106 // tree nodes are initialized.
107 ASSERT(frame->loader());
108 ASSERT(frame->tree());
109 }
110
111 void DartController::clearWindowShell()
112 {
113 m_scriptsLoaded = false;
114 m_states.clear();
115 }
116
117 class PostMessageTask : public ScriptExecutionContext::Task {
118 public:
119 PostMessageTask(Dart_Isolate destinationIsolate, Dart_Port destinationPort, Dart_Port replyPort, Dart_Message message)
120 : m_destinationIsolate(destinationIsolate)
121 , m_destinationPort(destinationPort)
122 , m_replyPort(replyPort)
123 , m_message(message) { }
124
125 ~PostMessageTask()
126 {
127 free(m_message);
128 }
129
130 virtual void performTask(ScriptExecutionContext* context)
131 {
132 // FIXME: one shouldn't trust isFullDomIsolate as another
133 // isolate with the same address might be allocated. Apparently better
134 // way would be to maintain a way to tell all the tasks that they are
135 // cancelled from now on. For example, we may maintain a list of all
136 // pending tasks and iterate over it.
137 // destinationIsolate might have been shut down before.
138 if (!DartUtilities::isFullDomIsolate(m_destinationIsolate))
139 return;
140 DartIsolateState::Scope scope(m_destinationIsolate);
141 DartApiScope apiScope;
142 Dart_Handle result = Dart_HandleMessage(m_destinationPort, m_replyPort, m_message);
143 if (Dart_IsError(result))
144 DartUtilities::reportProblem(context, result);
145 }
146
147 private:
148 Dart_Isolate m_destinationIsolate;
149 Dart_Port m_destinationPort;
150 Dart_Port m_replyPort;
151 Dart_Message m_message;
152 };
153
154 static bool postMessageCallback(Dart_Isolate destinationIsolate, Dart_Port desti nationPort, Dart_Port replyPort, Dart_Message message)
155 {
156 ASSERT(DartUtilities::isFullDomIsolate(destinationIsolate));
157 ScriptExecutionContext* destinationContext = DartUtilities::isolateContext(d estinationIsolate);
158 destinationContext->postTask(adoptPtr(new PostMessageTask(destinationIsolate , destinationPort, replyPort, message)));
159 return true;
160 }
161
162 static void closePortCallback(Dart_Isolate, Dart_Port)
163 {
164 }
165
166 void DartController::setupDOMEnabledIsolate(ScriptExecutionContext* context)
167 {
168 ASSERT(context);
169 Dart_SetMessageCallbacks(&postMessageCallback, &closePortCallback);
170 DartUtilities::registerIsolateContext(Dart_CurrentIsolate(), context);
171 }
172
173 bool DartController::createPureIsolateCallback(void* data, char** errorMsg)
174 {
175 Dart_Isolate isolate = Dart_CreateIsolate(DartUtilities::fullSnapshot(), dat a, errorMsg);
176 if (!isolate)
177 return false;
178
179 DartApiScope dartApiScope;
180 // It's safe to initialize DOM for pure Dart isolates: unless Dart isolate i s
181 // registered with DartUtilities::registerIsolatecContext, its DOM functiona lity
182 // will be disabled: we won't be able to resolve top level accessors.
183 initDOMIsolate();
184
185 DartApplicationLoader* dartApplicationLoader = reinterpret_cast<DartApplicat ionLoader*>(data);
186 // FIXME: when DartVM has shutdown callback, we'll be able to deref it.
187 dartApplicationLoader->ref();
188 ASSERT(!dartApplicationLoader->isLoadingMainIsolate());
189 dartApplicationLoader->reinjectSources();
190
191 return true;
192 }
193
194 void DartController::initVMIfNeeded()
195 {
196 static bool hasBeenInitialized = false;
197 if (hasBeenInitialized)
198 return;
199
200 // FIXME: make sure Dart_SetVMFlags is called even if there was no
201 // --dart-flags command line switch.
202 Dart_SetVMFlags(0, 0);
203 Dart_Initialize(&createPureIsolateCallback, 0);
204 hasBeenInitialized = true;
205 }
206
207 bool DartController::isDartMimeType(const String& mimeType)
208 {
209 DEFINE_STATIC_LOCAL(HashSet<String>, types, ());
210 if (types.isEmpty()) {
211 types.add("application/dart");
212 types.add("application/dart-app");
213 types.add("application/dart-script");
214 }
215 return !mimeType.isEmpty() && types.contains(mimeType);
216 }
217
218
219 class DartScriptRunner : public EventListener {
220 public:
221 static PassRefPtr<DartScriptRunner> create()
222 {
223 return adoptRef(new DartScriptRunner());
224 }
225
226 virtual void handleEvent(ScriptExecutionContext* context, Event*)
227 {
228 ASSERT(context->isDocument());
229 Document* document = static_cast<Document*>(context);
230
231 // this gets removed below, so protect it while handler runs.
232 RefPtr<DartScriptRunner> protect = this;
233 document->domWindow()->removeEventListener(String("DOMContentLoaded"), t his, false);
234
235 DartController::retrieve(context)->loadScripts();
236 }
237
238 virtual bool operator==(const EventListener& other)
239 {
240 return this == &other;
241 }
242
243 private:
244 DartScriptRunner()
245 : EventListener(EventListener::NativeEventListenerType)
246 {
247 }
248 };
249
250 void DartController::evaluate(const ScriptSourceCode& sourceCode)
251 {
252 if (!m_scriptsLoaded) {
253 m_scriptsLoaded = true;
254 // FIXME: We defer loading Dart scripts until document is fully parsed t o make
255 // sure DartController::loadScripts will find all the script tags in doc ument.
256 // To make startup faster, we should start loading Dart scripts immediat ely.
257 Document* document = m_frame->document();
258 if (document->readyState() == "loading")
259 document->domWindow()->addEventListener(String("DOMContentLoaded"), DartScriptRunner::create(), false);
260 else
261 loadScripts();
262 }
263 }
264
265
266 void DartController::loadScripts()
267 {
268 initVMIfNeeded();
269
270 Vector<RefPtr<HTMLScriptElement> > dartScripts;
271 RefPtr<NodeList> scripts = m_frame->document()->getElementsByTagName("script ");
272 for (unsigned i = 0; i < scripts->length(); ++i) {
273 Node* scriptNode = scripts->item(i);
274 HTMLScriptElement* scriptElement = static_cast<HTMLScriptElement*>(scrip tNode);
275 String typeAttr = scriptElement->getAttribute(HTMLNames::typeAttr).strin g();
276 if (isDartMimeType(typeAttr.stripWhiteSpace().lower()))
277 dartScripts.append(scriptElement);
278 }
279 Document* document = frame()->document();
280 for (size_t i = 0; i < dartScripts.size(); ++i) {
281 RefPtr<HTMLScriptElement> scriptElement = dartScripts.at(i);
282
283 // FIXME: it may make sense to ensure that we'll never call evaluate mor e than once for the same script tag.
284 DartPerScriptState* state = new DartPerScriptState(document, adoptRef(ne w DartApplicationLoader(document)));
285 ASSERT(state->isolate());
286 m_states.append(adoptPtr(state));
287
288 DartIsolateState::Scope scope(state->isolate());
289 if (scriptElement->src().isEmpty())
290 state->dartApplicationLoader()->load(m_frame->document()->url(), scr iptElement->scriptContent());
291 else
292 state->dartApplicationLoader()->loadScriptResource(scriptElement->sr c());
293 }
294 }
295
296 void DartController::bindToWindowObject(Frame* frame, const String& key, NPObjec t* object)
297 {
298 // FIXME: proper management of lifetime.
299 m_npObjectMap.set(key, object);
300 }
301
302 NPObject* DartController::npObject(const String& key)
303 {
304 return m_npObjectMap.get(key);
305 }
306
307 Dart_Handle DartController::callFunction(Dart_Handle function, int argc, Dart_Ha ndle* argv)
308 {
309 // FIXME: Introduce Dart variant of V8GCController::checkMemoryUsage();
310 const int kMaxRecursionDepth = 22;
311
312 int* recursion = DartUtilities::recursionForCurrentIsolate();
313
314 if (*recursion >= kMaxRecursionDepth)
315 return Dart_Error("Maximum call stack size exceeded");
316
317 // FIXME: implement InspectorInstrumentationCookie stuff a la v8.
318 (*recursion)++;
319 Dart_Handle result = Dart_InvokeClosure(function, argc, argv);
320 (*recursion)--;
321
322 // Release the storage mutex if applicable.
323 didLeaveScriptContext(*recursion);
324
325 // Handle fatal error in Dart VM a la v8.
326
327 return result;
328 }
329
330 class StartIsolateAction : public ScheduledAction {
331 public:
332 StartIsolateAction(Dart_Isolate isolate, const String& mainLibURL, const Str ing& entryPoint)
333 : ScheduledAction(v8::Handle<v8::Context>(), String())
334 , m_isolate(isolate)
335 , m_mainLibURL(mainLibURL)
336 , m_entryPoint(entryPoint)
337 { }
338
339 virtual void execute(ScriptExecutionContext* context)
340 {
341 DartIsolateState::Scope scope(m_isolate);
342 DartApiScope apiScope;
343
344 Dart_Port mainPort = Dart_GetMainPortId();
345 Dart_Handle port = Dart_GetReceivePort(mainPort);
346 if (Dart_IsError(port))
347 DartUtilities::reportProblem(context, port);
348
349 Dart_Handle mainLib = Dart_LookupLibrary(DartUtilities::stringToDartStri ng(m_mainLibURL));
350 if (Dart_IsError(mainLib))
351 DartUtilities::reportProblem(context, mainLib);
352
353 Dart_Handle result = Dart_InvokeStatic(
354 mainLib,
355 Dart_NewString(""),
356 DartUtilities::stringToDartString(m_entryPoint),
357 1, &port);
358 // FIXME: consider communicating exceptions back to parent isolate someh ow.
359 if (Dart_IsError(result))
360 DartUtilities::reportProblem(context, result);
361 }
362
363 private:
364 Dart_Isolate m_isolate;
365 const String m_mainLibURL;
366 const String m_entryPoint;
367 };
368
369 Dart_Handle DartController::spawnDomIsolate(DOMWindow* targetWindow, const Strin g& entryPoint)
370 {
371 Dart_Isolate parentIsolate = Dart_CurrentIsolate();
372
373 RefPtr<DartApplicationLoader> applicationLoader = isolateToDartApplicationLo aderMap().get(parentIsolate);
374 ASSERT(applicationLoader);
375
376 Dart_Isolate childIsolate;
377 Dart_Port childIsolatePort;
378 {
379 DartPerScriptState* state = new DartPerScriptState(targetWindow->documen t(), applicationLoader);
380 m_states.append(adoptPtr(state)); // FIXME: should we add to m_states? S hould navigation clear it?
381 childIsolate = state->isolate();
382
383 DartIsolateState::Scope scope(childIsolate);
384 DartApiScope apiScope;
385 applicationLoader->reinjectSources();
386 childIsolatePort = Dart_GetMainPortId();
387 }
388
389 Dart_Handle sendPort = Dart_NewSendPort(childIsolatePort);
390 if (Dart_IsError(sendPort))
391 return sendPort;
392
393 OwnPtr<StartIsolateAction> action = adoptPtr(new StartIsolateAction(childIso late, applicationLoader->mainLibraryURL(), entryPoint));
394 DOMTimer::install(m_frame->document(), action.release(), 0, true);
395
396 return sendPort;
397 }
398
399 void DartController::didLeaveScriptContext(int recursion)
400 {
401 // FIXME: implement this.
402 }
403
404 DartController* DartController::retrieve(Frame* frame)
405 {
406 if (!frame)
407 return 0;
408 return frame->script()->scriptControllerDelegate();
409 }
410
411 DartController* DartController::retrieve(ScriptExecutionContext* context)
412 {
413 if (!context || !context->isDocument())
414 return 0;
415 return retrieve(static_cast<Document*>(context)->frame());
416 }
417
418 }
OLDNEW
« no previous file with comments | « Source/WebCore/bindings/dart/DartController.h ('k') | Source/WebCore/bindings/dart/DartDOMWrapper.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698