1 // Copyright 2007, Google Inc.
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
6 // 1. Redistributions of source code must retain the above copyright notice,
7 // this list of conditions and the following disclaimer.
8 // 2. Redistributions in binary form must reproduce the above copyright notice,
9 // this list of conditions and the following disclaimer in the documentation
10 // and/or other materials provided with the distribution.
11 // 3. Neither the name of Google Inc. nor the names of its contributors may be
12 // used to endorse or promote products derived from this software without
13 // specific prior written permission.
15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef GEARS_BASE_COMMON_PERMISSIONS_DB_H__
27 #define GEARS_BASE_COMMON_PERMISSIONS_DB_H__
30 #include "gears/base/common/name_value_table.h"
31 #include "gears/base/common/security_model.h"
32 #include "gears/base/common/shortcut_table.h"
33 #include "gears/base/common/sqlite_wrapper.h"
36 // This class provides an API to manage the capabilities of pages within
37 // Gears. Right now, it is a baby API and only manages a single capability:
38 // the ability to access Gears at all. But we anticipate it growing into a
39 // bigger API, which would manage more fine-grained capabilities, such as the
40 // ability to store more than 1MB on disk, etc.
42 // TODO(aa): Think about factoring some of the commonalities between this class
43 // and WebCacheDB into a common base class.
46 // The allowable values of a permission.
47 enum PermissionValue
{
48 PERMISSION_DEFAULT
= 0,
49 PERMISSION_ALLOWED
= 1,
53 // Gets a thread-specific PermissionsDB instance.
54 static PermissionsDB
*GetDB();
56 // Sets the Gears access level for a given SecurityOrigin.
57 void SetCanAccessGears(const SecurityOrigin
&origin
, PermissionValue value
);
59 // Gets the Gears access level for a given SecurityOrigin.
60 PermissionsDB::PermissionValue
GetCanAccessGears(const SecurityOrigin
&origin
);
62 // Returns true if the origin has permission to use Gears.
63 bool IsOriginAllowed(const SecurityOrigin
&origin
) {
64 return GetCanAccessGears(origin
) == PERMISSION_ALLOWED
;
67 // Get all the origins with a specific value.
68 bool GetOriginsByValue(PermissionValue value
,
69 std::vector
<SecurityOrigin
> *result
);
71 // Attempts to enable Gears for a worker with the given SecurityOrigin.
72 bool EnableGearsForWorker(const SecurityOrigin
&origin
);
74 // The key used to cache instances of PermissionsDB in ThreadLocals.
75 static const std::string kThreadLocalKey
;
77 // Add (or overwrite) a shortcut for origin/name, with appUrl,
78 // icoUrl, and msg as data.
79 bool SetShortcut(const SecurityOrigin
&origin
, const char16
*name
,
80 const char16
*app_url
,
81 const std::vector
<std::string16
> &icon_urls
,
84 // Get the set of origins which have shortcuts.
85 bool GetOriginsWithShortcuts(std::vector
<SecurityOrigin
> *result
);
87 // Get the set of named shortcuts for a specific origin.
88 bool GetOriginShortcuts(const SecurityOrigin
&origin
,
89 std::vector
<std::string16
> *names
);
91 // Get the data for a specific shortcut.
92 bool GetShortcut(const SecurityOrigin
&origin
, const char16
*name
,
93 std::string16
*app_url
,
94 std::vector
<std::string16
> *icon_urls
,
97 // Delete a specific shortcut.
98 bool DeleteShortcut(const SecurityOrigin
&origin
, const char16
*name
);
100 // Delete all shortcuts for an origin.
101 bool DeleteShortcuts(const SecurityOrigin
&origin
);
104 // Private constructor, callers must use GetDB().
107 // Initializes the database. Must be called before other methods.
110 // Creates or upgrades the database to kCurrentVersion.
111 bool CreateOrUpgradeDatabase();
113 // Creates the database's schema.
114 bool CreateDatabase();
116 // Schema upgrade functions. Higher-numbered functions call
117 // lower-numbered functions as appropriate.
118 bool UpgradeToVersion4();
119 bool UpgradeToVersion3();
120 bool UpgradeToVersion2();
122 // Destructor function called by ThreadLocals to dispose of a thread-specific
123 // DB instance when a thread dies.
124 static void DestroyDB(void *context
);
126 // Database we use to store capabilities information.
129 // Version metadata for the capabilities database.
130 NameValueTable version_table_
;
132 // Maps origins to ability to access Gears.
133 NameValueTable access_table_
;
135 // Shortcuts origins have defined.
136 ShortcutTable shortcut_table_
;
138 DISALLOW_EVIL_CONSTRUCTORS(PermissionsDB
);
142 #endif // GEARS_BASE_COMMON_PERMISSIONS_DB_H__