GeolocationPermissions.java revision 4e584df4cee8334bc371c04a67bcd0a32e2f9480
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         * Sets the Geolocation permission state for the supplied origin.
52         *
53         * @param origin the origin for which permissions are set
54         * @param allow whether or not the origin should be allowed to use the
55         *              Geolocation API
56         * @param retain whether the permission should be retained beyond the
57         *               lifetime of a page currently being displayed by a
58         *               WebView
59         */
60        public void invoke(String origin, boolean allow, boolean retain);
61    };
62
63    /**
64     * Gets the singleton instance of this class.
65     *
66     * @return the singleton {@link GeolocationPermissions} instance
67     */
68    public static GeolocationPermissions getInstance() {
69      return WebViewFactory.getProvider().getGeolocationPermissions();
70    }
71
72    /**
73     * Gets the set of origins for which Geolocation permissions are stored.
74     *
75     * @param callback a {@link ValueCallback} to receive the result of this
76     *                 request. This object's
77     *                 {@link ValueCallback#onReceiveValue(T) onReceiveValue()}
78     *                 method will be invoked asynchronously with a set of
79     *                 Strings containing the origins for which Geolocation
80     *                 permissions are stored.
81     */
82    // Note that we represent the origins as strings. These are created using
83    // WebCore::SecurityOrigin::toString(). As long as all 'HTML 5 modules'
84    // (Database, Geolocation etc) do so, it's safe to match up origins based
85    // on this string.
86    public void getOrigins(ValueCallback<Set<String> > callback) {
87        // Must be a no-op for backward compatibility: see the hidden constructor for reason.
88    }
89
90    /**
91     * Gets the Geolocation permission state for the specified origin.
92     *
93     * @param origin the origin for which Geolocation permission is requested
94     * @param callback a {@link ValueCallback} to receive the result of this
95     *                 request. This object's
96     *                 {@link ValueCallback#onReceiveValue(T) onReceiveValue()}
97     *                 method will be invoked asynchronously with a boolean
98     *                 indicating whether or not the origin can use the
99     *                 Geolocation API.
100     */
101    public void getAllowed(String origin, ValueCallback<Boolean> callback) {
102        // Must be a no-op for backward compatibility: see the hidden constructor for reason.
103    }
104
105    /**
106     * Clears the Geolocation permission state for the specified origin.
107     *
108     * @param origin the origin for which Geolocation permissions are cleared
109     */
110    public void clear(String origin) {
111        // Must be a no-op for backward compatibility: see the hidden constructor for reason.
112    }
113
114    /**
115     * Allows the specified origin to use the Geolocation API.
116     *
117     * @param origin the origin for which Geolocation API use is allowed
118     */
119    public void allow(String origin) {
120        // Must be a no-op for backward compatibility: see the hidden constructor for reason.
121    }
122
123    /**
124     * Clears the Geolocation permission state for all origins.
125     */
126    public void clearAll() {
127        // Must be a no-op for backward compatibility: see the hidden constructor for reason.
128    }
129
130    /**
131     * This class should not be instantiated directly, applications must only use
132     * {@link #getInstance()} to obtain the instance.
133     * Note this constructor was erroneously public and published in SDK levels prior to 16, but
134     * applications using it would receive a non-functional instance of this class (there was no
135     * way to call createHandler() and createUIHandler(), so it would not work).
136     * @hide Only for use by WebViewProvider implementations
137     */
138    public GeolocationPermissions() {}
139}
140