1 /*
2  * Copyright (C) 2010 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #ifndef InjectedBundle_h
27 #define InjectedBundle_h
28 
29 #include "APIObject.h"
30 #include "InjectedBundleClient.h"
31 #include "SandboxExtension.h"
32 #include "WKBundle.h"
33 #include <WebCore/UserContentTypes.h>
34 #include <WebCore/UserScriptTypes.h>
35 #include <wtf/PassRefPtr.h>
36 #include <wtf/text/WTFString.h>
37 
38 #if PLATFORM(QT)
39 #include <QLibrary>
40 #endif
41 
42 namespace CoreIPC {
43     class ArgumentDecoder;
44     class Connection;
45     class MessageID;
46 }
47 
48 namespace WebKit {
49 
50 #if PLATFORM(MAC)
51 typedef CFBundleRef PlatformBundle;
52 #elif PLATFORM(WIN)
53 typedef HMODULE PlatformBundle;
54 #elif PLATFORM(QT)
55 typedef QLibrary PlatformBundle;
56 #elif PLATFORM(GTK)
57 typedef void* PlatformBundle;
58 #endif
59 
60 class ImmutableArray;
61 class InjectedBundleScriptWorld;
62 class WebCertificateInfo;
63 class WebFrame;
64 class WebPage;
65 class WebPageGroupProxy;
66 
67 class InjectedBundle : public APIObject {
68 public:
69     static const Type APIType = TypeBundle;
70 
create(const String & path)71     static PassRefPtr<InjectedBundle> create(const String& path)
72     {
73         return adoptRef(new InjectedBundle(path));
74     }
75     ~InjectedBundle();
76 
77     bool load(APIObject* initializationUserData);
setSandboxExtension(PassRefPtr<SandboxExtension> sandboxExtension)78     void setSandboxExtension(PassRefPtr<SandboxExtension> sandboxExtension) { m_sandboxExtension = sandboxExtension; }
79 
80     // API
81     void initializeClient(WKBundleClient*);
82     void postMessage(const String&, APIObject*);
83     void postSynchronousMessage(const String&, APIObject*, RefPtr<APIObject>& returnData);
84 #if PLATFORM(WIN)
85     void setHostAllowsAnyHTTPSCertificate(const String&);
86     void setClientCertificate(const String& host, const String& certificateSystemStoreName, const WebCertificateInfo*);
87 #endif
88 
89     // TestRunner only SPI
90     void setShouldTrackVisitedLinks(bool);
91     void removeAllVisitedLinks();
92     void activateMacFontAscentHack();
93     void overrideXSSAuditorEnabledForTestRunner(WebPageGroupProxy* pageGroup, bool enabled);
94     void setAllowUniversalAccessFromFileURLs(WebPageGroupProxy*, bool);
95     void setAllowFileAccessFromFileURLs(WebPageGroupProxy*, bool);
96     void setFrameFlatteningEnabled(WebPageGroupProxy*, bool);
97     void addOriginAccessWhitelistEntry(const String&, const String&, const String&, bool);
98     void removeOriginAccessWhitelistEntry(const String&, const String&, const String&, bool);
99     void resetOriginAccessWhitelists();
100     int numberOfPages(WebFrame*, double, double);
101     int pageNumberForElementById(WebFrame*, const String&, double, double);
102     String pageSizeAndMarginsInPixels(WebFrame*, int, int, int, int, int, int, int);
103     bool isPageBoxVisible(WebFrame*, int);
104 
105     // UserContent API
106     void addUserScript(WebPageGroupProxy*, InjectedBundleScriptWorld*, const String& source, const String& url, ImmutableArray* whitelist, ImmutableArray* blacklist, WebCore::UserScriptInjectionTime, WebCore::UserContentInjectedFrames);
107     void addUserStyleSheet(WebPageGroupProxy*, InjectedBundleScriptWorld*, const String& source, const String& url, ImmutableArray* whitelist, ImmutableArray* blacklist, WebCore::UserContentInjectedFrames);
108     void removeUserScript(WebPageGroupProxy*, InjectedBundleScriptWorld*, const String& url);
109     void removeUserStyleSheet(WebPageGroupProxy*, InjectedBundleScriptWorld*, const String& url);
110     void removeUserScripts(WebPageGroupProxy*, InjectedBundleScriptWorld*);
111     void removeUserStyleSheets(WebPageGroupProxy*, InjectedBundleScriptWorld*);
112     void removeAllUserContent(WebPageGroupProxy*);
113 
114     // Local storage API
115     void clearAllDatabases();
116     void setDatabaseQuota(uint64_t);
117 
118     // Garbage collection API
119     void garbageCollectJavaScriptObjects();
120     void garbageCollectJavaScriptObjectsOnAlternateThreadForDebugging(bool waitUntilDone);
121     size_t javaScriptObjectsCount();
122 
123     // Callback hooks
124     void didCreatePage(WebPage*);
125     void willDestroyPage(WebPage*);
126     void didInitializePageGroup(WebPageGroupProxy*);
127     void didReceiveMessage(const String&, APIObject*);
128 
129     void didReceiveMessage(CoreIPC::Connection*, CoreIPC::MessageID, CoreIPC::ArgumentDecoder*);
130 
131     static void reportException(JSContextRef, JSValueRef exception);
132 
133 private:
134     InjectedBundle(const String&);
135 
type()136     virtual Type type() const { return APIType; }
137 
138     String m_path;
139     PlatformBundle m_platformBundle; // This is leaked right now, since we never unload the bundle/module.
140 
141     RefPtr<SandboxExtension> m_sandboxExtension;
142 
143     InjectedBundleClient m_client;
144 };
145 
146 } // namespace WebKit
147 
148 #endif // InjectedBundle_h
149