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

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: respond to review Created 7 years, 5 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
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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 #include "core/html/HTMLPlugInElement.h" 59 #include "core/html/HTMLPlugInElement.h"
60 #include "core/inspector/InspectorInstrumentation.h" 60 #include "core/inspector/InspectorInstrumentation.h"
61 #include "core/inspector/ScriptCallStack.h" 61 #include "core/inspector/ScriptCallStack.h"
62 #include "core/loader/DocumentLoader.h" 62 #include "core/loader/DocumentLoader.h"
63 #include "core/loader/FrameLoader.h" 63 #include "core/loader/FrameLoader.h"
64 #include "core/loader/FrameLoaderClient.h" 64 #include "core/loader/FrameLoaderClient.h"
65 #include "core/page/ContentSecurityPolicy.h" 65 #include "core/page/ContentSecurityPolicy.h"
66 #include "core/page/DOMWindow.h" 66 #include "core/page/DOMWindow.h"
67 #include "core/page/Frame.h" 67 #include "core/page/Frame.h"
68 #include "core/page/Page.h" 68 #include "core/page/Page.h"
69 #include "core/page/PageConsole.h"
69 #include "core/page/Settings.h" 70 #include "core/page/Settings.h"
70 #include "core/platform/HistogramSupport.h" 71 #include "core/platform/HistogramSupport.h"
71 #include "core/platform/NotImplemented.h" 72 #include "core/platform/NotImplemented.h"
72 #include "core/platform/Widget.h" 73 #include "core/platform/Widget.h"
73 #include "core/platform/chromium/TraceEvent.h" 74 #include "core/platform/chromium/TraceEvent.h"
74 #include "core/plugins/PluginView.h" 75 #include "core/plugins/PluginView.h"
75 #include "weborigin/SecurityOrigin.h" 76 #include "weborigin/SecurityOrigin.h"
76 #include "wtf/CurrentTime.h" 77 #include "wtf/CurrentTime.h"
77 #include "wtf/StdLibExtras.h" 78 #include "wtf/StdLibExtras.h"
78 #include "wtf/StringExtras.h" 79 #include "wtf/StringExtras.h"
80 #include "wtf/TemporaryChange.h"
79 #include "wtf/text/CString.h" 81 #include "wtf/text/CString.h"
80 #include "wtf/text/StringBuilder.h" 82 #include "wtf/text/StringBuilder.h"
81 #include "wtf/text/TextPosition.h" 83 #include "wtf/text/TextPosition.h"
82 84
83 namespace WebCore { 85 namespace WebCore {
84 86
85 bool ScriptController::canAccessFromCurrentOrigin(Frame *frame) 87 bool ScriptController::canAccessFromCurrentOrigin(Frame *frame)
86 { 88 {
87 return !v8::Context::InContext() || BindingSecurity::shouldAllowAccessToFram e(frame); 89 return !v8::Context::InContext() || BindingSecurity::shouldAllowAccessToFram e(frame);
88 } 90 }
(...skipping 572 matching lines...) Expand 10 before | Expand all | Expand 10 after
661 663
662 String scriptResult; 664 String scriptResult;
663 if (!result.getString(scriptResult)) 665 if (!result.getString(scriptResult))
664 return true; 666 return true;
665 667
666 // We're still in a frame, so there should be a DocumentLoader. 668 // We're still in a frame, so there should be a DocumentLoader.
667 ASSERT(m_frame->document()->loader()); 669 ASSERT(m_frame->document()->loader());
668 670
669 if (!locationChangeBefore && m_frame->navigationScheduler()->locationChangeP ending()) 671 if (!locationChangeBefore && m_frame->navigationScheduler()->locationChangeP ending())
670 return true; 672 return true;
671 673
672 // DocumentWriter::replaceDocument can cause the DocumentLoader to get deref 'ed and possible destroyed, 674 // DocumentWriter::replaceDocument can cause the DocumentLoader to get deref 'ed and possible destroyed,
673 // so protect it with a RefPtr. 675 // so protect it with a RefPtr.
674 if (RefPtr<DocumentLoader> loader = m_frame->document()->loader()) 676 if (RefPtr<DocumentLoader> loader = m_frame->document()->loader())
675 loader->replaceDocument(scriptResult, ownerDocument.get()); 677 loader->replaceDocument(scriptResult, ownerDocument.get());
676 return true; 678 return true;
677 } 679 }
678 680
679 ScriptValue ScriptController::executeScriptInMainWorld(const ScriptSourceCode& s ourceCode) 681 ScriptValue ScriptController::executeScriptInMainWorld(const ScriptSourceCode& s ourceCode)
680 { 682 {
681 String sourceURL = sourceCode.url(); 683 String sourceURL = sourceCode.url();
682 const String* savedSourceURL = m_sourceURL; 684 const String* savedSourceURL = m_sourceURL;
683 m_sourceURL = &sourceURL; 685 m_sourceURL = &sourceURL;
684 686
685 v8::HandleScope handleScope; 687 v8::HandleScope handleScope;
686 v8::Handle<v8::Context> v8Context = ScriptController::mainWorldContext(m_fra me); 688 v8::Handle<v8::Context> v8Context = ScriptController::mainWorldContext(m_fra me);
687 if (v8Context.IsEmpty()) 689 if (v8Context.IsEmpty())
688 return ScriptValue(); 690 return ScriptValue();
689 691
692 String processedString = m_frame->script()->preprocess(sourceCode.source(), sourceURL);
693 ScriptSourceCode processedSourceCode(processedString, sourceCode.url(), sour ceCode.startPosition());
694
690 v8::Context::Scope scope(v8Context); 695 v8::Context::Scope scope(v8Context);
691 RefPtr<Frame> protect(m_frame); 696 RefPtr<Frame> protect(m_frame);
692 v8::Local<v8::Value> object = compileAndRunScript(sourceCode); 697 v8::Local<v8::Value> object = compileAndRunScript(processedSourceCode);
693 698
694 m_sourceURL = savedSourceURL; 699 m_sourceURL = savedSourceURL;
695 700
696 if (object.IsEmpty()) 701 if (object.IsEmpty())
697 return ScriptValue(); 702 return ScriptValue();
698 703
699 return ScriptValue(object); 704 return ScriptValue(object);
700 } 705 }
701 706
702 void ScriptController::executeScriptInIsolatedWorld(int worldID, const Vector<Sc riptSourceCode>& sources, int extensionGroup, Vector<ScriptValue>* results) 707 void ScriptController::executeScriptInIsolatedWorld(int worldID, const Vector<Sc riptSourceCode>& sources, int extensionGroup, Vector<ScriptValue>* results)
(...skipping 23 matching lines...) Expand all
726 731
727 v8Results = evaluateHandleScope.Close(resultArray); 732 v8Results = evaluateHandleScope.Close(resultArray);
728 } 733 }
729 734
730 if (results && !v8Results.IsEmpty()) { 735 if (results && !v8Results.IsEmpty()) {
731 for (size_t i = 0; i < v8Results->Length(); ++i) 736 for (size_t i = 0; i < v8Results->Length(); ++i)
732 results->append(ScriptValue(v8Results->Get(i))); 737 results->append(ScriptValue(v8Results->Get(i)));
733 } 738 }
734 } 739 }
735 740
741 class ScriptController::ScriptPreprocessor {
abarth-chromium 2013/07/09 23:14:10 Please put this class in a separate file. We pref
742 WTF_MAKE_NONCOPYABLE(ScriptPreprocessor);
743 public:
744 ScriptPreprocessor(const String& preprocessorScript, ScriptController* contr oller, PageConsole* console)
745 : m_controller(controller)
746 , m_isPreprocessing(false)
747 {
748 v8::TryCatch tryCatch;
749 tryCatch.SetVerbose(true);
750 ScriptSourceCode preprocessor(preprocessorScript);
751 Vector<ScriptSourceCode> sources;
752 sources.append(preprocessor);
753 Vector<ScriptValue> scriptResults;
754 controller->executeScriptInIsolatedWorld(ScriptPreprocessorIsolatedWorld Id, sources, 0, &scriptResults);
755
756 ASSERT(scriptResults.size() == 1);
abarth-chromium 2013/07/09 23:14:10 Please handle this error instead of just ASSERTing
johnjbarton 2013/07/10 23:24:23 Done.
757 ScriptValue preprocessorFunction = scriptResults[0];
758 if (!preprocessorFunction.isFunction()) {
759 console->addMessage(JSMessageSource, ErrorMessageLevel, "The preproc essor must compile to a function.");
760 return;
abarth-chromium 2013/07/09 23:14:10 You're already set up to handle an error here. Wh
johnjbarton 2013/07/10 23:24:23 Done.
761 }
762
763 v8::Local<v8::Context> context = isolatedWorldContext();
764 v8::Isolate* isolate = context->GetIsolate();
765 m_preprocessorFunction.set(isolate, v8::Handle<v8::Function>::Cast(prepr ocessorFunction.v8Value()));
766 }
767
768 v8::Local<v8::Context> isolatedWorldContext()
769 {
770 RefPtr<DOMWrapperWorld> world = DOMWrapperWorld::ensureIsolatedWorld(Scr iptPreprocessorIsolatedWorldId, 0);
abarth-chromium 2013/07/09 23:14:10 What is the 0 in the second position here?
771 V8WindowShell* isolatedWorldShell = m_controller->windowShell(world.get( ));
johnjbarton 2013/07/10 23:24:23 This groups the isolatedWorld with other worlds th
772 return isolatedWorldShell->context();
773 }
774
775 String preprocessSourceCode(const String& sourceCode, const String& sourceNa me)
776 {
777 if (m_preprocessorFunction.isEmpty())
778 return sourceCode;
779
780 v8::Local<v8::Context> context = isolatedWorldContext();
781 v8::Isolate* isolate(context->GetIsolate());
782 v8::HandleScope handleScope(isolate);
783 v8::Context::Scope contextScope(context);
784
785 v8::Handle<v8::String> sourceCodeString = v8String(sourceCode, isolate);
786 v8::Handle<v8::String> sourceNameString = v8String(sourceName, isolate);
787 v8::Handle<v8::Value> argv[] = { sourceCodeString, sourceNameString };
788
789 v8::TryCatch tryCatch;
790 tryCatch.SetVerbose(true);
791 TemporaryChange<bool> isPreprocessing(m_isPreprocessing, true);
792 v8::Handle<v8::Value> resultValue =
793 V8ScriptRunner::callAsFunction(m_preprocessorFunction.newLocal(isola te), context->Global(), WTF_ARRAY_LENGTH(argv), argv);
abarth-chromium 2013/07/09 23:14:10 You can combine these lines. There's no line limi
johnjbarton 2013/07/10 23:24:23 Done.
794
795 if (!resultValue.IsEmpty() && resultValue->IsString())
796 return toWebCoreStringWithNullCheck(resultValue);
797
798 return sourceCode;
799 }
800
801 bool hasPreprocessorFunction()
802 {
803 return !m_preprocessorFunction.isEmpty();
804 }
805
806 bool isPreprocessing()
807 {
808 return m_isPreprocessing;
809 }
810
811 private:
812 String m_preprocessorBody;
813 ScopedPersistent<v8::Function> m_preprocessorFunction;
814 ScriptController* m_controller;
815 bool m_isPreprocessing;
816 };
817
818 bool ScriptController::hasScriptPreprocessor()
819 {
820 return m_scriptPreprocessor && m_scriptPreprocessor->hasPreprocessorFunction ();
821 }
822
823 bool ScriptController::isPreprocessingScript()
824 {
825 return hasScriptPreprocessor() && m_scriptPreprocessor->isPreprocessing();
826 }
827
828 void ScriptController::setScriptPreprocessor(const String& preprocessorBody)
829 {
830 // We delay the creation of the preprocess until just before the first JS fr om the
831 // Web page to ensure that the debugger's console initialization code has co mpleted.
832 m_preprocessorSource = preprocessorBody;
833 }
834
835 void ScriptController::clearScriptPreprocessor()
836 {
837 m_scriptPreprocessor.clear();
838 m_preprocessorSource = "";
abarth-chromium 2013/07/09 23:14:10 m_preprocessorSource = String(); That will get yo
johnjbarton 2013/07/10 23:24:23 Done.
839 }
840
841 String ScriptController::preprocess(const String& scriptSource, const String& sc riptName)
842 {
843 if (m_preprocessorSource.isEmpty())
844 return scriptSource;
845
846 if (!m_scriptPreprocessor)
847 m_scriptPreprocessor = adoptPtr(new ScriptPreprocessor(m_preprocessorSou rce, this, this->m_frame->page()->console()));
abarth-chromium 2013/07/09 23:14:10 this->m_frame -> m_frame The "this->" isn't
johnjbarton 2013/07/10 23:24:23 Done
848
849 if (!m_scriptPreprocessor->hasPreprocessorFunction())
850 return scriptSource;
851
852 return m_scriptPreprocessor->preprocessSourceCode(scriptSource, scriptName);
853 }
854
855
736 } // namespace WebCore 856 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698