Chromium Code Reviews| 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/ScriptSourceCode.h" | |
| 37 #include "bindings/v8/ScriptValue.h" | |
| 38 #include "bindings/v8/V8Binding.h" | |
| 39 #include "bindings/v8/V8ScriptRunner.h" | |
| 40 #include "bindings/v8/V8WindowShell.h" | |
| 41 #include "core/page/DOMWindow.h" | |
| 42 #include "core/page/Frame.h" | |
| 43 #include "core/page/Page.h" | |
| 44 #include "core/page/PageConsole.h" | |
| 45 #include "wtf/TemporaryChange.h" | |
| 46 | |
| 47 namespace WebCore { | |
| 48 | |
| 49 ScriptPreprocessor::ScriptPreprocessor(const String& preprocessorScript, ScriptC ontroller* controller, PageConsole* console) | |
| 50 : m_controller(controller) | |
| 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 v8::Local<v8::Context> context = isolatedWorldContext(); | |
| 73 v8::Isolate* isolate = context->GetIsolate(); | |
|
haraken
2013/07/11 01:13:59
Instead of looking up the Isolate every time, you
johnjbarton
2013/07/11 19:14:42
Done.
| |
| 74 m_preprocessorFunction.set(isolate, v8::Handle<v8::Function>::Cast(preproces sorFunction.v8Value())); | |
| 75 } | |
| 76 | |
| 77 v8::Local<v8::Context> ScriptPreprocessor::isolatedWorldContext() | |
|
haraken
2013/07/11 01:13:59
It looks wasteful to call isolatedWorldContext() a
johnjbarton
2013/07/11 19:14:42
Done.
| |
| 78 { | |
| 79 RefPtr<DOMWrapperWorld> world = DOMWrapperWorld::ensureIsolatedWorld(ScriptP reprocessorIsolatedWorldId, DOMWrapperWorld::mainWorldExtensionGroup); | |
| 80 V8WindowShell* isolatedWorldShell = m_controller->windowShell(world.get()); | |
| 81 return isolatedWorldShell->context(); | |
| 82 } | |
| 83 | |
| 84 String ScriptPreprocessor::preprocessSourceCode(const String& sourceCode, const String& sourceName) | |
| 85 { | |
| 86 if (m_preprocessorFunction.isEmpty()) | |
| 87 return sourceCode; | |
| 88 | |
| 89 v8::Local<v8::Context> context = isolatedWorldContext(); | |
| 90 v8::Isolate* isolate(context->GetIsolate()); | |
| 91 v8::HandleScope handleScope(isolate); | |
| 92 v8::Context::Scope contextScope(context); | |
| 93 | |
| 94 v8::Handle<v8::String> sourceCodeString = v8String(sourceCode, isolate); | |
| 95 v8::Handle<v8::String> sourceNameString = v8String(sourceName, isolate); | |
| 96 v8::Handle<v8::Value> argv[] = { sourceCodeString, sourceNameString }; | |
| 97 | |
| 98 v8::TryCatch tryCatch; | |
| 99 tryCatch.SetVerbose(true); | |
| 100 TemporaryChange<bool> isPreprocessing(m_isPreprocessing, true); | |
| 101 v8::Handle<v8::Value> resultValue = V8ScriptRunner::callAsFunction(m_preproc essorFunction.newLocal(isolate), context->Global(), WTF_ARRAY_LENGTH(argv), argv ); | |
| 102 | |
| 103 if (!resultValue.IsEmpty() && resultValue->IsString()) | |
| 104 return toWebCoreStringWithNullCheck(resultValue); | |
| 105 | |
| 106 return sourceCode; | |
| 107 } | |
| 108 | |
| 109 bool ScriptPreprocessor::hasPreprocessorFunction() | |
| 110 { | |
| 111 return !m_preprocessorFunction.isEmpty(); | |
| 112 } | |
| 113 | |
| 114 bool ScriptPreprocessor::isPreprocessing() | |
| 115 { | |
| 116 return m_isPreprocessing; | |
| 117 } | |
| 118 | |
| 119 } // namespace WebCore | |
| OLD | NEW |