GeolocationPermissions.java revision d3101b1d300f5942fdb7dfa323dc8830c4edc007
1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.webkit;
18
19import java.util.Set;
20
21/**
22 * This class is used to manage permissions for the WebView's Geolocation
23 * JavaScript API.
24 *
25 * Geolocation permissions are applied to an origin, which consists of the
26 * host, scheme and port of a URI. In order for web content to use the
27 * Geolocation API, permission must be granted for that content's origin.
28 *
29 * This class stores Geolocation permissions. An origin's permission state can
30 * be either allowed or denied. This class uses Strings to represent
31 * an origin.
32 *
33 * When an origin attempts to use the Geolocation API, but no permission state
34 * is currently set for that origin,
35 * {@link WebChromeClient#onGeolocationPermissionsShowPrompt(String,GeolocationPermissions.Callback) WebChromeClient.onGeolocationPermissionsShowPrompt()}
36 * is called. This allows the permission state to be set for that origin.
37 *
38 * The methods of this class can be used to modify and interrogate the stored
39 * Geolocation permissions at any time.
40 */
41// Within WebKit, Geolocation permissions may be applied either temporarily
42// (for the duration of the page) or permanently. This class deals only with
43// permanent permissions.
44public class GeolocationPermissions {
45    /**
46     * A callback interface used by the host application to set the Geolocation
47     * permission state for an origin.
48     */
49    public interface Callback {
50        /**
51         * Set the Geolocation permission state for the supplied origin.
52         * @param origin The origin for which permissions are set.
53         * @param allow Whether or not the origin should be allowed to use the
54         *              Geolocation API.
55         * @param retain Whether the permission should be retained beyond the
56         *               lifetime of a page currently being displayed by a
57         *               WebView.
58         */
59        public void invoke(String origin, boolean allow, boolean retain);
60    };
61
62    /**
63     * Get the singleton instance of this class.
64     * @return The singleton {@link GeolocationPermissions} instance.
65     */
66    public static GeolocationPermissions getInstance() {
67      return WebViewFactory.getProvider().getGeolocationPermissions();
68    }
69
70    /**
71     * Get the set of origins for which Geolocation permissions are stored.
72     * @param callback A {@link ValueCallback} to receive the result of this
73     *                 request. This object's
74     *                 {@link ValueCallback#onReceiveValue(T) onReceiveValue()}
75     *                 method will be invoked asynchronously with a set of
76     *                 Strings containing the origins for which Geolocation
77     *                 permissions are stored.
78     */
79    // Note that we represent the origins as strings. These are created using
80    // WebCore::SecurityOrigin::toString(). As long as all 'HTML 5 modules'
81    // (Database, Geolocation etc) do so, it's safe to match up origins based
82    // on this string.
83    public void getOrigins(ValueCallback<Set<String> > callback) {
84        // Must be a no-op for backward compatibility: see the hidden constructor for reason.
85    }
86
87    /**
88     * Get the Geolocation permission state for the specified origin.
89     * @param origin The origin for which Geolocation permission is requested.
90     * @param callback A {@link ValueCallback} to receive the result of this
91     *                 request. This object's
92     *                 {@link ValueCallback#onReceiveValue(T) onReceiveValue()}
93     *                 method will be invoked asynchronously with a boolean
94     *                 indicating whether or not the origin can use the
95     *                 Geolocation API.
96     */
97    public void getAllowed(String origin, ValueCallback<Boolean> callback) {
98        // Must be a no-op for backward compatibility: see the hidden constructor for reason.
99    }
100
101    /**
102     * Clear the Geolocation permission state for the specified origin.
103     * @param origin The origin for which Geolocation permissions are cleared.
104     */
105    public void clear(String origin) {
106        // Must be a no-op for backward compatibility: see the hidden constructor for reason.
107    }
108
109    /**
110     * Allow the specified origin to use the Geolocation API.
111     * @param origin The origin for which Geolocation API use is allowed.
112     */
113    public void allow(String origin) {
114        // Must be a no-op for backward compatibility: see the hidden constructor for reason.
115    }
116
117    /**
118     * Clear the Geolocation permission state for all origins.
119     */
120    public void clearAll() {
121        // Must be a no-op for backward compatibility: see the hidden constructor for reason.
122    }
123
124    /**
125     * This class should not be instantiated directly, applications must only use
126     * {@link #getInstance()} to obtain the instance.
127     * Note this constructor was erroneously public and published in SDK levels prior to 16, but
128     * applications using it would receive a non-functional instance of this class (there was no
129     * way to call createHandler() and createUIHandler(), so it would not work).
130     * @hide Only for use by WebViewProvider implementations
131     */
132    public GeolocationPermissions() {}
133}
134