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 APPLE COMPUTER, INC. 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 // We must include this before before HashMap.h, as it provides specalizations 31 // for String hash types instantiated there. 32 #include "StringHash.h" 33 #include "HashMap.h" 34 #include "HashSet.h" 35 #include "Timer.h" 36 #include "Vector.h" 37 #include "wtf/RefCounted.h" 38 39 namespace WebCore { 40 class Frame; 41 class Geolocation; 42 } 43 44 namespace android { 45 46 class WebViewCore; 47 48 // The GeolocationPermissions class manages permissions for the browser. 49 // Each instance handles permissions for a given main frame. The class 50 // enforces the following policy. 51 // - Non-remembered permissions last for the dureation of the main frame. 52 // - Remembered permissions last indefinitely. 53 // - All permissions are shared between child frames of a main frame. 54 // - Only remembered permissions are shared between main frames. 55 // - Remembered permissions are made available for use in the browser 56 // settings menu. 57 class GeolocationPermissions : public RefCounted<GeolocationPermissions> { 58 public: 59 // Creates the GeolocationPermissions object to manage permissions for 60 // the specified main frame (i.e. tab). The WebViewCore is used to 61 // communicate with the browser to display UI. 62 GeolocationPermissions(WebViewCore* webViewCore, WebCore::Frame* mainFrame); 63 virtual ~GeolocationPermissions(); 64 65 // Queries the permission state for the specified frame. If the 66 // permission state has not yet been set, prompts the user. Once the 67 // permission state has been determined, asynchronously calls back to 68 // the Geolocation objects in all frames in this WebView that are from 69 // the same origin as the requesting frame. 70 void queryPermissionState(WebCore::Frame* frame); 71 72 // Provides this object the given permission state from the user. The 73 // new permission state is recorded and will trigger callbacks to 74 // geolocation objects as described above. If any other permission 75 // requests are queued, the next is started. 76 void providePermissionState(WebCore::String origin, bool allow, bool remember); 77 78 // Clears the temporary permission state and any pending requests. Used 79 // when the main frame is refreshed or navigated to a new URL. 80 void resetTemporaryPermissionStates(); 81 82 // Static methods for use from Java. These are used to interact with the 83 // browser settings menu and to update the permanent permissions when 84 // system settings are changed. 85 typedef HashSet<WebCore::String> OriginSet; 86 static OriginSet getOrigins(); 87 static bool getAllowed(WebCore::String origin); 88 static void clear(WebCore::String origin); 89 static void allow(WebCore::String origin); 90 static void clearAll(); 91 static void setAlwaysDeny(bool deny); 92 93 static void setDatabasePath(WebCore::String path); 94 95 // Saves the permanent permissions to the DB if required. 96 static void maybeStorePermanentPermissions(); 97 98 private: 99 // Records the permission state for the specified origin. 100 void recordPermissionState(WebCore::String origin, bool allow, bool remember); 101 102 // Used to make an asynchronous callback to the Geolocation objects. 103 void makeAsynchronousCallbackToGeolocation(WebCore::String origin, bool allow); 104 void timerFired(WebCore::Timer<GeolocationPermissions>* timer); 105 106 // Calls back to the Geolocation objects in all frames from the 107 // specified origin. There may be no such objects, as the frames using 108 // Geolocation from the specified origin may no longer use Geolocation, 109 // or may have been navigated to a different origin.. 110 void maybeCallbackFrames(WebCore::String origin, bool allow); 111 112 // Cancels pending permission requests for the specified origin in 113 // other main frames (ie browser tabs). This is used when the user 114 // specifies permission to be remembered. 115 static void cancelPendingRequestsInOtherTabs(WebCore::String origin); 116 void cancelPendingRequests(WebCore::String origin); 117 118 static void maybeLoadPermanentPermissions(); 119 120 WebViewCore* m_webViewCore; 121 WebCore::Frame* m_mainFrame; 122 WebCore::String m_originInProgress; 123 typedef Vector<WebCore::String> OriginVector; 124 OriginVector m_queuedOrigins; 125 126 typedef WTF::HashMap<WebCore::String, bool> PermissionsMap; 127 PermissionsMap m_temporaryPermissions; 128 static PermissionsMap s_permanentPermissions; 129 130 typedef WTF::Vector<GeolocationPermissions*> GeolocationPermissionsVector; 131 static GeolocationPermissionsVector s_instances; 132 133 WebCore::Timer<GeolocationPermissions> m_timer; 134 135 struct CallbackData { 136 WebCore::String origin; 137 bool allow; 138 }; 139 CallbackData m_callbackData; 140 141 static bool s_alwaysDeny; 142 143 static bool s_permanentPermissionsLoaded; 144 static WebCore::String s_databasePath; 145 }; 146 147 } // namespace android 148 149 #endif 150