1 /* 2 * Copyright (C) 2009 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 "WebGeolocationClient.h" 27 28 #import "WebDelegateImplementationCaching.h" 29 #import "WebFrameInternal.h" 30 #import "WebGeolocationPositionInternal.h" 31 #import "WebSecurityOriginInternal.h" 32 #import "WebUIDelegatePrivate.h" 33 #import "WebViewInternal.h" 34 #import <WebCore/BlockExceptions.h> 35 #import <WebCore/Frame.h> 36 #import <WebCore/Geolocation.h> 37 38 using namespace WebCore; 39 40 @interface WebGeolocationPolicyListener : NSObject <WebGeolocationPolicyListener> 41 { 42 RefPtr<Geolocation> _geolocation; 43 } 44 - (id)initWithGeolocation:(Geolocation*)geolocation; 45 @end 46 47 WebGeolocationClient::WebGeolocationClient(WebView *webView) 48 : m_webView(webView) 49 { 50 } 51 52 void WebGeolocationClient::geolocationDestroyed() 53 { 54 delete this; 55 } 56 57 void WebGeolocationClient::startUpdating() 58 { 59 [[m_webView _geolocationProvider] registerWebView:m_webView]; 60 } 61 62 void WebGeolocationClient::stopUpdating() 63 { 64 [[m_webView _geolocationProvider] unregisterWebView:m_webView]; 65 } 66 67 void WebGeolocationClient::requestPermission(Geolocation* geolocation) 68 { 69 BEGIN_BLOCK_OBJC_EXCEPTIONS; 70 71 SEL selector = @selector(webView:decidePolicyForGeolocationRequestFromOrigin:frame:listener:); 72 if (![[m_webView UIDelegate] respondsToSelector:selector]) { 73 geolocation->setIsAllowed(false); 74 return; 75 } 76 77 Frame *frame = geolocation->frame(); 78 WebSecurityOrigin *webOrigin = [[WebSecurityOrigin alloc] _initWithWebCoreSecurityOrigin:frame->document()->securityOrigin()]; 79 WebGeolocationPolicyListener* listener = [[WebGeolocationPolicyListener alloc] initWithGeolocation:geolocation]; 80 81 CallUIDelegate(m_webView, selector, webOrigin, kit(frame), listener); 82 83 [webOrigin release]; 84 [listener release]; 85 86 END_BLOCK_OBJC_EXCEPTIONS; 87 } 88 89 GeolocationPosition* WebGeolocationClient::lastPosition() 90 { 91 #if ENABLE(CLIENT_BASED_GEOLOCATION) 92 return core([[m_webView _geolocationProvider] lastPosition]); 93 #else 94 return 0; 95 #endif 96 } 97 98 @implementation WebGeolocationPolicyListener 99 100 - (id)initWithGeolocation:(Geolocation*)geolocation 101 { 102 if (!(self = [super init])) 103 return nil; 104 _geolocation = geolocation; 105 return self; 106 } 107 108 - (void)allow 109 { 110 _geolocation->setIsAllowed(true); 111 } 112 113 - (void)deny 114 { 115 _geolocation->setIsAllowed(false); 116 } 117 118 @end 119 120