~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebKit2/WebProcess/InjectedBundle/API/mac/WKWebProcessPlugIn.mm

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2012 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
#import "config.h"
 
27
 
 
28
#if defined(__LP64__) && defined(__clang__)
 
29
 
 
30
#import "WKWebProcessPlugIn.h"
 
31
#import "WKWebProcessPlugInInternal.h"
 
32
 
 
33
#import "InjectedBundle.h"
 
34
#import "WKConnectionInternal.h"
 
35
#import "WKBundle.h"
 
36
#import "WKBundleAPICast.h"
 
37
#import "WKRetainPtr.h"
 
38
#import "WKWebProcessPlugInBrowserContextControllerInternal.h"
 
39
#import <wtf/RetainPtr.h>
 
40
 
 
41
typedef HashMap<WKBundlePageRef, RetainPtr<WKWebProcessPlugInBrowserContextController *> > BundlePageWrapperCache;
 
42
 
 
43
@interface WKWebProcessPlugInController () {
 
44
    RetainPtr<id<WKWebProcessPlugIn> > _principalClassInstance;
 
45
    WKRetainPtr<WKBundleRef> _bundleRef;
 
46
    BundlePageWrapperCache _bundlePageWrapperCache;
 
47
    RetainPtr<WKConnection *> _connectionWrapper;
 
48
}
 
49
@end
 
50
 
 
51
@implementation WKWebProcessPlugInController (Internal)
 
52
 
 
53
static void didCreatePage(WKBundleRef bundle, WKBundlePageRef page, const void* clientInfo)
 
54
{
 
55
    WKWebProcessPlugInController *plugInController = (WKWebProcessPlugInController *)clientInfo;
 
56
    id<WKWebProcessPlugIn> principalClassInstance = plugInController->_principalClassInstance.get();
 
57
 
 
58
    if ([principalClassInstance respondsToSelector:@selector(webProcessPlugIn:didCreateBrowserContextController:)]) {
 
59
        ASSERT(!plugInController->_bundlePageWrapperCache.contains(page));
 
60
 
 
61
        WKWebProcessPlugInBrowserContextController* browserContextController = [[WKWebProcessPlugInBrowserContextController alloc] _initWithBundlePageRef:page];
 
62
        plugInController->_bundlePageWrapperCache.set(page, browserContextController);
 
63
 
 
64
        [principalClassInstance webProcessPlugIn:plugInController didCreateBrowserContextController:browserContextController];
 
65
    }
 
66
}
 
67
 
 
68
static void willDestroyPage(WKBundleRef bundle, WKBundlePageRef page, const void* clientInfo)
 
69
{
 
70
    WKWebProcessPlugInController *plugInController = (WKWebProcessPlugInController *)clientInfo;
 
71
    id<WKWebProcessPlugIn> principalClassInstance = plugInController->_principalClassInstance.get();
 
72
 
 
73
    // If we never added the bundle page to the cache, which can happen if webProcessPlugIn:didCreateBrowserContextController: is not implemented,
 
74
    // there is no reason to call webProcessPlugIn:willDestroyBrowserContextController:, so don't.
 
75
    BundlePageWrapperCache::iterator it = plugInController->_bundlePageWrapperCache.find(page);
 
76
    if (it == plugInController->_bundlePageWrapperCache.end()) {
 
77
        ASSERT(![principalClassInstance respondsToSelector:@selector(webProcessPlugIn:didCreateBrowserContextController:)]);
 
78
        return;
 
79
    }
 
80
 
 
81
    if ([principalClassInstance respondsToSelector:@selector(webProcessPlugIn:willDestroyBrowserContextController:)])
 
82
        [principalClassInstance webProcessPlugIn:plugInController willDestroyBrowserContextController:it->value.get()];
 
83
 
 
84
    plugInController->_bundlePageWrapperCache.remove(it);
 
85
}
 
86
 
 
87
static void setUpBundleClient(WKWebProcessPlugInController *plugInController, WKBundleRef bundleRef)
 
88
{
 
89
    WKBundleClient bundleClient;
 
90
    memset(&bundleClient, 0, sizeof(bundleClient));
 
91
 
 
92
    bundleClient.version = kWKBundleClientCurrentVersion;
 
93
    bundleClient.clientInfo = plugInController;
 
94
    bundleClient.didCreatePage = didCreatePage;
 
95
    bundleClient.willDestroyPage = willDestroyPage;
 
96
 
 
97
    WKBundleSetClient(bundleRef, &bundleClient);
 
98
}
 
99
 
 
100
static WKWebProcessPlugInController *sharedInstance;
 
101
 
 
102
+ (WKWebProcessPlugInController *)_shared
 
103
{
 
104
    ASSERT_WITH_MESSAGE(sharedInstance, "+[WKWebProcessPlugIn _shared] called without first initializing it.");
 
105
    return sharedInstance;
 
106
}
 
107
 
 
108
- (id)_initWithPrincipalClassInstance:(id<WKWebProcessPlugIn>)principalClassInstance bundleRef:(WKBundleRef)bundleRef
 
109
{
 
110
    self = [super init];
 
111
    if (!self)
 
112
        return nil;
 
113
 
 
114
    _principalClassInstance = principalClassInstance;
 
115
    _bundleRef = bundleRef;
 
116
    _connectionWrapper = adoptNS([[WKConnection alloc] _initWithConnectionRef:WKBundleGetApplicationConnection(_bundleRef.get())]);
 
117
 
 
118
    ASSERT_WITH_MESSAGE(!sharedInstance, "WKWebProcessPlugInController initialized multiple times.");
 
119
    sharedInstance = self;
 
120
 
 
121
    setUpBundleClient(self, bundleRef);
 
122
 
 
123
    return self;
 
124
}
 
125
 
 
126
- (WKWebProcessPlugInBrowserContextController *)_browserContextControllerForBundlePageRef:(WKBundlePageRef)pageRef
 
127
{
 
128
    ASSERT(_bundlePageWrapperCache.contains(pageRef));
 
129
    return _bundlePageWrapperCache.get(pageRef).get();
 
130
}
 
131
 
 
132
@end
 
133
 
 
134
@implementation WKWebProcessPlugInController
 
135
 
 
136
- (WKConnection *)connection
 
137
{
 
138
    return _connectionWrapper.get();
 
139
}
 
140
 
 
141
@end
 
142
 
 
143
#endif // defined(__LP64__) && defined(__clang__)