| OLD | NEW |
| (Empty) | |
| 1 /* |
| 2 * Copyright (C) 2008, 2009 Google Inc. All rights reserved. |
| 3 * Copyright (C) 2009 Apple Inc. All rights reserved. |
| 4 * |
| 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions are |
| 7 * met: |
| 8 * |
| 9 * * Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. |
| 11 * * Redistributions in binary form must reproduce the above |
| 12 * copyright notice, this list of conditions and the following disclaimer |
| 13 * in the documentation and/or other materials provided with the |
| 14 * distribution. |
| 15 * * Neither the name of Google Inc. nor the names of its |
| 16 * contributors may be used to endorse or promote products derived from |
| 17 * this software without specific prior written permission. |
| 18 * |
| 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 */ |
| 31 |
| 32 #include "config.h" |
| 33 #include "bindings/v8/ScriptPreprocessor.h" |
| 34 |
| 35 #include "bindings/v8/ScriptController.h" |
| 36 #include "bindings/v8/ScriptDebugServer.h" |
| 37 #include "bindings/v8/ScriptSourceCode.h" |
| 38 #include "bindings/v8/ScriptValue.h" |
| 39 #include "bindings/v8/V8Binding.h" |
| 40 #include "bindings/v8/V8ScriptRunner.h" |
| 41 #include "bindings/v8/V8WindowShell.h" |
| 42 #include "core/page/DOMWindow.h" |
| 43 #include "core/page/Frame.h" |
| 44 #include "core/page/Page.h" |
| 45 #include "core/page/PageConsole.h" |
| 46 #include "wtf/TemporaryChange.h" |
| 47 |
| 48 namespace WebCore { |
| 49 |
| 50 ScriptPreprocessor::ScriptPreprocessor(const String& preprocessorScript, ScriptC
ontroller* controller, PageConsole* console) |
| 51 : m_isPreprocessing(false) |
| 52 { |
| 53 v8::TryCatch tryCatch; |
| 54 tryCatch.SetVerbose(true); |
| 55 ScriptSourceCode preprocessor(preprocessorScript); |
| 56 Vector<ScriptSourceCode> sources; |
| 57 sources.append(preprocessor); |
| 58 Vector<ScriptValue> scriptResults; |
| 59 controller->executeScriptInIsolatedWorld(ScriptPreprocessorIsolatedWorldId,
sources, DOMWrapperWorld::mainWorldExtensionGroup, &scriptResults); |
| 60 |
| 61 if (scriptResults.size() != 1) { |
| 62 console->addMessage(JSMessageSource, ErrorMessageLevel, "ScriptPreproces
sor internal error, one ScriptSourceCode must give exactly one result."); |
| 63 return; |
| 64 } |
| 65 |
| 66 ScriptValue preprocessorFunction = scriptResults[0]; |
| 67 if (!preprocessorFunction.isFunction()) { |
| 68 console->addMessage(JSMessageSource, ErrorMessageLevel, "The preprocesso
r must compile to a function."); |
| 69 return; |
| 70 } |
| 71 |
| 72 RefPtr<DOMWrapperWorld> world = DOMWrapperWorld::ensureIsolatedWorld(ScriptP
reprocessorIsolatedWorldId, DOMWrapperWorld::mainWorldExtensionGroup); |
| 73 V8WindowShell* isolatedWorldShell = controller->windowShell(world.get()); |
| 74 v8::Local<v8::Context> context = isolatedWorldShell->context(); |
| 75 m_isolate = context->GetIsolate(); |
| 76 |
| 77 m_context.set(m_isolate, context); |
| 78 m_preprocessorFunction.set(m_isolate, v8::Handle<v8::Function>::Cast(preproc
essorFunction.v8Value())); |
| 79 } |
| 80 |
| 81 String ScriptPreprocessor::preprocessSourceCode(const String& sourceCode, const
String& sourceName) |
| 82 { |
| 83 if (m_preprocessorFunction.isEmpty()) |
| 84 return sourceCode; |
| 85 |
| 86 v8::HandleScope handleScope(m_isolate); |
| 87 v8::Context::Scope contextScope(m_context.newLocal(m_isolate)); |
| 88 |
| 89 v8::Handle<v8::String> sourceCodeString = v8String(sourceCode, m_isolate); |
| 90 v8::Handle<v8::String> sourceNameString = v8String(sourceName, m_isolate); |
| 91 v8::Handle<v8::Value> argv[] = { sourceCodeString, sourceNameString }; |
| 92 |
| 93 v8::TryCatch tryCatch; |
| 94 tryCatch.SetVerbose(true); |
| 95 TemporaryChange<bool> isPreprocessing(m_isPreprocessing, true); |
| 96 v8::Handle<v8::Value> resultValue = V8ScriptRunner::callAsFunction(m_preproc
essorFunction.newLocal(m_isolate), m_context.newLocal(m_isolate)->Global(), WTF_
ARRAY_LENGTH(argv), argv); |
| 97 |
| 98 if (!resultValue.IsEmpty() && resultValue->IsString()) |
| 99 return toWebCoreStringWithNullCheck(resultValue); |
| 100 |
| 101 return sourceCode; |
| 102 } |
| 103 |
| 104 void ScriptPreprocessor::preprocessEval(ScriptDebugServer* debugServer, v8::Hand
le<v8::Object> eventData) |
| 105 { |
| 106 if (m_isPreprocessing) |
| 107 return; |
| 108 |
| 109 v8::Local<v8::Context> debugContext = v8::Debug::GetDebugContext(); |
| 110 v8::Context::Scope contextScope(debugContext); |
| 111 |
| 112 // <script> tag source and attribute value source are preprocessed before we
enter V8. |
| 113 // Avoid preprocessing any internal scripts by processing only eval source i
n this V8 event handler. |
| 114 v8::Handle<v8::Value> argvEventData[] = { eventData }; |
| 115 String typeInfo = toWebCoreStringWithUndefinedOrNullCheck(debugServer->callD
ebuggerMethod("getScriptCompilationTypeInfo", WTF_ARRAY_LENGTH(argvEventData), a
rgvEventData)); |
| 116 if (!typeInfo.startsWith("eval")) |
| 117 return; |
| 118 |
| 119 // The name and source are in the JS event data. |
| 120 String scriptName = toWebCoreStringWithUndefinedOrNullCheck(debugServer->cal
lDebuggerMethod("getScriptName", WTF_ARRAY_LENGTH(argvEventData), argvEventData)
); |
| 121 String script = toWebCoreStringWithUndefinedOrNullCheck(debugServer->callDeb
uggerMethod("getScriptSource", WTF_ARRAY_LENGTH(argvEventData), argvEventData)); |
| 122 |
| 123 String patchedScript = preprocessSourceCode(script, scriptName); |
| 124 |
| 125 v8::Handle<v8::Value> argvPatchedScript[] = { eventData, v8String(patchedScr
ipt, debugContext->GetIsolate()) }; |
| 126 debugServer->callDebuggerMethod("setScriptSource", WTF_ARRAY_LENGTH(argvPatc
hedScript), argvPatchedScript); |
| 127 } |
| 128 |
| 129 |
| 130 } // namespace WebCore |
| OLD | NEW |