OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/history/core/browser/typed_url_sync_metadata_database.h" | |
6 | |
7 #include "base/strings/string_number_conversions.h" | |
8 #include "sql/statement.h" | |
9 | |
10 namespace history { | |
11 | |
12 namespace { | |
13 | |
14 const char kTypedURLModelTypeStateKey[] = "typed_url_model_type_state"; | |
brettw
2017/03/20 17:27:06
Can you provide documentation somewhere (maybe her
Gang Wu
2017/03/20 21:34:47
Done.
| |
15 | |
16 } // namespace | |
17 | |
18 // Description of database table: | |
19 // | |
20 // typed_url_sync_metadata | |
21 // storage_key the rowid of an entry in urls table, used by service to | |
22 // look up native data with sync metadata records. | |
23 // value Serialize sync EntityMetadata, which is for tracking sync | |
24 // state of each typed url. | |
25 | |
26 TypedURLSyncMetadataDatabase::TypedURLSyncMetadataDatabase() {} | |
27 | |
28 TypedURLSyncMetadataDatabase::~TypedURLSyncMetadataDatabase() {} | |
29 | |
30 bool TypedURLSyncMetadataDatabase::GetAllSyncMetadata( | |
31 syncer::MetadataBatch* metadata_batch) { | |
32 syncer::EntityMetadataMap metadata_records_map; | |
33 | |
34 // TODO(gangwu): crbug.com/701670 remove metadata_records_map to reduce memory | |
35 // usage and time. | |
36 if (GetAllSyncEntityMetadata(&metadata_records_map)) { | |
37 for (const auto& storage_key_to_metadata : metadata_records_map) { | |
38 metadata_batch->AddMetadata(storage_key_to_metadata.first, | |
39 storage_key_to_metadata.second); | |
40 } | |
41 } else { | |
42 return false; | |
43 } | |
44 | |
45 sync_pb::ModelTypeState model_type_state; | |
46 if (GetModelTypeState(&model_type_state)) { | |
47 metadata_batch->SetModelTypeState(model_type_state); | |
48 } else { | |
49 return false; | |
50 } | |
51 | |
52 return true; | |
53 } | |
54 | |
55 bool TypedURLSyncMetadataDatabase::UpdateSyncMetadata( | |
56 const std::string& storage_key, | |
57 const sync_pb::EntityMetadata& metadata) { | |
58 int64_t storage_key_int = 0; | |
59 if (!base::StringToInt64(storage_key, &storage_key_int)) { | |
60 return false; | |
61 } | |
62 sql::Statement s(GetDB().GetUniqueStatement( | |
63 "INSERT OR REPLACE INTO typed_url_sync_metadata " | |
64 "(storage_key, value) VALUES(?, ?)")); | |
65 s.BindInt64(0, storage_key_int); | |
66 s.BindString(1, metadata.SerializeAsString()); | |
67 | |
68 return s.Run(); | |
69 } | |
70 | |
71 bool TypedURLSyncMetadataDatabase::ClearSyncMetadata( | |
72 const std::string& storage_key) { | |
73 int64_t storage_key_int = 0; | |
74 if (!base::StringToInt64(storage_key, &storage_key_int)) { | |
75 return false; | |
76 } | |
77 sql::Statement s(GetDB().GetUniqueStatement( | |
78 "DELETE FROM typed_url_sync_metadata WHERE storage_key=?")); | |
79 s.BindInt64(0, storage_key_int); | |
80 | |
81 return s.Run(); | |
82 } | |
83 | |
84 bool TypedURLSyncMetadataDatabase::UpdateModelTypeState( | |
85 const sync_pb::ModelTypeState& model_type_state) { | |
86 DCHECK_GT(GetMetaTable()->GetVersionNumber(), 0); | |
87 | |
88 std::string serialized_state = model_type_state.SerializeAsString(); | |
89 return GetMetaTable()->SetValue(kTypedURLModelTypeStateKey, serialized_state); | |
90 } | |
91 | |
92 bool TypedURLSyncMetadataDatabase::ClearModelTypeState() { | |
93 DCHECK_GT(GetMetaTable()->GetVersionNumber(), 0); | |
94 return GetMetaTable()->DeleteKey(kTypedURLModelTypeStateKey); | |
95 } | |
96 | |
97 bool TypedURLSyncMetadataDatabase::InitSyncTable() { | |
98 if (!GetDB().DoesTableExist("typed_url_sync_metadata")) { | |
99 if (!GetDB().Execute("CREATE TABLE typed_url_sync_metadata (" | |
100 "storage_key INTEGER PRIMARY KEY NOT NULL," | |
101 "value BLOB)")) { | |
102 NOTREACHED(); | |
103 return false; | |
104 } | |
105 } | |
106 return true; | |
107 } | |
108 | |
109 bool TypedURLSyncMetadataDatabase::GetAllSyncEntityMetadata( | |
110 syncer::EntityMetadataMap* metadata_records) { | |
111 sql::Statement s(GetDB().GetUniqueStatement( | |
112 "SELECT storage_key, value FROM typed_url_sync_metadata")); | |
113 | |
114 while (s.Step()) { | |
115 std::string storage_key = base::Int64ToString(s.ColumnInt64(0)); | |
116 std::string serialized_metadata = s.ColumnString(1); | |
117 sync_pb::EntityMetadata metadata_record; | |
118 if (metadata_record.ParseFromString(serialized_metadata)) { | |
119 metadata_records->insert(std::make_pair(storage_key, metadata_record)); | |
120 } else { | |
121 return false; | |
122 } | |
123 } | |
124 return true; | |
125 } | |
126 | |
127 bool TypedURLSyncMetadataDatabase::GetModelTypeState( | |
128 sync_pb::ModelTypeState* state) { | |
129 DCHECK_GT(GetMetaTable()->GetVersionNumber(), 0); | |
130 std::string serialized_state; | |
131 if (!GetMetaTable()->GetValue(kTypedURLModelTypeStateKey, | |
132 &serialized_state)) { | |
133 return true; | |
134 } | |
135 | |
136 return state->ParseFromString(serialized_state); | |
137 } | |
138 | |
139 } // namespace history | |
OLD | NEW |