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

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

Issue 24758003: ScriptController: limit script execution API to executeScript and callFunction, pass valid ScriptEx… (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: w/ ScheduledAction executing in given context. Created 7 years, 2 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 | Annotate | Revision Log
« no previous file with comments | « Source/bindings/v8/ScriptController.h ('k') | Source/bindings/v8/ScriptFunctionCall.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 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 143
144 void ScriptController::updateSecurityOrigin() 144 void ScriptController::updateSecurityOrigin()
145 { 145 {
146 m_windowShell->updateSecurityOrigin(); 146 m_windowShell->updateSecurityOrigin();
147 } 147 }
148 148
149 v8::Local<v8::Value> ScriptController::callFunction(v8::Handle<v8::Function> fun ction, v8::Handle<v8::Object> receiver, int argc, v8::Handle<v8::Value> args[]) 149 v8::Local<v8::Value> ScriptController::callFunction(v8::Handle<v8::Function> fun ction, v8::Handle<v8::Object> receiver, int argc, v8::Handle<v8::Value> args[])
150 { 150 {
151 // Keep Frame (and therefore ScriptController) alive. 151 // Keep Frame (and therefore ScriptController) alive.
152 RefPtr<Frame> protect(m_frame); 152 RefPtr<Frame> protect(m_frame);
153 return ScriptController::callFunctionWithInstrumentation(m_frame ? m_frame-> document() : 0, function, receiver, argc, args, m_isolate); 153 return ScriptController::callFunction(m_frame->document(), function, receive r, argc, args, m_isolate);
154 }
155
156 ScriptValue ScriptController::callFunctionEvenIfScriptDisabled(v8::Handle<v8::Fu nction> function, v8::Handle<v8::Object> receiver, int argc, v8::Handle<v8::Valu e> argv[])
157 {
158 return ScriptValue(callFunction(function, receiver, argc, argv), m_isolate);
159 } 154 }
160 155
161 static void resourceInfo(const v8::Handle<v8::Function> function, String& resour ceName, int& lineNumber) 156 static void resourceInfo(const v8::Handle<v8::Function> function, String& resour ceName, int& lineNumber)
162 { 157 {
163 v8::ScriptOrigin origin = function->GetScriptOrigin(); 158 v8::ScriptOrigin origin = function->GetScriptOrigin();
164 if (origin.ResourceName().IsEmpty()) { 159 if (origin.ResourceName().IsEmpty()) {
165 resourceName = "undefined"; 160 resourceName = "undefined";
166 lineNumber = 1; 161 lineNumber = 1;
167 } else { 162 } else {
168 resourceName = toWebCoreString(origin.ResourceName()); 163 resourceName = toWebCoreString(origin.ResourceName());
169 lineNumber = function->GetScriptLineNumber() + 1; 164 lineNumber = function->GetScriptLineNumber() + 1;
170 } 165 }
171 } 166 }
172 167
173 static String resourceString(const v8::Handle<v8::Function> function) 168 static String resourceString(const v8::Handle<v8::Function> function)
174 { 169 {
175 String resourceName; 170 String resourceName;
176 int lineNumber; 171 int lineNumber;
177 resourceInfo(function, resourceName, lineNumber); 172 resourceInfo(function, resourceName, lineNumber);
178 173
179 StringBuilder builder; 174 StringBuilder builder;
180 builder.append(resourceName); 175 builder.append(resourceName);
181 builder.append(':'); 176 builder.append(':');
182 builder.appendNumber(lineNumber); 177 builder.appendNumber(lineNumber);
183 return builder.toString(); 178 return builder.toString();
184 } 179 }
185 180
186 v8::Local<v8::Value> ScriptController::callFunctionWithInstrumentation(ScriptExe cutionContext* context, v8::Handle<v8::Function> function, v8::Handle<v8::Object > receiver, int argc, v8::Handle<v8::Value> args[], v8::Isolate* isolate) 181 v8::Local<v8::Value> ScriptController::callFunction(ScriptExecutionContext* cont ext, v8::Handle<v8::Function> function, v8::Handle<v8::Object> receiver, int arg c, v8::Handle<v8::Value> args[], v8::Isolate* isolate)
187 { 182 {
188 InspectorInstrumentationCookie cookie; 183 InspectorInstrumentationCookie cookie;
189 if (InspectorInstrumentation::timelineAgentEnabled(context)) { 184 if (InspectorInstrumentation::timelineAgentEnabled(context)) {
190 String resourceName; 185 String resourceName;
191 int lineNumber; 186 int lineNumber;
192 resourceInfo(function, resourceName, lineNumber); 187 resourceInfo(function, resourceName, lineNumber);
193 cookie = InspectorInstrumentation::willCallFunction(context, resourceNam e, lineNumber); 188 cookie = InspectorInstrumentation::willCallFunction(context, resourceNam e, lineNumber);
194 } 189 }
195 190
196 v8::Local<v8::Value> result = V8ScriptRunner::callFunction(function, context , receiver, argc, args, isolate); 191 v8::Local<v8::Value> result = V8ScriptRunner::callFunction(function, context , receiver, argc, args, isolate);
197 192
198 InspectorInstrumentation::didCallFunction(cookie); 193 InspectorInstrumentation::didCallFunction(cookie);
199 return result; 194 return result;
200 } 195 }
201 196
202 v8::Local<v8::Value> ScriptController::compileAndRunScript(const ScriptSourceCod e& source, AccessControlStatus corsStatus) 197 v8::Local<v8::Value> ScriptController::executeScriptAndReturnValue(v8::Handle<v8 ::Context> context, const ScriptSourceCode& source, AccessControlStatus corsStat us)
203 { 198 {
204 ASSERT(v8::Context::InContext()); 199 v8::Context::Scope scope(context);
205 200
206 InspectorInstrumentationCookie cookie = InspectorInstrumentation::willEvalua teScript(m_frame, source.url().isNull() ? String() : source.url().string(), sour ce.startLine()); 201 InspectorInstrumentationCookie cookie = InspectorInstrumentation::willEvalua teScript(m_frame, source.url().isNull() ? String() : source.url().string(), sour ce.startLine());
207 202
208 v8::Local<v8::Value> result; 203 v8::Local<v8::Value> result;
209 { 204 {
210 // Isolate exceptions that occur when compiling and executing 205 // Isolate exceptions that occur when compiling and executing
211 // the code. These exceptions should not interfere with 206 // the code. These exceptions should not interfere with
212 // javascript code we might evaluate from C++ when returning 207 // javascript code we might evaluate from C++ when returning
213 // from here. 208 // from here.
214 v8::TryCatch tryCatch; 209 v8::TryCatch tryCatch;
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
307 { 302 {
308 if (!v8::Context::InContext()) 303 if (!v8::Context::InContext())
309 return contextForWorld(this, mainThreadNormalWorld()); 304 return contextForWorld(this, mainThreadNormalWorld());
310 305
311 v8::Handle<v8::Context> context = v8::Context::GetEntered(); 306 v8::Handle<v8::Context> context = v8::Context::GetEntered();
312 DOMWrapperWorld* isolatedWorld = DOMWrapperWorld::isolatedWorld(context); 307 DOMWrapperWorld* isolatedWorld = DOMWrapperWorld::isolatedWorld(context);
313 if (!isolatedWorld) 308 if (!isolatedWorld)
314 return contextForWorld(this, mainThreadNormalWorld()); 309 return contextForWorld(this, mainThreadNormalWorld());
315 310
316 Frame* frame = toFrameIfNotDetached(context); 311 Frame* frame = toFrameIfNotDetached(context);
317 if (!m_frame)
318 return v8::Local<v8::Context>();
319
320 if (m_frame == frame) 312 if (m_frame == frame)
321 return v8::Local<v8::Context>::New(m_isolate, context); 313 return v8::Local<v8::Context>::New(m_isolate, context);
322 314
323 return contextForWorld(this, isolatedWorld); 315 return contextForWorld(this, isolatedWorld);
324 } 316 }
325 317
326 v8::Local<v8::Context> ScriptController::mainWorldContext() 318 v8::Local<v8::Context> ScriptController::mainWorldContext()
327 { 319 {
328 return contextForWorld(this, mainThreadNormalWorld()); 320 return contextForWorld(this, mainThreadNormalWorld());
329 } 321 }
(...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after
663 if (v8Context.IsEmpty()) 655 if (v8Context.IsEmpty())
664 return ScriptValue(); 656 return ScriptValue();
665 657
666 RefPtr<Frame> protect(m_frame); 658 RefPtr<Frame> protect(m_frame);
667 if (m_frame->loader()->stateMachine()->isDisplayingInitialEmptyDocument()) 659 if (m_frame->loader()->stateMachine()->isDisplayingInitialEmptyDocument())
668 m_frame->loader()->didAccessInitialDocument(); 660 m_frame->loader()->didAccessInitialDocument();
669 661
670 OwnPtr<ScriptSourceCode> maybeProcessedSourceCode = InspectorInstrumentatio n::preprocess(m_frame, sourceCode); 662 OwnPtr<ScriptSourceCode> maybeProcessedSourceCode = InspectorInstrumentatio n::preprocess(m_frame, sourceCode);
671 const ScriptSourceCode& sourceCodeToCompile = maybeProcessedSourceCode ? *ma ybeProcessedSourceCode : sourceCode; 663 const ScriptSourceCode& sourceCodeToCompile = maybeProcessedSourceCode ? *ma ybeProcessedSourceCode : sourceCode;
672 664
673 v8::Context::Scope scope(v8Context); 665 v8::Local<v8::Value> object = executeScriptAndReturnValue(v8Context, sourceC odeToCompile, corsStatus);
674 v8::Local<v8::Value> object = compileAndRunScript(sourceCodeToCompile, corsS tatus);
675
676 m_sourceURL = savedSourceURL; 666 m_sourceURL = savedSourceURL;
677 667
678 if (object.IsEmpty()) 668 if (object.IsEmpty())
679 return ScriptValue(); 669 return ScriptValue();
680 670
681 return ScriptValue(object, m_isolate); 671 return ScriptValue(object, m_isolate);
682 } 672 }
683 673
684 void ScriptController::executeScriptInIsolatedWorld(int worldID, const Vector<Sc riptSourceCode>& sources, int extensionGroup, Vector<ScriptValue>* results) 674 void ScriptController::executeScriptInIsolatedWorld(int worldID, const Vector<Sc riptSourceCode>& sources, int extensionGroup, Vector<ScriptValue>* results)
685 { 675 {
686 ASSERT(worldID > 0); 676 ASSERT(worldID > 0);
687 677
688 v8::HandleScope handleScope(m_isolate); 678 v8::HandleScope handleScope(m_isolate);
689 v8::Local<v8::Array> v8Results; 679 v8::Local<v8::Array> v8Results;
690 { 680 {
691 v8::HandleScope evaluateHandleScope(m_isolate); 681 v8::HandleScope evaluateHandleScope(m_isolate);
692 RefPtr<DOMWrapperWorld> world = DOMWrapperWorld::ensureIsolatedWorld(wor ldID, extensionGroup); 682 RefPtr<DOMWrapperWorld> world = DOMWrapperWorld::ensureIsolatedWorld(wor ldID, extensionGroup);
693 V8WindowShell* isolatedWorldShell = windowShell(world.get()); 683 V8WindowShell* isolatedWorldShell = windowShell(world.get());
694 684
695 if (!isolatedWorldShell->isContextInitialized()) 685 if (!isolatedWorldShell->isContextInitialized())
696 return; 686 return;
697 687
698 v8::Local<v8::Context> context = isolatedWorldShell->context(); 688 v8::Local<v8::Context> context = isolatedWorldShell->context();
699 v8::Context::Scope contextScope(context); 689 v8::Context::Scope contextScope(context);
700 v8::Local<v8::Array> resultArray = v8::Array::New(sources.size()); 690 v8::Local<v8::Array> resultArray = v8::Array::New(sources.size());
701 691
702 for (size_t i = 0; i < sources.size(); ++i) { 692 for (size_t i = 0; i < sources.size(); ++i) {
703 v8::Local<v8::Value> evaluationResult = compileAndRunScript(sources[ i]); 693 v8::Local<v8::Value> evaluationResult = executeScriptAndReturnValue( context, sources[i]);
704 if (evaluationResult.IsEmpty()) 694 if (evaluationResult.IsEmpty())
705 evaluationResult = v8::Local<v8::Value>::New(m_isolate, v8::Unde fined(m_isolate)); 695 evaluationResult = v8::Local<v8::Value>::New(m_isolate, v8::Unde fined(m_isolate));
706 resultArray->Set(i, evaluationResult); 696 resultArray->Set(i, evaluationResult);
707 } 697 }
708 698
709 v8Results = evaluateHandleScope.Close(resultArray); 699 v8Results = evaluateHandleScope.Close(resultArray);
710 } 700 }
711 701
712 if (results && !v8Results.IsEmpty()) { 702 if (results && !v8Results.IsEmpty()) {
713 for (size_t i = 0; i < v8Results->Length(); ++i) 703 for (size_t i = 0; i < v8Results->Length(); ++i)
714 results->append(ScriptValue(v8Results->Get(i), m_isolate)); 704 results->append(ScriptValue(v8Results->Get(i), m_isolate));
715 } 705 }
716 } 706 }
717 707
718 } // namespace WebCore 708 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/bindings/v8/ScriptController.h ('k') | Source/bindings/v8/ScriptFunctionCall.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698