Home | History | Annotate | Download | only in WebCoreSupport
      1 /*
      2  * Copyright 2009, 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 #ifndef GeolocationPermissions_h
     27 #define GeolocationPermissions_h
     28 
     29 #include <PlatformString.h>
     30 #include <Timer.h>
     31 #include <wtf/HashMap.h>
     32 #include <wtf/HashSet.h>
     33 #include <wtf/RefCounted.h>
     34 #include <wtf/Vector.h>
     35 #include <wtf/text/StringHash.h>
     36 
     37 namespace WebCore {
     38     class Frame;
     39     class Geolocation;
     40     class SQLiteDatabase;
     41 }
     42 
     43 namespace android {
     44 
     45     class WebViewCore;
     46 
     47     // The GeolocationPermissions class manages Geolocation permissions for the
     48     // browser. Permissions are managed on a per-origin basis, as required by
     49     // the Geolocation spec - http://dev.w3.org/geo/api/spec-source.html. An
     50     // origin specifies the scheme, host and port of particular frame.  An
     51     // origin is represented here as a string, using the output of
     52     // WebCore::SecurityOrigin::toString.
     53     //
     54     // Each instance handles permissions for a given main frame. The class
     55     // enforces the following policy.
     56     // - Non-remembered permissions last for the dureation of the main frame.
     57     // - Remembered permissions last indefinitely.
     58     // - All permissions are shared between child frames of a main frame.
     59     // - Only remembered permissions are shared between main frames.
     60     // - Remembered permissions are made available for use in the browser
     61     //   settings menu.
     62     class GeolocationPermissions : public RefCounted<GeolocationPermissions> {
     63       public:
     64         // Creates the GeolocationPermissions object to manage permissions for
     65         // the WebView.
     66         GeolocationPermissions(WebViewCore* webViewCore);
     67         virtual ~GeolocationPermissions();
     68 
     69         // Queries the permission state for the specified frame. If the
     70         // permission state has not yet been set, prompts the user. Once the
     71         // permission state has been determined, asynchronously calls back to
     72         // the Geolocation objects in all frames in this WebView that are from
     73         // the same origin as the requesting frame.
     74         void queryPermissionState(WebCore::Frame* frame);
     75         void cancelPermissionStateQuery(WebCore::Frame*);
     76 
     77         // Provides this object with a permission state set by the user. The
     78         // permission is specified by 'allow' and applied to 'origin'. If
     79         // 'remember' is set, the permission state is remembered permanently.
     80         // The new permission state is recorded and will trigger callbacks to
     81         // geolocation objects as described above. If any other permission
     82         // requests are queued, the next is started.
     83         void providePermissionState(WTF::String origin, bool allow, bool remember);
     84 
     85         // Clears the temporary permission state and any pending requests. Used
     86         // when the main frame is refreshed or navigated to a new URL.
     87         void resetTemporaryPermissionStates();
     88 
     89         // Static methods for use from Java. These are used to interact with the
     90         // browser settings menu and to update the permanent permissions when
     91         // system settings are changed.
     92         // Gets the list of all origins for which permanent permissions are
     93         // recorded.
     94         typedef HashSet<WTF::String> OriginSet;
     95         static OriginSet getOrigins();
     96         // Gets whether the specified origin is allowed.
     97         static bool getAllowed(WTF::String origin);
     98         // Clears the permission state for the specified origin.
     99         static void clear(WTF::String origin);
    100         // Sets the permission state for the specified origin to allowed.
    101         static void allow(WTF::String origin);
    102         // Clears the permission state for all origins.
    103         static void clearAll();
    104         // Sets whether the GeolocationPermissions object should always deny
    105         // permission requests, irrespective of previously recorded permission
    106         // states.
    107         static void setAlwaysDeny(bool deny);
    108 
    109         static void setDatabasePath(WTF::String path);
    110         static bool openDatabase(WebCore::SQLiteDatabase*);
    111 
    112         // Saves the permanent permissions to the DB if required.
    113         static void maybeStorePermanentPermissions();
    114 
    115       private:
    116         // Records the permission state for the specified origin and whether
    117         // this should be remembered.
    118         void recordPermissionState(WTF::String origin, bool allow, bool remember);
    119 
    120         // Used to make an asynchronous callback to the Geolocation objects.
    121         void makeAsynchronousCallbackToGeolocation(WTF::String origin, bool allow);
    122         void timerFired(WebCore::Timer<GeolocationPermissions>* timer);
    123 
    124         // Calls back to the Geolocation objects in all frames from the
    125         // specified origin. There may be no such objects, as the frames using
    126         // Geolocation from the specified origin may no longer use Geolocation,
    127         // or may have been navigated to a different origin..
    128         void maybeCallbackFrames(WTF::String origin, bool allow);
    129 
    130         // Cancels pending permission requests for the specified origin in
    131         // other main frames (ie browser tabs). This is used when the user
    132         // specifies permission to be remembered.
    133         static void cancelPendingRequestsInOtherTabs(WTF::String origin);
    134         void cancelPendingRequests(WTF::String origin);
    135 
    136         static void maybeLoadPermanentPermissions();
    137 
    138         const WTF::String& nextOriginInQueue();
    139 
    140         WebViewCore* m_webViewCore;
    141         // A vector of the origins queued to make a permission request.
    142         // The first in the vector is the origin currently making the request.
    143         typedef Vector<WTF::String> OriginVector;
    144         OriginVector m_queuedOrigins;
    145         // A map from a queued origin to the set of frames that have requested
    146         // permission for that origin.
    147         typedef HashSet<WebCore::Frame*> FrameSet;
    148         typedef HashMap<WTF::String, FrameSet> OriginToFramesMap;
    149         OriginToFramesMap m_queuedOriginsToFramesMap;
    150 
    151         typedef WTF::HashMap<WTF::String, bool> PermissionsMap;
    152         PermissionsMap m_temporaryPermissions;
    153         static PermissionsMap s_permanentPermissions;
    154 
    155         typedef WTF::Vector<GeolocationPermissions*> GeolocationPermissionsVector;
    156         static GeolocationPermissionsVector s_instances;
    157 
    158         WebCore::Timer<GeolocationPermissions> m_timer;
    159 
    160         struct CallbackData {
    161             WTF::String origin;
    162             bool allow;
    163         };
    164         CallbackData m_callbackData;
    165 
    166         static bool s_alwaysDeny;
    167 
    168         static bool s_permanentPermissionsLoaded;
    169         static bool s_permanentPermissionsModified;
    170         static WTF::String s_databasePath;
    171     };
    172 
    173 }  // namespace android
    174 
    175 #endif
    176