• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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 
17 package com.android.browser;
18 
19 import android.util.Log;
20 
21 import java.util.HashMap;
22 import java.util.Iterator;
23 
24 /**
25  * The permission mechanism works the following way:
26  *
27  * PermissionType allows to define a type of permission
28  *   (e.g. localStorage/locationData), storing a name and a set of
29  *   resource ids corresponding to the GUI resources.
30  *
31  * Permission defines an actual permission instance, with a type and a value.
32  *
33  * OriginPermissions holds an origin with a set of Permission objects
34  */
35 class GearsPermissions {
36 
37   private static final String TAG = "GearsPermissions";
38 
39   /**
40    * Defines a type of permission
41    *
42    * Store the permission's name (used in the json result)
43    * Graphically, each permission is a label followed by two radio buttons.
44    * We store the resources ids here.
45    */
46   public static class PermissionType {
47     public static final int PERMISSION_NOT_SET = 0;
48     public static final int PERMISSION_ALLOWED = 1;
49     public static final int PERMISSION_DENIED = 2;
50 
51     String mName;
52     int mTitleRsc;
53     int mSubtitleOnRsc;
54     int mSubtitleOffRsc;
55 
PermissionType(String name)56     PermissionType(String name) {
57       mName = name;
58     }
59 
setResources(int titleRsc, int subtitleOnRsc, int subtitleOffRsc)60     public void setResources(int titleRsc,
61         int subtitleOnRsc, int subtitleOffRsc) {
62       mTitleRsc = titleRsc;
63       mSubtitleOnRsc = subtitleOnRsc;
64       mSubtitleOffRsc = subtitleOffRsc;
65     }
66 
getName()67     public String getName() {
68       return mName;
69     }
70 
getTitleRsc()71     public int getTitleRsc() {
72       return mTitleRsc;
73     }
74 
getSubtitleOnRsc()75     public int getSubtitleOnRsc() {
76       return mSubtitleOnRsc;
77     }
78 
getSubtitleOffRsc()79     public int getSubtitleOffRsc() {
80       return mSubtitleOffRsc;
81     }
82 
83   }
84 
85   /**
86    * Simple class to store an instance of a permission
87    *
88    * i.e. a permission type and a value
89    * Value can be either PERMISSION_NOT_SET,
90    * PERMISSION_ALLOWED or PERMISSION_DENIED
91    * (defined in PermissionType).
92    */
93   public static class Permission {
94     PermissionType mType;
95     int mValue;
96 
Permission(PermissionType type, int value)97     Permission(PermissionType type, int value) {
98       mType = type;
99       mValue = value;
100     }
101 
Permission(PermissionType type)102     Permission(PermissionType type) {
103       mType = type;
104       mValue = 0;
105     }
106 
getType()107     public PermissionType getType() {
108       return mType;
109     }
110 
setValue(int value)111     public void setValue(int value) {
112       mValue = value;
113     }
114 
getValue()115     public int getValue() {
116       return mValue;
117     }
118   }
119 
120   /**
121    * Interface used by the GearsNativeDialog implementation
122    * to listen to changes in the permissions.
123    */
124   public interface PermissionsChangesListener {
setPermission(PermissionType type, int perm)125     public boolean setPermission(PermissionType type, int perm);
126   }
127 
128   /**
129    * Holds the model for an origin -- each origin has a set of
130    * permissions.
131    */
132   public static class OriginPermissions {
133     HashMap<PermissionType, Permission> mPermissions;
134     String mOrigin;
135     public static PermissionsChangesListener mListener;
136 
setListener(PermissionsChangesListener listener)137     public static void setListener(PermissionsChangesListener listener) {
138       mListener = listener;
139     }
140 
OriginPermissions(String anOrigin)141     OriginPermissions(String anOrigin) {
142       mOrigin = anOrigin;
143       mPermissions = new HashMap<PermissionType, Permission>();
144     }
145 
OriginPermissions(OriginPermissions perms)146     OriginPermissions(OriginPermissions perms) {
147       mOrigin = perms.getOrigin();
148       mPermissions = new HashMap<PermissionType, Permission>();
149       HashMap<PermissionType, Permission> permissions = perms.getPermissions();
150       Iterator<PermissionType> iterator = permissions.keySet().iterator();
151       while (iterator.hasNext()) {
152         Permission permission = permissions.get(iterator.next());
153         int value = permission.getValue();
154         setPermission(permission.getType(), value);
155       }
156     }
157 
getOrigin()158     public String getOrigin() {
159       return mOrigin;
160     }
161 
getPermissions()162     public HashMap<PermissionType, Permission> getPermissions() {
163       return mPermissions;
164     }
165 
getPermission(PermissionType type)166     public int getPermission(PermissionType type) {
167       return mPermissions.get(type).getValue();
168     }
169 
setPermission(PermissionType type, int perm)170     public void setPermission(PermissionType type, int perm) {
171       if (mPermissions.get(type) == null) {
172         Permission permission = new Permission(type, perm);
173         mPermissions.put(type, permission);
174         return;
175       }
176 
177       if (mListener != null) {
178         mListener.setPermission(type, perm);
179       }
180 
181       mPermissions.get(type).setValue(perm);
182     }
183 
print()184     public void print() {
185       Log.v(TAG, "Permissions for " + mOrigin);
186       Iterator<PermissionType> iterator = mPermissions.keySet().iterator();
187       while (iterator.hasNext()) {
188         Permission permission = mPermissions.get(iterator.next());
189         String name = permission.getType().getName();
190         int value = permission.getValue();
191         Log.v(TAG, "  " + name + ": " + value);
192       }
193     }
194   }
195 
196 }
197