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
42namespace CoreIPC {
43    class ArgumentDecoder;
44    class Connection;
45    class MessageID;
46}
47
48namespace WebKit {
49
50#if PLATFORM(MAC)
51typedef CFBundleRef PlatformBundle;
52#elif PLATFORM(WIN)
53typedef HMODULE PlatformBundle;
54#elif PLATFORM(QT)
55typedef QLibrary PlatformBundle;
56#elif PLATFORM(GTK)
57typedef void* PlatformBundle;
58#endif
59
60class ImmutableArray;
61class InjectedBundleScriptWorld;
62class WebCertificateInfo;
63class WebFrame;
64class WebPage;
65class WebPageGroupProxy;
66
67class InjectedBundle : public APIObject {
68public:
69    static const Type APIType = TypeBundle;
70
71    static PassRefPtr<InjectedBundle> create(const String& path)
72    {
73        return adoptRef(new InjectedBundle(path));
74    }
75    ~InjectedBundle();
76
77    bool load(APIObject* initializationUserData);
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 overrideAllowUniversalAccessFromFileURLsForTestRunner(WebPageGroupProxy*, bool);
95    void setAllowFileAccessFromFileURLs(WebPageGroupProxy*, bool);
96    int numberOfPages(WebFrame*, double, double);
97    int pageNumberForElementById(WebFrame*, const String&, double, double);
98    String pageSizeAndMarginsInPixels(WebFrame*, int, int, int, int, int, int, int);
99    bool isPageBoxVisible(WebFrame*, int);
100
101    // UserContent API
102    void addUserScript(WebPageGroupProxy*, InjectedBundleScriptWorld*, const String& source, const String& url, ImmutableArray* whitelist, ImmutableArray* blacklist, WebCore::UserScriptInjectionTime, WebCore::UserContentInjectedFrames);
103    void addUserStyleSheet(WebPageGroupProxy*, InjectedBundleScriptWorld*, const String& source, const String& url, ImmutableArray* whitelist, ImmutableArray* blacklist, WebCore::UserContentInjectedFrames);
104    void removeUserScript(WebPageGroupProxy*, InjectedBundleScriptWorld*, const String& url);
105    void removeUserStyleSheet(WebPageGroupProxy*, InjectedBundleScriptWorld*, const String& url);
106    void removeUserScripts(WebPageGroupProxy*, InjectedBundleScriptWorld*);
107    void removeUserStyleSheets(WebPageGroupProxy*, InjectedBundleScriptWorld*);
108    void removeAllUserContent(WebPageGroupProxy*);
109
110    // Local storage API
111    void clearAllDatabases();
112    void setDatabaseQuota(uint64_t);
113
114    // Garbage collection API
115    void garbageCollectJavaScriptObjects();
116    void garbageCollectJavaScriptObjectsOnAlternateThreadForDebugging(bool waitUntilDone);
117    size_t javaScriptObjectsCount();
118
119    // Callback hooks
120    void didCreatePage(WebPage*);
121    void willDestroyPage(WebPage*);
122    void didInitializePageGroup(WebPageGroupProxy*);
123    void didReceiveMessage(const String&, APIObject*);
124
125    void didReceiveMessage(CoreIPC::Connection*, CoreIPC::MessageID, CoreIPC::ArgumentDecoder*);
126
127    static void reportException(JSContextRef, JSValueRef exception);
128
129private:
130    InjectedBundle(const String&);
131
132    virtual Type type() const { return APIType; }
133
134    String m_path;
135    PlatformBundle m_platformBundle; // This is leaked right now, since we never unload the bundle/module.
136
137    RefPtr<SandboxExtension> m_sandboxExtension;
138
139    InjectedBundleClient m_client;
140};
141
142} // namespace WebKit
143
144#endif // InjectedBundle_h
145