OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2007-2009 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 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 | |
31 #include "config.h" | |
32 #include "ScheduledAction.h" | |
33 | |
34 #include "Document.h" | |
35 #include "ScriptExecutionContext.h" | |
36 #include "ScriptSourceCode.h" | |
37 | |
38 #if PLATFORM(CHROMIUM) | |
39 #include "TraceEvent.h" | |
40 #endif | |
41 | |
42 #include "V8Binding.h" | |
43 #include "V8Proxy.h" | |
44 #include "WorkerContext.h" | |
45 #include "WorkerContextExecutionProxy.h" | |
46 #include "WorkerThread.h" | |
47 | |
48 namespace WebCore { | |
49 | |
50 ScheduledAction::ScheduledAction(v8::Handle<v8::Context> context, v8::Handle<v8:
:Function> func, int argc, v8::Handle<v8::Value> argv[]) | |
51 : m_context(context) | |
52 , m_code(String(), KURL(), TextPosition::belowRangePosition()) | |
53 { | |
54 m_function = v8::Persistent<v8::Function>::New(func); | |
55 | |
56 #ifndef NDEBUG | |
57 V8GCController::registerGlobalHandle(SCHEDULED_ACTION, this, m_function); | |
58 #endif | |
59 | |
60 m_argc = argc; | |
61 if (argc > 0) { | |
62 m_argv = new v8::Persistent<v8::Value>[argc]; | |
63 for (int i = 0; i < argc; i++) { | |
64 m_argv[i] = v8::Persistent<v8::Value>::New(argv[i]); | |
65 | |
66 #ifndef NDEBUG | |
67 V8GCController::registerGlobalHandle(SCHEDULED_ACTION, this, m_argv[i]); | |
68 #endif | |
69 } | |
70 } else | |
71 m_argv = 0; | |
72 } | |
73 | |
74 ScheduledAction::~ScheduledAction() | |
75 { | |
76 if (m_function.IsEmpty()) | |
77 return; | |
78 | |
79 #ifndef NDEBUG | |
80 V8GCController::unregisterGlobalHandle(this, m_function); | |
81 #endif | |
82 m_function.Dispose(); | |
83 | |
84 for (int i = 0; i < m_argc; i++) { | |
85 #ifndef NDEBUG | |
86 V8GCController::unregisterGlobalHandle(this, m_argv[i]); | |
87 #endif | |
88 m_argv[i].Dispose(); | |
89 } | |
90 | |
91 if (m_argc > 0) | |
92 delete[] m_argv; | |
93 } | |
94 | |
95 void ScheduledAction::execute(ScriptExecutionContext* context) | |
96 { | |
97 V8Proxy* proxy = V8Proxy::retrieve(context); | |
98 if (proxy) | |
99 execute(proxy); | |
100 #if ENABLE(WORKERS) | |
101 else if (context->isWorkerContext()) | |
102 execute(static_cast<WorkerContext*>(context)); | |
103 #endif | |
104 // It's possible that Javascript is disabled and that we have neither a V8Pr
oxy | |
105 // nor a WorkerContext. Do nothing in that case. | |
106 } | |
107 | |
108 void ScheduledAction::execute(V8Proxy* proxy) | |
109 { | |
110 ASSERT(proxy); | |
111 | |
112 v8::HandleScope handleScope; | |
113 v8::Handle<v8::Context> v8Context = v8::Local<v8::Context>::New(m_context.ge
t()); | |
114 if (v8Context.IsEmpty()) | |
115 return; // JS may not be enabled. | |
116 | |
117 #if PLATFORM(CHROMIUM) | |
118 TRACE_EVENT("ScheduledAction::execute", this, 0); | |
119 #endif | |
120 | |
121 v8::Context::Scope scope(v8Context); | |
122 | |
123 // FIXME: Need to implement timeouts for preempting a long-running script. | |
124 if (!m_function.IsEmpty() && m_function->IsFunction()) { | |
125 proxy->callFunction(v8::Persistent<v8::Function>::Cast(m_function), v8Co
ntext->Global(), m_argc, m_argv); | |
126 Document::updateStyleForAllDocuments(); | |
127 } else | |
128 proxy->evaluate(m_code, 0); | |
129 | |
130 // The 'proxy' may be invalid at this point since JS could have released the
owning Frame. | |
131 } | |
132 | |
133 #if ENABLE(WORKERS) | |
134 void ScheduledAction::execute(WorkerContext* workerContext) | |
135 { | |
136 // In a Worker, the execution should always happen on a worker thread. | |
137 ASSERT(workerContext->thread()->threadID() == currentThread()); | |
138 | |
139 WorkerScriptController* scriptController = workerContext->script(); | |
140 | |
141 if (!m_function.IsEmpty() && m_function->IsFunction()) { | |
142 v8::HandleScope handleScope; | |
143 v8::Handle<v8::Context> v8Context = v8::Local<v8::Context>::New(m_contex
t.get()); | |
144 ASSERT(!v8Context.IsEmpty()); | |
145 v8::Context::Scope scope(v8Context); | |
146 m_function->Call(v8Context->Global(), m_argc, m_argv); | |
147 } else | |
148 scriptController->evaluate(m_code); | |
149 } | |
150 #endif | |
151 | |
152 } // namespace WebCore | |
OLD | NEW |