| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ | 5 #ifndef CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ |
| 6 #define CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ | 6 #define CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ |
| 7 | 7 |
| 8 #include <cstddef> | 8 #include <cstddef> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "chrome/browser/predictors/glowplug_key_value_table.h" |
| 16 #include "chrome/browser/predictors/predictor_table_base.h" | 17 #include "chrome/browser/predictors/predictor_table_base.h" |
| 17 #include "chrome/browser/predictors/resource_prefetch_common.h" | 18 #include "chrome/browser/predictors/resource_prefetch_common.h" |
| 18 #include "chrome/browser/predictors/resource_prefetch_predictor.pb.h" | 19 #include "chrome/browser/predictors/resource_prefetch_predictor.pb.h" |
| 19 #include "components/precache/core/proto/precache.pb.h" | 20 #include "components/precache/core/proto/precache.pb.h" |
| 20 | 21 |
| 21 namespace sql { | |
| 22 class Statement; | |
| 23 } | |
| 24 | |
| 25 namespace predictors { | 22 namespace predictors { |
| 26 | 23 |
| 27 // Interface for database tables used by the ResourcePrefetchPredictor. | 24 // Interface for database tables used by the ResourcePrefetchPredictor. |
| 28 // All methods except the constructor and destructor need to be called on the DB | 25 // All methods except the constructor and destructor need to be called on the DB |
| 29 // thread. | 26 // thread. |
| 30 // | 27 // |
| 31 // Currently manages: | 28 // Currently manages: |
| 32 // - UrlResourceTable - resources per Urls. | 29 // - UrlResourceTable - key: url, value: PrefetchData |
| 33 // - UrlRedirectTable - redirects per Urls. | 30 // - UrlRedirectTable - key: url, value: RedirectData |
| 34 // - HostResourceTable - resources per host. | 31 // - HostResourceTable - key: host, value: PrefetchData |
| 35 // - HostRedirectTable - redirects per host. | 32 // - HostRedirectTable - key: host, value: RedirectData |
| 33 // - ManifestTable - key: host with stripped "www." prefix, |
| 34 // value: precache::PrecacheManifest |
| 35 // - OriginTable - key: host, value: OriginData |
| 36 class ResourcePrefetchPredictorTables : public PredictorTableBase { | 36 class ResourcePrefetchPredictorTables : public PredictorTableBase { |
| 37 public: | 37 public: |
| 38 typedef std::map<std::string, PrefetchData> PrefetchDataMap; | 38 typedef std::map<std::string, PrefetchData> PrefetchDataMap; |
| 39 typedef std::map<std::string, RedirectData> RedirectDataMap; | 39 typedef std::map<std::string, RedirectData> RedirectDataMap; |
| 40 typedef std::map<std::string, precache::PrecacheManifest> ManifestDataMap; | 40 typedef std::map<std::string, precache::PrecacheManifest> ManifestDataMap; |
| 41 typedef std::map<std::string, OriginData> OriginDataMap; | 41 typedef std::map<std::string, OriginData> OriginDataMap; |
| 42 | 42 |
| 43 // Returns data for all Urls and Hosts. | 43 // Returns data for all Urls and Hosts. |
| 44 virtual void GetAllData(PrefetchDataMap* url_data_map, | 44 virtual void GetAllData(PrefetchDataMap* url_data_map, |
| 45 PrefetchDataMap* host_data_map, | 45 PrefetchDataMap* host_data_map, |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 // The maximum length of the string that can be stored in the DB. | 124 // The maximum length of the string that can be stored in the DB. |
| 125 static constexpr size_t kMaxStringLength = 1024; | 125 static constexpr size_t kMaxStringLength = 1024; |
| 126 | 126 |
| 127 protected: | 127 protected: |
| 128 // Protected for testing. Use PredictorDatabase::resource_prefetch_tables() | 128 // Protected for testing. Use PredictorDatabase::resource_prefetch_tables() |
| 129 // instead of this constructor. | 129 // instead of this constructor. |
| 130 ResourcePrefetchPredictorTables(); | 130 ResourcePrefetchPredictorTables(); |
| 131 ~ResourcePrefetchPredictorTables() override; | 131 ~ResourcePrefetchPredictorTables() override; |
| 132 | 132 |
| 133 private: | 133 private: |
| 134 // Represents the type of information that is stored in prefetch database. | |
| 135 enum class PrefetchDataType { RESOURCE, REDIRECT, MANIFEST, ORIGIN }; | |
| 136 | |
| 137 enum class TableOperationType { INSERT, REMOVE }; | |
| 138 | |
| 139 friend class PredictorDatabaseInternal; | 134 friend class PredictorDatabaseInternal; |
| 140 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, | 135 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, |
| 141 DatabaseVersionIsSet); | 136 DatabaseVersionIsSet); |
| 142 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, | 137 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, |
| 143 DatabaseIsResetWhenIncompatible); | 138 DatabaseIsResetWhenIncompatible); |
| 144 | 139 |
| 145 // Database version. Always increment it when any change is made to the data | 140 // Database version. Always increment it when any change is made to the data |
| 146 // schema (including the .proto). | 141 // schema (including the .proto). |
| 147 static constexpr int kDatabaseVersion = 8; | 142 static constexpr int kDatabaseVersion = 8; |
| 148 | 143 |
| 149 // Helper functions below help perform functions on the Url and host table | |
| 150 // using the same code. | |
| 151 void GetAllResourceDataHelper(PrefetchKeyType key_type, | |
| 152 PrefetchDataMap* data_map); | |
| 153 void GetAllRedirectDataHelper(PrefetchKeyType key_type, | |
| 154 RedirectDataMap* redirect_map); | |
| 155 void GetAllManifestDataHelper(ManifestDataMap* manifest_map); | |
| 156 void GetAllOriginDataHelper(OriginDataMap* manifest_map); | |
| 157 | |
| 158 void UpdateDataHelper(PrefetchKeyType key_type, | |
| 159 PrefetchDataType data_type, | |
| 160 const std::string& key, | |
| 161 const google::protobuf::MessageLite& data); | |
| 162 void DeleteDataHelper(PrefetchKeyType key_type, | |
| 163 PrefetchDataType data_type, | |
| 164 const std::vector<std::string>& keys); | |
| 165 | |
| 166 // PredictorTableBase: | 144 // PredictorTableBase: |
| 167 void CreateTableIfNonExistent() override; | 145 void CreateTableIfNonExistent() override; |
| 168 void LogDatabaseStats() override; | 146 void LogDatabaseStats() override; |
| 169 | 147 |
| 170 static bool DropTablesIfOutdated(sql::Connection* db); | 148 static bool DropTablesIfOutdated(sql::Connection* db); |
| 171 static int GetDatabaseVersion(sql::Connection* db); | 149 static int GetDatabaseVersion(sql::Connection* db); |
| 172 static bool SetDatabaseVersion(sql::Connection* db, int version); | 150 static bool SetDatabaseVersion(sql::Connection* db, int version); |
| 173 | 151 |
| 174 // Helper to return cached Statements. | 152 std::unique_ptr<GlowplugKeyValueTable<PrefetchData>> url_resource_table_; |
| 175 std::unique_ptr<sql::Statement> GetTableUpdateStatement( | 153 std::unique_ptr<GlowplugKeyValueTable<RedirectData>> url_redirect_table_; |
| 176 PrefetchKeyType key_type, | 154 std::unique_ptr<GlowplugKeyValueTable<PrefetchData>> host_resource_table_; |
| 177 PrefetchDataType data_type, | 155 std::unique_ptr<GlowplugKeyValueTable<RedirectData>> host_redirect_table_; |
| 178 TableOperationType op_type); | 156 std::unique_ptr<GlowplugKeyValueTable<precache::PrecacheManifest>> |
| 179 | 157 manifest_table_; |
| 180 static const char* GetTableName(PrefetchKeyType key_type, | 158 std::unique_ptr<GlowplugKeyValueTable<OriginData>> origin_table_; |
| 181 PrefetchDataType data_type); | |
| 182 | 159 |
| 183 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTables); | 160 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTables); |
| 184 }; | 161 }; |
| 185 | 162 |
| 186 } // namespace predictors | 163 } // namespace predictors |
| 187 | 164 |
| 188 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ | 165 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ |
| OLD | NEW |