2 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
3 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
4 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "ScriptController.h"
24 #include "ContentSecurityPolicy.h"
26 #include "DocumentLoader.h"
28 #include "FrameLoaderClient.h"
30 #include "ScriptSourceCode.h"
31 #include "ScriptValue.h"
32 #include "SecurityOrigin.h"
34 #include "UserGestureIndicator.h"
38 bool ScriptController::canExecuteScripts(ReasonForCallingCanExecuteScripts reason)
40 // FIXME: We should get this information from the document instead of the frame.
41 if (m_frame->loader()->isSandboxed(SandboxScripts))
44 if (m_frame->document() && m_frame->document()->isViewSource()) {
45 ASSERT(m_frame->document()->securityOrigin()->isUnique());
49 Settings* settings = m_frame->settings();
50 const bool allowed = m_frame->loader()->client()->allowJavaScript(settings && settings->isJavaScriptEnabled());
51 if (!allowed && reason == AboutToExecuteScript)
52 m_frame->loader()->client()->didNotAllowScript();
56 ScriptValue ScriptController::executeScript(const String& script, bool forceUserGesture)
58 UserGestureIndicator gestureIndicator(forceUserGesture ? DefinitelyProcessingUserGesture : PossiblyProcessingUserGesture);
59 return executeScript(ScriptSourceCode(script, m_frame->document()->url()));
62 ScriptValue ScriptController::executeScript(const ScriptSourceCode& sourceCode)
64 if (!canExecuteScripts(AboutToExecuteScript) || isPaused())
67 bool wasInExecuteScript = m_inExecuteScript;
68 m_inExecuteScript = true;
70 RefPtr<Frame> protect(m_frame); // Script execution can destroy the frame, and thus the ScriptController.
72 ScriptValue result = evaluate(sourceCode);
74 if (!wasInExecuteScript) {
75 m_inExecuteScript = false;
76 Document::updateStyleForAllDocuments();
82 bool ScriptController::executeIfJavaScriptURL(const KURL& url, ShouldReplaceDocumentIfJavaScriptURL shouldReplaceDocumentIfJavaScriptURL)
84 if (!protocolIsJavaScript(url))
88 || !m_frame->page()->javaScriptURLsAreAllowed()
89 || !m_frame->document()->contentSecurityPolicy()->allowJavaScriptURLs()
90 || m_frame->inViewSourceMode())
93 // We need to hold onto the Frame here because executing script can
95 RefPtr<Frame> protector(m_frame);
97 const int javascriptSchemeLength = sizeof("javascript:") - 1;
99 String decodedURL = decodeURLEscapeSequences(url.string());
100 ScriptValue result = executeScript(decodedURL.substring(javascriptSchemeLength));
102 // If executing script caused this frame to be removed from the page, we
103 // don't want to try to replace its document!
104 if (!m_frame->page())
109 JSDOMWindowShell* shell = windowShell(mainThreadNormalWorld());
110 JSC::ExecState* exec = shell->window()->globalExec();
111 if (!result.getString(exec, scriptResult))
114 if (!result.getString(scriptResult))
118 // FIXME: We should always replace the document, but doing so
119 // synchronously can cause crashes:
120 // http://bugs.webkit.org/show_bug.cgi?id=16782
121 if (shouldReplaceDocumentIfJavaScriptURL == ReplaceDocumentIfJavaScriptURL) {
122 // We're still in a frame, so there should be a DocumentLoader.
123 ASSERT(m_frame->document()->loader());
125 // DocumentWriter::replaceDocument can cause the DocumentLoader to get deref'ed and possible destroyed,
126 // so protect it with a RefPtr.
127 if (RefPtr<DocumentLoader> loader = m_frame->document()->loader())
128 loader->writer()->replaceDocument(scriptResult);
133 } // namespace WebCore