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