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