Home | History | Annotate | Download | only in WebCoreSupport
      1 /*
      2  * Copyright 2012, The Android Open Source Project
      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  *  * Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  *  * 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 THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #include "config.h"
     27 #include "GeolocationManager.h"
     28 
     29 #include "GeolocationClientImpl.h"
     30 #include "WebViewCore.h"
     31 
     32 #include <Frame.h>
     33 #include <GeolocationError.h>
     34 #include <GeolocationPosition.h>
     35 #include <JNIHelp.h>
     36 #include <Page.h>
     37 
     38 using WebCore::GeolocationClient;
     39 using WebCore::GeolocationClientMock;
     40 
     41 namespace android {
     42 
     43 GeolocationManager::GeolocationManager(WebViewCore* webViewCore)
     44     : m_useMock(false)
     45     , m_webViewCore(webViewCore)
     46 {
     47 }
     48 
     49 GeolocationClient* GeolocationManager::client() const
     50 {
     51     if (m_useMock)
     52         return mockClient();
     53     return realClient();
     54 }
     55 
     56 void GeolocationManager::suspendRealClient()
     57 {
     58     // Don't create the real client if it's not present.
     59     if (m_realClient)
     60         m_realClient->suspend();
     61 }
     62 
     63 void GeolocationManager::resumeRealClient()
     64 {
     65     // Don't create the real client if it's not present.
     66     if (m_realClient)
     67         m_realClient->resume();
     68 }
     69 
     70 void GeolocationManager::resetRealClientTemporaryPermissionStates()
     71 {
     72     // Don't create the real client if it's not present.
     73     if (m_realClient)
     74         m_realClient->resetTemporaryPermissionStates();
     75 }
     76 
     77 void GeolocationManager::provideRealClientPermissionState(WTF::String origin, bool allow, bool remember)
     78 {
     79     // Don't create the real client if it's not present.
     80     if (m_realClient)
     81         m_realClient->providePermissionState(origin, allow, remember);
     82 }
     83 
     84 void GeolocationManager::setUseMock()
     85 {
     86     m_useMock = true;
     87     m_mockClient.clear();
     88 }
     89 
     90 void GeolocationManager::setMockPosition(PassRefPtr<WebCore::GeolocationPosition> position)
     91 {
     92     ASSERT(m_useMock);
     93     mockClient()->setPosition(position);
     94 }
     95 
     96 void GeolocationManager::setMockError(PassRefPtr<WebCore::GeolocationError> error)
     97 {
     98     ASSERT(m_useMock);
     99     mockClient()->setError(error);
    100 }
    101 
    102 void GeolocationManager::setMockPermission(bool allowed)
    103 {
    104     ASSERT(m_useMock);
    105     mockClient()->setPermission(allowed);
    106 }
    107 
    108 GeolocationClientImpl* GeolocationManager::realClient() const
    109 {
    110     if (!m_realClient)
    111         m_realClient.set(new GeolocationClientImpl(m_webViewCore));
    112     return m_realClient.get();
    113 }
    114 
    115 GeolocationClientMock* GeolocationManager::mockClient() const
    116 {
    117     ASSERT(m_useMock);
    118     if (!m_mockClient) {
    119         m_mockClient.set(new GeolocationClientMock);
    120         m_mockClient->setController(m_webViewCore->mainFrame()->page()->geolocationController());
    121     }
    122     return m_mockClient.get();
    123 }
    124 
    125 } // namespace android
    126