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

Side by Side Diff: Source/bindings/v8/ScriptController.cpp

Issue 13575004: Apply script preprocessor to Web page scripts only. (Closed) Base URL: https://chromium.googlesource.com/external/WebKit_trimmed.git@master
Patch Set: Partial response to review Created 7 years, 6 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
« no previous file with comments | « Source/bindings/v8/ScriptController.h ('k') | Source/bindings/v8/ScriptDebugServer.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2008, 2009 Google Inc. All rights reserved. 2 * Copyright (C) 2008, 2009 Google Inc. All rights reserved.
3 * Copyright (C) 2009 Apple Inc. All rights reserved. 3 * Copyright (C) 2009 Apple Inc. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are 6 * modification, are permitted provided that the following conditions are
7 * met: 7 * met:
8 * 8 *
9 * * Redistributions of source code must retain the above copyright 9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
(...skipping 637 matching lines...) Expand 10 before | Expand all | Expand 10 after
648 // don't want to try to replace its document! 648 // don't want to try to replace its document!
649 if (!m_frame->page()) 649 if (!m_frame->page())
650 return true; 650 return true;
651 651
652 String scriptResult; 652 String scriptResult;
653 if (!result.getString(scriptResult)) 653 if (!result.getString(scriptResult))
654 return true; 654 return true;
655 655
656 // We're still in a frame, so there should be a DocumentLoader. 656 // We're still in a frame, so there should be a DocumentLoader.
657 ASSERT(m_frame->document()->loader()); 657 ASSERT(m_frame->document()->loader());
658 658
659 // DocumentWriter::replaceDocument can cause the DocumentLoader to get deref 'ed and possible destroyed, 659 // DocumentWriter::replaceDocument can cause the DocumentLoader to get deref 'ed and possible destroyed,
660 // so protect it with a RefPtr. 660 // so protect it with a RefPtr.
661 if (RefPtr<DocumentLoader> loader = m_frame->document()->loader()) 661 if (RefPtr<DocumentLoader> loader = m_frame->document()->loader())
662 loader->writer()->replaceDocument(scriptResult, ownerDocument.get()); 662 loader->writer()->replaceDocument(scriptResult, ownerDocument.get());
663 return true; 663 return true;
664 } 664 }
665 665
666 ScriptValue ScriptController::executeScriptInMainWorld(const ScriptSourceCode& s ourceCode) 666 ScriptValue ScriptController::executeScriptInMainWorld(const ScriptSourceCode& s ourceCode)
667 { 667 {
668 String sourceURL = sourceCode.url(); 668 String sourceURL = sourceCode.url();
669 const String* savedSourceURL = m_sourceURL; 669 const String* savedSourceURL = m_sourceURL;
670 m_sourceURL = &sourceURL; 670 m_sourceURL = &sourceURL;
671 671
672 v8::HandleScope handleScope; 672 v8::HandleScope handleScope;
673 v8::Handle<v8::Context> v8Context = ScriptController::mainWorldContext(m_fra me); 673 v8::Handle<v8::Context> v8Context = ScriptController::mainWorldContext(m_fra me);
674 if (v8Context.IsEmpty()) 674 if (v8Context.IsEmpty())
675 return ScriptValue(); 675 return ScriptValue();
676 676
677 String processedString = m_frame->script()->preprocess(sourceCode.source(), sourceURL);
678 ScriptSourceCode processedSourceCode(processedString, sourceCode.url(), sour ceCode.startPosition());
679
677 v8::Context::Scope scope(v8Context); 680 v8::Context::Scope scope(v8Context);
678 RefPtr<Frame> protect(m_frame); 681 RefPtr<Frame> protect(m_frame);
679 v8::Local<v8::Value> object = compileAndRunScript(sourceCode); 682 v8::Local<v8::Value> object = compileAndRunScript(processedSourceCode);
680 683
681 m_sourceURL = savedSourceURL; 684 m_sourceURL = savedSourceURL;
682 685
683 if (object.IsEmpty()) 686 if (object.IsEmpty())
684 return ScriptValue(); 687 return ScriptValue();
685 688
686 return ScriptValue(object); 689 return ScriptValue(object);
687 } 690 }
688 691
689 void ScriptController::executeScriptInIsolatedWorld(int worldID, const Vector<Sc riptSourceCode>& sources, int extensionGroup, Vector<ScriptValue>* results) 692 void ScriptController::executeScriptInIsolatedWorld(int worldID, const Vector<Sc riptSourceCode>& sources, int extensionGroup, Vector<ScriptValue>* results)
(...skipping 23 matching lines...) Expand all
713 716
714 v8Results = evaluateHandleScope.Close(resultArray); 717 v8Results = evaluateHandleScope.Close(resultArray);
715 } 718 }
716 719
717 if (results && !v8Results.IsEmpty()) { 720 if (results && !v8Results.IsEmpty()) {
718 for (size_t i = 0; i < v8Results->Length(); ++i) 721 for (size_t i = 0; i < v8Results->Length(); ++i)
719 results->append(ScriptValue(v8Results->Get(i))); 722 results->append(ScriptValue(v8Results->Get(i)));
720 } 723 }
721 } 724 }
722 725
726 static const char preprocessorName[] = "$preprocessor.js";
727
728 class ScriptController::ScriptPreprocessor {
729 WTF_MAKE_NONCOPYABLE(ScriptPreprocessor);
730 public:
731 ScriptPreprocessor(const String& preprocessorScript, ScriptController* contr oller)
732 : m_controller(controller), m_isolate(controller->m_isolate)
733 {
734 v8::HandleScope scope(m_isolate);
735
736 v8::Local<v8::Context> context = v8::Context::New(m_isolate);
abarth-chromium 2013/06/07 20:13:45 This CL is not lgtm. Please do not create ad-hoc
737 if (context.IsEmpty())
738 return;
739
740 v8::Local<v8::Value> preprocessorFunction;
741 v8::TryCatch tryCatch;
742
743 v8::Context::Scope contextScope(context);
744 v8::Handle<v8::String> preprocessor = v8String(preprocessorScript, m_iso late);
745 preprocessorFunction = V8ScriptRunner::compileAndRunInternalScript(prepr ocessor, m_isolate);
746
747 if (tryCatch.HasCaught()) {
748 reportException(tryCatch, preprocessorName);
749 return;
750 }
751
752 if (preprocessorFunction.IsEmpty())
753 m_creationError = "Internal error: The preprocessor outer script gav e no value without throwing.";
754 else if (!preprocessorFunction->IsFunction())
755 m_creationError = "The preprocessor must compile to a function.";
756
757 if (!m_creationError.isEmpty()) {
758 reportErrorMessage(preprocessorName);
759 return;
760 }
761
762 m_utilityContext.set(m_isolate, context);
763 m_preprocessorFunction.set(m_isolate, v8::Handle<v8::Function>::Cast(pre processorFunction));
764 }
765
766 void reportException(const v8::TryCatch& tryCatch, const String& sourceName)
767 {
768 v8::Local<v8::Message> message = tryCatch.Message();
769 if (!tryCatch.Message().IsEmpty())
770 m_creationError = toWebCoreStringWithUndefinedOrNullCheck(message->G et());
771 else
772 m_creationError = "Unknown exception.";
773
774 RefPtr<ScriptCallStack> callStack = createScriptCallStack(
775 message->GetStackTrace(), ScriptCallStack::maxCallStackSizeToCapture );
776
777 reportErrorMessage(sourceName, message->GetLineNumber(), callStack);
778 }
779
780 void reportErrorMessage(const String& sourceName, int lineNumber = 1, PassRe fPtr<ScriptCallStack> callStack = RefPtr<ScriptCallStack>())
781 {
782 v8::Context::Scope contextScope(m_controller->mainWorldContext());
783 getScriptExecutionContext()->reportException(m_creationError, lineNumber , sourceName, callStack);
784 }
785
786 String preprocessSourceCode(const String& sourceCode, const String& sourceNa me)
787 {
788 v8::HandleScope handleScope(m_isolate);
789 if (m_preprocessorFunction.isEmpty())
790 return sourceCode;
791
792 v8::Local<v8::Context> context = v8::Local<v8::Context>::New(m_utilityCo ntext.newLocal(m_isolate));
793 v8::Context::Scope contextScope(context);
794
795 v8::Handle<v8::String> sourceCodeString = v8String(sourceCode, m_isolate );
796
797 v8::Handle<v8::String> sourceNameString = v8String(sourceName, m_isolate );
798 v8::Handle<v8::Value> argv[] = { sourceCodeString, sourceNameString };
799
800 v8::TryCatch tryCatch;
801 v8::Handle<v8::Value> resultValue =
802 V8ScriptRunner::callAsFunction(m_preprocessorFunction.newLocal(m_iso late), context->Global(), 2, argv);
803
804 if (tryCatch.HasCaught()) {
805 reportException(tryCatch, sourceName);
806 return sourceCode;
807 }
808
809 if (resultValue->IsString())
810 return toWebCoreStringWithNullCheck(resultValue);
811
812 return sourceCode;
813 }
814
815 bool hadCreationError()
816 {
817 return !m_creationError.isEmpty();
818 }
819
820 ~ScriptPreprocessor()
821 {
822 }
823
824 private:
825 ScopedPersistent<v8::Context> m_utilityContext;
826 String m_preprocessorBody;
827 ScopedPersistent<v8::Function> m_preprocessorFunction;
828 ScriptController* m_controller;
829 v8::Isolate* m_isolate;
830 String m_creationError;
831 };
832
833 void ScriptController::setScriptPreprocessor(const String& preprocessorBody)
834 {
835 // The preprocessor will be created the first time it is needed.
836 m_scriptPreprocessor.clear();
837 m_preprocessorSource = preprocessorBody;
838 }
839
840 String ScriptController::preprocess(const String& scriptSource, const String& sc riptName)
841 {
842 if (m_preprocessorSource.isEmpty())
843 return scriptSource;
844
845 if (!m_scriptPreprocessor)
846 m_scriptPreprocessor = adoptPtr(new ScriptPreprocessor(m_preprocessorSou rce, this));
847
848 if (m_scriptPreprocessor->hadCreationError())
849 return scriptSource;
850
851 return m_scriptPreprocessor->preprocessSourceCode(scriptSource, scriptName);
852 }
853
854
723 } // namespace WebCore 855 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/bindings/v8/ScriptController.h ('k') | Source/bindings/v8/ScriptDebugServer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698