| OLD | NEW |
| 1 // Copyright 2007, Google Inc. | 1 // Copyright 2007, Google Inc. |
| 2 // | 2 // |
| 3 // Redistribution and use in source and binary forms, with or without | 3 // Redistribution and use in source and binary forms, with or without |
| 4 // modification, are permitted provided that the following conditions are met: | 4 // modification, are permitted provided that the following conditions are met: |
| 5 // | 5 // |
| 6 // 1. Redistributions of source code must retain the above copyright notice, | 6 // 1. Redistributions of source code must retain the above copyright notice, |
| 7 // this list of conditions and the following disclaimer. | 7 // this list of conditions and the following disclaimer. |
| 8 // 2. Redistributions in binary form must reproduce the above copyright notice, | 8 // 2. Redistributions in binary form must reproduce the above copyright notice, |
| 9 // this list of conditions and the following disclaimer in the documentation | 9 // this list of conditions and the following disclaimer in the documentation |
| 10 // and/or other materials provided with the distribution. | 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 | 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 | 12 // used to endorse or promote products derived from this software without |
| 13 // specific prior written permission. | 13 // specific prior written permission. |
| 14 // | 14 // |
| 15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED | 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 | 16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | 17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | 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, | 21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | 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 | 23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | 25 |
| 26 #ifndef GEARS_BASE_COMMON_PERMISSIONS_DB_H__ | 26 #ifndef GEARS_BASE_COMMON_PERMISSIONS_DB_H__ |
| 27 #define GEARS_BASE_COMMON_PERMISSIONS_DB_H__ | 27 #define GEARS_BASE_COMMON_PERMISSIONS_DB_H__ |
| 28 | 28 |
| 29 #include <map> | 29 #include <map> |
| 30 #include "gears/base/common/database_name_table.h" | 30 #include "gears/base/common/database_name_table.h" |
| 31 #include "gears/base/common/database2_versions_table.h" |
| 31 #include "gears/base/common/name_value_table.h" | 32 #include "gears/base/common/name_value_table.h" |
| 32 #include "gears/base/common/security_model.h" | 33 #include "gears/base/common/security_model.h" |
| 33 #include "gears/base/common/shortcut_table.h" | 34 #include "gears/base/common/shortcut_table.h" |
| 34 #include "gears/base/common/sqlite_wrapper.h" | 35 #include "gears/base/common/sqlite_wrapper.h" |
| 35 | 36 |
| 36 // TODO(cprince): Consider merging this with PermissionsDB::PermissionValue. | 37 // TODO(cprince): Consider merging this with PermissionsDB::PermissionValue. |
| 37 // But note the _TEMPORARY values hae no meaning to PermissionsDB. | 38 // But note the _TEMPORARY values hae no meaning to PermissionsDB. |
| 38 enum PermissionState { | 39 enum PermissionState { |
| 39 NOT_SET, | 40 NOT_SET, |
| 40 ALLOWED_PERMANENTLY, | 41 ALLOWED_PERMANENTLY, |
| 41 ALLOWED_TEMPORARILY, | 42 ALLOWED_TEMPORARILY, |
| 42 DENIED_PERMANENTLY, | 43 DENIED_PERMANENTLY, |
| 43 DENIED_TEMPORARILY | 44 DENIED_TEMPORARILY |
| 44 }; | 45 }; |
| 45 | 46 |
| 46 // This class provides an API to manage the capabilities of pages within | 47 // This class provides an API to manage the capabilities of pages within |
| 47 // Gears. Right now, it is a baby API and only manages a single capability: | 48 // Gears. Right now, it is a baby API and only manages a single capability: |
| 48 // the ability to access Gears at all. But we anticipate it growing into a | 49 // the ability to access Gears at all. But we anticipate it growing into a |
| 49 // bigger API, which would manage more fine-grained capabilities, such as the | 50 // bigger API, which would manage more fine-grained capabilities, such as the |
| 50 // ability to store more than 1MB on disk, etc. | 51 // ability to store more than 1MB on disk, etc. |
| 51 // | 52 // |
| 52 // TODO(aa): Think about factoring some of the commonalities between this class | 53 // TODO(aa): Think about factoring some of the commonalities between this class |
| 53 // and WebCacheDB into a common base class. | 54 // and WebCacheDB into a common base class. |
| 54 // | 55 // |
| 55 // TODO(cprince): Should rename this class and file to SettingsDB, as we store | 56 // TODO(cprince): Should rename this class and file to SettingsDB, as we store |
| 56 // shortcut info here too. (But be careful to preserve the on-disk filename.) | 57 // shortcut info here too. (But be careful to preserve the on-disk filename.) |
| 57 class PermissionsDB { | 58 class PermissionsDB { |
| 58 public: | 59 public: |
| 59 // The allowable values of a permission. | 60 // The allowable values of a permission. |
| 60 enum PermissionValue { | 61 enum PermissionValue { |
| 61 PERMISSION_NOT_SET = 0, // origin has no persisted value | 62 PERMISSION_NOT_SET = 0, // origin has no persisted value |
| 62 PERMISSION_ALLOWED = 1, | 63 PERMISSION_ALLOWED = 1, |
| 63 PERMISSION_DENIED = 2 | 64 PERMISSION_DENIED = 2 |
| 64 }; | 65 }; |
| 65 | 66 |
| 66 // Gets a thread-specific PermissionsDB instance. | 67 // Gets a thread-specific PermissionsDB instance. |
| 67 static PermissionsDB *GetDB(); | 68 static PermissionsDB *GetDB(); |
| 68 | 69 |
| 69 // Sets the Gears access level for a given SecurityOrigin. | 70 // Sets the Gears access level for a given SecurityOrigin. |
| 70 void SetCanAccessGears(const SecurityOrigin &origin, PermissionValue value); | 71 void SetCanAccessGears(const SecurityOrigin &origin, PermissionValue value); |
| 71 | 72 |
| 72 // Gets the Gears access level for a given SecurityOrigin. | 73 // Gets the Gears access level for a given SecurityOrigin. |
| 73 PermissionsDB::PermissionValue GetCanAccessGears(const SecurityOrigin &origin)
; | 74 PermissionsDB::PermissionValue GetCanAccessGears(const SecurityOrigin &origin)
; |
| 74 | 75 |
| 75 // Returns true if the origin has permission to use Gears. | 76 // Returns true if the origin has permission to use Gears. |
| 76 bool IsOriginAllowed(const SecurityOrigin &origin) { | 77 bool IsOriginAllowed(const SecurityOrigin &origin) { |
| 77 // SAFARI-TEMP | 78 // SAFARI-TEMP |
| 78 #ifdef BROWSER_WEBKIT | 79 #ifdef BROWSER_WEBKIT |
| 79 return true; | 80 return true; |
| 80 #else | 81 #else |
| 81 return GetCanAccessGears(origin) == PERMISSION_ALLOWED; | 82 return GetCanAccessGears(origin) == PERMISSION_ALLOWED; |
| 82 #endif | 83 #endif |
| 83 } | 84 } |
| 84 | 85 |
| 85 // Gets all the origins with a specific value. | 86 // Gets all the origins with a specific value. |
| 86 bool GetOriginsByValue(PermissionValue value, | 87 bool GetOriginsByValue(PermissionValue value, |
| 87 std::vector<SecurityOrigin> *result); | 88 std::vector<SecurityOrigin> *result); |
| 88 | 89 |
| 89 // Attempts to enable Gears for a worker with the given SecurityOrigin. | 90 // Attempts to enable Gears for a worker with the given SecurityOrigin. |
| 90 bool EnableGearsForWorker(const SecurityOrigin &origin); | 91 bool EnableGearsForWorker(const SecurityOrigin &origin); |
| 91 | 92 |
| 92 // The key used to cache instances of PermissionsDB in ThreadLocals. | 93 // The key used to cache instances of PermissionsDB in ThreadLocals. |
| 93 static const std::string kThreadLocalKey; | 94 static const std::string kThreadLocalKey; |
| 94 | 95 |
| 95 // Adds (or overwrites) a shortcut for origin/name, with appUrl, | 96 // Adds (or overwrites) a shortcut for origin/name, with appUrl, |
| 96 // iconUrl, msg as data, and whether or not to allow creation of this | 97 // iconUrl, msg as data, and whether or not to allow creation of this |
| 97 // shortcut. | 98 // shortcut. |
| 98 bool SetShortcut(const SecurityOrigin &origin, const char16 *name, | 99 bool SetShortcut(const SecurityOrigin &origin, const char16 *name, |
| 99 const char16 *app_url, | 100 const char16 *app_url, |
| 100 const char16 *icon16x16_url, | 101 const char16 *icon16x16_url, |
| 101 const char16 *icon32x32_url, | 102 const char16 *icon32x32_url, |
| 102 const char16 *icon48x48_url, | 103 const char16 *icon48x48_url, |
| 103 const char16 *icon128x128_url, | 104 const char16 *icon128x128_url, |
| 104 const char16 *msg, | 105 const char16 *msg, |
| 105 const bool allow); | 106 const bool allow); |
| 106 | 107 |
| 107 // Gets the set of origins which have shortcuts. | 108 // Gets the set of origins which have shortcuts. |
| 108 bool GetOriginsWithShortcuts(std::vector<SecurityOrigin> *result); | 109 bool GetOriginsWithShortcuts(std::vector<SecurityOrigin> *result); |
| 109 | 110 |
| 110 // Gets the set of named shortcuts for a specific origin. | 111 // Gets the set of named shortcuts for a specific origin. |
| 111 bool GetOriginShortcuts(const SecurityOrigin &origin, | 112 bool GetOriginShortcuts(const SecurityOrigin &origin, |
| 112 std::vector<std::string16> *names); | 113 std::vector<std::string16> *names); |
| 113 | 114 |
| 114 // Gets the data for a specific shortcut. | 115 // Gets the data for a specific shortcut. |
| 115 bool GetShortcut(const SecurityOrigin &origin, const char16 *name, | 116 bool GetShortcut(const SecurityOrigin &origin, const char16 *name, |
| 116 std::string16 *app_url, | 117 std::string16 *app_url, |
| 117 std::string16 *icon16x16_url, | 118 std::string16 *icon16x16_url, |
| 118 std::string16 *icon32x32_url, | 119 std::string16 *icon32x32_url, |
| 119 std::string16 *icon48x48_url, | 120 std::string16 *icon48x48_url, |
| 120 std::string16 *icon128x128_url, | 121 std::string16 *icon128x128_url, |
| 121 std::string16 *msg, | 122 std::string16 *msg, |
| 122 bool *allow); | 123 bool *allow); |
| 123 | 124 |
| 124 // Deletes a specific shortcut. | 125 // Deletes a specific shortcut. |
| 125 bool DeleteShortcut(const SecurityOrigin &origin, const char16 *name); | 126 bool DeleteShortcut(const SecurityOrigin &origin, const char16 *name); |
| 126 | 127 |
| 127 // Deletes all shortcuts for an origin. | 128 // Deletes all shortcuts for an origin. |
| 128 bool DeleteShortcuts(const SecurityOrigin &origin); | 129 bool DeleteShortcuts(const SecurityOrigin &origin); |
| 129 | 130 |
| 130 // For a given database_name, fill basename with the name of the | 131 // For a given database_name, fill basename with the name of the |
| 131 // file to use in origin's directory, and returns true if | 132 // file to use in origin's directory, and returns true if |
| 132 // successful. | 133 // successful. |
| 133 bool GetDatabaseBasename(const SecurityOrigin &origin, | 134 bool GetDatabaseBasename(const SecurityOrigin &origin, |
| 134 const char16 *database_name, | 135 const char16 *database_name, |
| 135 std::string16 *basename); | 136 std::string16 *basename); |
| 136 | 137 |
| 137 // Mark the given database basename corrupt so that future calls to | 138 // Mark the given database basename corrupt so that future calls to |
| 138 // GetDatabaseBasename will no longer return it. The basename is | 139 // GetDatabaseBasename will no longer return it. The basename is |
| 139 // required because another thread of control could have already | 140 // required because another thread of control could have already |
| 140 // invalidated the database for the origin. | 141 // invalidated the database for the origin. |
| 141 bool MarkDatabaseCorrupt(const SecurityOrigin &origin, | 142 bool MarkDatabaseCorrupt(const SecurityOrigin &origin, |
| 142 const char16 *database_name, | 143 const char16 *database_name, |
| 143 const char16 *basename); | 144 const char16 *basename); |
| 145 |
| 146 // Database2 API: get current database version for a given database at the |
| 147 // specified origin |
| 148 bool GetDatabase2Version(const SecurityOrigin &origin, |
| 149 const std::string16 &name, |
| 150 std::string16 *version, |
| 151 bool *found); |
| 152 |
| 153 // Database2 API: set or create a version for a given database at the |
| 154 // specified origin |
| 155 bool SetDatabase2Version(const SecurityOrigin &origin, |
| 156 const std::string16 &name, |
| 157 const std::string16 &version); |
| 144 | 158 |
| 145 private: | 159 private: |
| 146 // Private constructor, callers must use GetDB(). | 160 // Private constructor, callers must use GetDB(). |
| 147 PermissionsDB(); | 161 PermissionsDB(); |
| 148 | 162 |
| 149 // Initializes the database. Must be called before other methods. | 163 // Initializes the database. Must be called before other methods. |
| 150 bool Init(); | 164 bool Init(); |
| 151 | 165 |
| 152 // Creates or upgrades the database to kCurrentVersion. | 166 // Creates or upgrades the database to kCurrentVersion. |
| 153 bool CreateOrUpgradeDatabase(); | 167 bool CreateOrUpgradeDatabase(); |
| 154 | 168 |
| 155 // Creates the database's schema. | 169 // Creates the database's schema. |
| 156 bool CreateDatabase(); | 170 bool CreateDatabase(); |
| 157 | 171 |
| 158 // Schema upgrade functions. Higher-numbered functions call | 172 // Schema upgrade functions. Higher-numbered functions call |
| 159 // lower-numbered functions as appropriate. | 173 // lower-numbered functions as appropriate. |
| 160 bool UpgradeToVersion7(); | 174 bool UpgradeToVersion7(); |
| 161 bool UpgradeToVersion6(); | 175 bool UpgradeToVersion6(); |
| 162 bool UpgradeToVersion5(); | 176 bool UpgradeToVersion5(); |
| 163 bool UpgradeToVersion4(); | 177 bool UpgradeToVersion4(); |
| 164 bool UpgradeToVersion3(); | 178 bool UpgradeToVersion3(); |
| 165 bool UpgradeToVersion2(); | 179 bool UpgradeToVersion2(); |
| 166 | 180 |
| 167 // Destructor function called by ThreadLocals to dispose of a thread-specific | 181 // Destructor function called by ThreadLocals to dispose of a thread-specific |
| 168 // DB instance when a thread dies. | 182 // DB instance when a thread dies. |
| 169 static void DestroyDB(void *context); | 183 static void DestroyDB(void *context); |
| 170 | 184 |
| 171 // Database we use to store capabilities information. | 185 // Database we use to store capabilities information. |
| 172 SQLDatabase db_; | 186 SQLDatabase db_; |
| 173 | 187 |
| 174 // Version metadata for the capabilities database. | 188 // Version metadata for the capabilities database. |
| 175 NameValueTable version_table_; | 189 NameValueTable version_table_; |
| 176 | 190 |
| 177 // Maps origins to ability to access Gears. | 191 // Maps origins to ability to access Gears. |
| 178 NameValueTable access_table_; | 192 NameValueTable access_table_; |
| 179 | 193 |
| 180 // Shortcuts origins have defined. | 194 // Shortcuts origins have defined. |
| 181 ShortcutTable shortcut_table_; | 195 ShortcutTable shortcut_table_; |
| 182 | 196 |
| 183 DatabaseNameTable database_name_table_; | 197 DatabaseNameTable database_name_table_; |
| 198 |
| 199 // database2 metadata |
| 200 Database2VersionsTable database2_versions_table_; |
| 184 | 201 |
| 185 DISALLOW_EVIL_CONSTRUCTORS(PermissionsDB); | 202 DISALLOW_EVIL_CONSTRUCTORS(PermissionsDB); |
| 186 DECL_SINGLE_THREAD | 203 DECL_SINGLE_THREAD |
| 187 }; | 204 }; |
| 188 | 205 |
| 189 #endif // GEARS_BASE_COMMON_PERMISSIONS_DB_H__ | 206 #endif // GEARS_BASE_COMMON_PERMISSIONS_DB_H__ |
| OLD | NEW |