1/*
2 * Copyright (C) 2013 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#import "WKWebProcessPlugInFrameInternal.h"
28
29#if WK_API_ENABLED
30
31#import "WKNSArray.h"
32#import "WKNSURLExtras.h"
33#import "WKWebProcessPlugInBrowserContextControllerInternal.h"
34#import "WKWebProcessPlugInHitTestResultInternal.h"
35#import "WKWebProcessPlugInNodeHandleInternal.h"
36#import "WKWebProcessPlugInScriptWorldInternal.h"
37#import "WebProcess.h"
38#import "_WKFrameHandleInternal.h"
39#import <JavaScriptCore/JSValue.h>
40#import <WebCore/CertificateInfo.h>
41#import <WebCore/IntPoint.h>
42
43using namespace WebKit;
44
45@implementation WKWebProcessPlugInFrame {
46    API::ObjectStorage<WebFrame> _frame;
47}
48
49+ (instancetype)lookUpFrameFromHandle:(_WKFrameHandle *)handle
50{
51    WebFrame* webFrame = WebProcess::shared().webFrame(handle._frameID);
52    if (!webFrame)
53        return nil;
54
55    return wrapper(*webFrame);
56}
57
58- (void)dealloc
59{
60    _frame->~WebFrame();
61    [super dealloc];
62}
63
64- (JSContext *)jsContextForWorld:(WKWebProcessPlugInScriptWorld *)world
65{
66    return [JSContext contextWithJSGlobalContextRef:_frame->jsContextForWorld(&[world _scriptWorld])];
67}
68
69- (WKWebProcessPlugInHitTestResult *)hitTest:(CGPoint)point
70{
71    RefPtr<InjectedBundleHitTestResult> hitTestResult = _frame->hitTest(WebCore::IntPoint(point));
72    return [wrapper(*hitTestResult.release().leakRef()) autorelease];
73}
74
75- (JSValue *)jsNodeForNodeHandle:(WKWebProcessPlugInNodeHandle *)nodeHandle inWorld:(WKWebProcessPlugInScriptWorld *)world
76{
77    JSValueRef valueRef = _frame->jsWrapperForWorld(&[nodeHandle _nodeHandle], &[world _scriptWorld]);
78    return [JSValue valueWithJSValueRef:valueRef inContext:[self jsContextForWorld:world]];
79}
80
81- (WKWebProcessPlugInBrowserContextController *)_browserContextController
82{
83    return wrapper(*_frame->page());
84}
85
86- (NSURL *)URL
87{
88    return [NSURL _web_URLWithWTFString:_frame->url()];
89}
90
91- (NSArray *)childFrames
92{
93    return [wrapper(*_frame->childFrames().leakRef()) autorelease];
94}
95
96- (BOOL)containsAnyFormElements
97{
98    return !!_frame->containsAnyFormElements();
99}
100
101- (_WKFrameHandle *)handle
102{
103    return [wrapper(*API::FrameHandle::create(_frame->frameID()).leakRef()) autorelease];
104}
105
106- (BOOL)_hasCustomContentProvider
107{
108    if (!_frame->isMainFrame())
109        return false;
110
111    return _frame->page()->mainFrameHasCustomContentProvider();
112}
113
114- (NSArray *)_certificateChain
115{
116    return (NSArray *)_frame->certificateInfo().certificateChain();
117}
118
119- (NSURL *)_provisionalURL
120{
121    return [NSURL _web_URLWithWTFString:_frame->provisionalURL()];
122}
123
124#pragma mark WKObject protocol implementation
125
126- (API::Object&)_apiObject
127{
128    return *_frame;
129}
130
131@end
132
133#endif // WK_API_ENABLED
134