initial import
[vuplus_webkit] / Source / WebCore / bindings / ScriptControllerBase.cpp
1 /*
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.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "ScriptController.h"
23
24 #include "ContentSecurityPolicy.h"
25 #include "Document.h"
26 #include "DocumentLoader.h"
27 #include "Frame.h"
28 #include "FrameLoaderClient.h"
29 #include "Page.h"
30 #include "ScriptSourceCode.h"
31 #include "ScriptValue.h"
32 #include "SecurityOrigin.h"
33 #include "Settings.h"
34 #include "UserGestureIndicator.h"
35
36 namespace WebCore {
37
38 bool ScriptController::canExecuteScripts(ReasonForCallingCanExecuteScripts reason)
39 {
40     // FIXME: We should get this information from the document instead of the frame.
41     if (m_frame->loader()->isSandboxed(SandboxScripts))
42         return false;
43
44     if (m_frame->document() && m_frame->document()->isViewSource()) {
45         ASSERT(m_frame->document()->securityOrigin()->isUnique());
46         return true;
47     }
48
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();
53     return allowed;
54 }
55
56 ScriptValue ScriptController::executeScript(const String& script, bool forceUserGesture)
57 {
58     UserGestureIndicator gestureIndicator(forceUserGesture ? DefinitelyProcessingUserGesture : PossiblyProcessingUserGesture);
59     return executeScript(ScriptSourceCode(script, m_frame->document()->url()));
60 }
61
62 ScriptValue ScriptController::executeScript(const ScriptSourceCode& sourceCode)
63 {
64     if (!canExecuteScripts(AboutToExecuteScript) || isPaused())
65         return ScriptValue();
66
67     bool wasInExecuteScript = m_inExecuteScript;
68     m_inExecuteScript = true;
69
70     RefPtr<Frame> protect(m_frame); // Script execution can destroy the frame, and thus the ScriptController.
71
72     ScriptValue result = evaluate(sourceCode);
73
74     if (!wasInExecuteScript) {
75         m_inExecuteScript = false;
76         Document::updateStyleForAllDocuments();
77     }
78
79     return result;
80 }
81
82 bool ScriptController::executeIfJavaScriptURL(const KURL& url, ShouldReplaceDocumentIfJavaScriptURL shouldReplaceDocumentIfJavaScriptURL)
83 {
84     if (!protocolIsJavaScript(url))
85         return false;
86
87     if (!m_frame->page()
88         || !m_frame->page()->javaScriptURLsAreAllowed()
89         || !m_frame->document()->contentSecurityPolicy()->allowJavaScriptURLs()
90         || m_frame->inViewSourceMode())
91         return true;
92
93     // We need to hold onto the Frame here because executing script can
94     // destroy the frame.
95     RefPtr<Frame> protector(m_frame);
96
97     const int javascriptSchemeLength = sizeof("javascript:") - 1;
98
99     String decodedURL = decodeURLEscapeSequences(url.string());
100     ScriptValue result = executeScript(decodedURL.substring(javascriptSchemeLength));
101
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())
105         return true;
106
107     String scriptResult;
108 #if USE(JSC)
109     JSDOMWindowShell* shell = windowShell(mainThreadNormalWorld());
110     JSC::ExecState* exec = shell->window()->globalExec();
111     if (!result.getString(exec, scriptResult))
112         return true;
113 #else
114     if (!result.getString(scriptResult))
115         return true;
116 #endif
117
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());
124         
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);
129     }
130     return true;
131 }
132
133 } // namespace WebCore