Fix the issue that Web Audio test case fails on PR3.
[framework/web/webkit-efl.git] / 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 #include <wtf/text/TextPosition.h>
36
37 namespace WebCore {
38
39 bool ScriptController::canExecuteScripts(ReasonForCallingCanExecuteScripts reason)
40 {
41     if (m_frame->document() && m_frame->document()->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()->allowScript(settings && settings->isScriptEnabled());
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     RefPtr<Frame> protect(m_frame); // Script execution can destroy the frame, and thus the ScriptController.
68
69     return evaluate(sourceCode);
70 }
71
72 bool ScriptController::executeIfJavaScriptURL(const KURL& url, ShouldReplaceDocumentIfJavaScriptURL shouldReplaceDocumentIfJavaScriptURL)
73 {
74     if (!protocolIsJavaScript(url))
75         return false;
76
77     if (!m_frame->page()
78         || !m_frame->page()->javaScriptURLsAreAllowed()
79         || !m_frame->document()->contentSecurityPolicy()->allowJavaScriptURLs(m_frame->document()->url(), eventHandlerPosition().m_line)
80         || m_frame->inViewSourceMode())
81         return true;
82
83     // We need to hold onto the Frame here because executing script can
84     // destroy the frame.
85     RefPtr<Frame> protector(m_frame);
86     RefPtr<Document> ownerDocument(m_frame->document());
87
88     const int javascriptSchemeLength = sizeof("javascript:") - 1;
89
90     String decodedURL = decodeURLEscapeSequences(url.string());
91     ScriptValue result = executeScript(decodedURL.substring(javascriptSchemeLength));
92
93     // If executing script caused this frame to be removed from the page, we
94     // don't want to try to replace its document!
95     if (!m_frame->page())
96         return true;
97
98     String scriptResult;
99 #if USE(JSC)
100     JSDOMWindowShell* shell = windowShell(mainThreadNormalWorld());
101     JSC::ExecState* exec = shell->window()->globalExec();
102     if (!result.getString(exec, scriptResult))
103         return true;
104 #else
105     if (!result.getString(scriptResult))
106         return true;
107 #endif
108
109     // FIXME: We should always replace the document, but doing so
110     //        synchronously can cause crashes:
111     //        http://bugs.webkit.org/show_bug.cgi?id=16782
112     if (shouldReplaceDocumentIfJavaScriptURL == ReplaceDocumentIfJavaScriptURL) {
113         // We're still in a frame, so there should be a DocumentLoader.
114         ASSERT(m_frame->document()->loader());
115         
116         // DocumentWriter::replaceDocument can cause the DocumentLoader to get deref'ed and possible destroyed,
117         // so protect it with a RefPtr.
118         if (RefPtr<DocumentLoader> loader = m_frame->document()->loader())
119             loader->writer()->replaceDocument(scriptResult, ownerDocument.get());
120     }
121     return true;
122 }
123
124 } // namespace WebCore