1fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block/*
2fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block * Copyright 2009, The Android Open Source Project
3fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block *
4fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block * Redistribution and use in source and binary forms, with or without
5fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block * modification, are permitted provided that the following conditions
6fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block * are met:
7fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block *  * Redistributions of source code must retain the above copyright
8fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block *    notice, this list of conditions and the following disclaimer.
9fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block *  * Redistributions in binary form must reproduce the above copyright
10fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block *    notice, this list of conditions and the following disclaimer in the
11fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block *    documentation and/or other materials provided with the distribution.
12fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block *
13fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
14fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
163298a7d84b96ade73c6b170671711a8f2792ae59Steve Block * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block */
25fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block
26fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block#ifndef GeolocationPermissions_h
27fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block#define GeolocationPermissions_h
28fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block
29084a2f00a6b30ce87ec8a8eabbea3ac9cbfa6e7eSteve Block#include <PlatformString.h>
30084a2f00a6b30ce87ec8a8eabbea3ac9cbfa6e7eSteve Block#include <Timer.h>
319ca5793315e248418a353cc1443f3803ae0adfe6Steve Block#include <wtf/HashMap.h>
329ca5793315e248418a353cc1443f3803ae0adfe6Steve Block#include <wtf/HashSet.h>
339ca5793315e248418a353cc1443f3803ae0adfe6Steve Block#include <wtf/RefCounted.h>
349ca5793315e248418a353cc1443f3803ae0adfe6Steve Block#include <wtf/Vector.h>
35dd7ee22af4f03672fa823324323466054f19a054Iain Merrick#include <wtf/text/StringHash.h>
36fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block
37fe55045391147883a38ed161b0c80f4e356bd9e0Steve Blocknamespace WebCore {
38fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block    class Frame;
39fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block    class Geolocation;
4009face10395095a906046a91e971351b4432093eSteve Block    class SQLiteDatabase;
41fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block}
42fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block
43fe55045391147883a38ed161b0c80f4e356bd9e0Steve Blocknamespace android {
44fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block
45fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block    class WebViewCore;
46fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block
47a6166fb9925974a5cf9d8f94318527d0003a7be7Steve Block    // The GeolocationPermissions class manages Geolocation permissions for the
48a6166fb9925974a5cf9d8f94318527d0003a7be7Steve Block    // browser. Permissions are managed on a per-origin basis, as required by
49a6166fb9925974a5cf9d8f94318527d0003a7be7Steve Block    // the Geolocation spec - http://dev.w3.org/geo/api/spec-source.html. An
50a6166fb9925974a5cf9d8f94318527d0003a7be7Steve Block    // origin specifies the scheme, host and port of particular frame.  An
51a6166fb9925974a5cf9d8f94318527d0003a7be7Steve Block    // origin is represented here as a string, using the output of
52a6166fb9925974a5cf9d8f94318527d0003a7be7Steve Block    // WebCore::SecurityOrigin::toString.
53a6166fb9925974a5cf9d8f94318527d0003a7be7Steve Block    //
54fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block    // Each instance handles permissions for a given main frame. The class
55fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block    // enforces the following policy.
56fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block    // - Non-remembered permissions last for the dureation of the main frame.
57fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block    // - Remembered permissions last indefinitely.
58fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block    // - All permissions are shared between child frames of a main frame.
59fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block    // - Only remembered permissions are shared between main frames.
60fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block    // - Remembered permissions are made available for use in the browser
61fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block    //   settings menu.
62fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block    class GeolocationPermissions : public RefCounted<GeolocationPermissions> {
63fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block      public:
64fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block        // Creates the GeolocationPermissions object to manage permissions for
65084a2f00a6b30ce87ec8a8eabbea3ac9cbfa6e7eSteve Block        // the WebView.
66084a2f00a6b30ce87ec8a8eabbea3ac9cbfa6e7eSteve Block        GeolocationPermissions(WebViewCore* webViewCore);
67fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block        virtual ~GeolocationPermissions();
68fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block
69fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block        // Queries the permission state for the specified frame. If the
70fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block        // permission state has not yet been set, prompts the user. Once the
71fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block        // permission state has been determined, asynchronously calls back to
72fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block        // the Geolocation objects in all frames in this WebView that are from
73fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block        // the same origin as the requesting frame.
74fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block        void queryPermissionState(WebCore::Frame* frame);
753429efc308249172d26d6c9edb5e2d36c1c5855aSteve Block        void cancelPermissionStateQuery(WebCore::Frame*);
76fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block
77a6166fb9925974a5cf9d8f94318527d0003a7be7Steve Block        // Provides this object with a permission state set by the user. The
78a6166fb9925974a5cf9d8f94318527d0003a7be7Steve Block        // permission is specified by 'allow' and applied to 'origin'. If
79a6166fb9925974a5cf9d8f94318527d0003a7be7Steve Block        // 'remember' is set, the permission state is remembered permanently.
80a6166fb9925974a5cf9d8f94318527d0003a7be7Steve Block        // The new permission state is recorded and will trigger callbacks to
81fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block        // geolocation objects as described above. If any other permission
82fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block        // requests are queued, the next is started.
8345b64f60af3cbfa8c78388e35e9b669a0c284bdcBen Murdoch        void providePermissionState(WTF::String origin, bool allow, bool remember);
84fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block
85fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block        // Clears the temporary permission state and any pending requests. Used
86fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block        // when the main frame is refreshed or navigated to a new URL.
87fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block        void resetTemporaryPermissionStates();
88fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block
89fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block        // Static methods for use from Java. These are used to interact with the
907aecfeda60289f788a0a934bd7b277b93f3b0679Steve Block        // browser settings menu and to update the permanent permissions when
917aecfeda60289f788a0a934bd7b277b93f3b0679Steve Block        // system settings are changed.
92a6166fb9925974a5cf9d8f94318527d0003a7be7Steve Block        // Gets the list of all origins for which permanent permissions are
93a6166fb9925974a5cf9d8f94318527d0003a7be7Steve Block        // recorded.
9445b64f60af3cbfa8c78388e35e9b669a0c284bdcBen Murdoch        typedef HashSet<WTF::String> OriginSet;
95fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block        static OriginSet getOrigins();
96a6166fb9925974a5cf9d8f94318527d0003a7be7Steve Block        // Gets whether the specified origin is allowed.
9745b64f60af3cbfa8c78388e35e9b669a0c284bdcBen Murdoch        static bool getAllowed(WTF::String origin);
98a6166fb9925974a5cf9d8f94318527d0003a7be7Steve Block        // Clears the permission state for the specified origin.
9945b64f60af3cbfa8c78388e35e9b669a0c284bdcBen Murdoch        static void clear(WTF::String origin);
100a6166fb9925974a5cf9d8f94318527d0003a7be7Steve Block        // Sets the permission state for the specified origin to allowed.
10145b64f60af3cbfa8c78388e35e9b669a0c284bdcBen Murdoch        static void allow(WTF::String origin);
102a6166fb9925974a5cf9d8f94318527d0003a7be7Steve Block        // Clears the permission state for all origins.
103fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block        static void clearAll();
104a6166fb9925974a5cf9d8f94318527d0003a7be7Steve Block        // Sets whether the GeolocationPermissions object should always deny
105a6166fb9925974a5cf9d8f94318527d0003a7be7Steve Block        // permission requests, irrespective of previously recorded permission
106a6166fb9925974a5cf9d8f94318527d0003a7be7Steve Block        // states.
107a7f4953b4f6bd15510aa67ffe11c85130aedbd26Steve Block        static void setAlwaysDeny(bool deny);
108fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block
10945b64f60af3cbfa8c78388e35e9b669a0c284bdcBen Murdoch        static void setDatabasePath(WTF::String path);
11009face10395095a906046a91e971351b4432093eSteve Block        static bool openDatabase(WebCore::SQLiteDatabase*);
11124016ee3f73b86a5865b073953213022f22e2e2fSteve Block
112e7430864058b3c222b4b0468cf8812a9a733818bSteve Block        // Saves the permanent permissions to the DB if required.
113e7430864058b3c222b4b0468cf8812a9a733818bSteve Block        static void maybeStorePermanentPermissions();
114e7430864058b3c222b4b0468cf8812a9a733818bSteve Block
115fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block      private:
116a6166fb9925974a5cf9d8f94318527d0003a7be7Steve Block        // Records the permission state for the specified origin and whether
117a6166fb9925974a5cf9d8f94318527d0003a7be7Steve Block        // this should be remembered.
11845b64f60af3cbfa8c78388e35e9b669a0c284bdcBen Murdoch        void recordPermissionState(WTF::String origin, bool allow, bool remember);
119fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block
120fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block        // Used to make an asynchronous callback to the Geolocation objects.
12145b64f60af3cbfa8c78388e35e9b669a0c284bdcBen Murdoch        void makeAsynchronousCallbackToGeolocation(WTF::String origin, bool allow);
122fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block        void timerFired(WebCore::Timer<GeolocationPermissions>* timer);
123fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block
124fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block        // Calls back to the Geolocation objects in all frames from the
125fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block        // specified origin. There may be no such objects, as the frames using
126fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block        // Geolocation from the specified origin may no longer use Geolocation,
127fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block        // or may have been navigated to a different origin..
12845b64f60af3cbfa8c78388e35e9b669a0c284bdcBen Murdoch        void maybeCallbackFrames(WTF::String origin, bool allow);
129fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block
130fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block        // Cancels pending permission requests for the specified origin in
131fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block        // other main frames (ie browser tabs). This is used when the user
132fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block        // specifies permission to be remembered.
13345b64f60af3cbfa8c78388e35e9b669a0c284bdcBen Murdoch        static void cancelPendingRequestsInOtherTabs(WTF::String origin);
13445b64f60af3cbfa8c78388e35e9b669a0c284bdcBen Murdoch        void cancelPendingRequests(WTF::String origin);
135fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block
13624016ee3f73b86a5865b073953213022f22e2e2fSteve Block        static void maybeLoadPermanentPermissions();
13724016ee3f73b86a5865b073953213022f22e2e2fSteve Block
13845b64f60af3cbfa8c78388e35e9b669a0c284bdcBen Murdoch        const WTF::String& nextOriginInQueue();
1393429efc308249172d26d6c9edb5e2d36c1c5855aSteve Block
140fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block        WebViewCore* m_webViewCore;
1413429efc308249172d26d6c9edb5e2d36c1c5855aSteve Block        // A vector of the origins queued to make a permission request.
1423429efc308249172d26d6c9edb5e2d36c1c5855aSteve Block        // The first in the vector is the origin currently making the request.
14345b64f60af3cbfa8c78388e35e9b669a0c284bdcBen Murdoch        typedef Vector<WTF::String> OriginVector;
144fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block        OriginVector m_queuedOrigins;
1453429efc308249172d26d6c9edb5e2d36c1c5855aSteve Block        // A map from a queued origin to the set of frames that have requested
1463429efc308249172d26d6c9edb5e2d36c1c5855aSteve Block        // permission for that origin.
1473429efc308249172d26d6c9edb5e2d36c1c5855aSteve Block        typedef HashSet<WebCore::Frame*> FrameSet;
14845b64f60af3cbfa8c78388e35e9b669a0c284bdcBen Murdoch        typedef HashMap<WTF::String, FrameSet> OriginToFramesMap;
1493429efc308249172d26d6c9edb5e2d36c1c5855aSteve Block        OriginToFramesMap m_queuedOriginsToFramesMap;
150fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block
15145b64f60af3cbfa8c78388e35e9b669a0c284bdcBen Murdoch        typedef WTF::HashMap<WTF::String, bool> PermissionsMap;
152fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block        PermissionsMap m_temporaryPermissions;
153fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block        static PermissionsMap s_permanentPermissions;
154fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block
155fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block        typedef WTF::Vector<GeolocationPermissions*> GeolocationPermissionsVector;
156fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block        static GeolocationPermissionsVector s_instances;
157fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block
158fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block        WebCore::Timer<GeolocationPermissions> m_timer;
159fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block
160fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block        struct CallbackData {
16145b64f60af3cbfa8c78388e35e9b669a0c284bdcBen Murdoch            WTF::String origin;
162fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block            bool allow;
163fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block        };
164fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block        CallbackData m_callbackData;
165fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block
166a7f4953b4f6bd15510aa67ffe11c85130aedbd26Steve Block        static bool s_alwaysDeny;
16724016ee3f73b86a5865b073953213022f22e2e2fSteve Block
16824016ee3f73b86a5865b073953213022f22e2e2fSteve Block        static bool s_permanentPermissionsLoaded;
1694419d5c4673a8a2bae80b3db23b048bdce5b9dafSteve Block        static bool s_permanentPermissionsModified;
17045b64f60af3cbfa8c78388e35e9b669a0c284bdcBen Murdoch        static WTF::String s_databasePath;
171fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block    };
172fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block
173fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block}  // namespace android
174fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block
175fe55045391147883a38ed161b0c80f4e356bd9e0Steve Block#endif
176