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 #include "components/enhanced_bookmarks/persistent_image_store.h" | 5 #include "components/enhanced_bookmarks/persistent_image_store.h" |
6 | 6 |
7 #include "components/enhanced_bookmarks/image_store_util.h" | 7 #include "components/enhanced_bookmarks/image_store_util.h" |
8 #include "sql/statement.h" | 8 #include "sql/statement.h" |
9 #include "sql/transaction.h" | 9 #include "sql/transaction.h" |
10 #include "ui/gfx/geometry/size.h" | 10 #include "ui/gfx/geometry/size.h" |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 | 69 |
70 } // namespace | 70 } // namespace |
71 | 71 |
72 PersistentImageStore::PersistentImageStore(const base::FilePath& path) | 72 PersistentImageStore::PersistentImageStore(const base::FilePath& path) |
73 : ImageStore(), | 73 : ImageStore(), |
74 path_(path.Append( | 74 path_(path.Append( |
75 base::FilePath::FromUTF8Unsafe("BookmarkImageAndUrlStore.db"))) { | 75 base::FilePath::FromUTF8Unsafe("BookmarkImageAndUrlStore.db"))) { |
76 } | 76 } |
77 | 77 |
78 bool PersistentImageStore::HasKey(const GURL& page_url) { | 78 bool PersistentImageStore::HasKey(const GURL& page_url) { |
79 DCHECK(thread_checker_.CalledOnValidThread()); | 79 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
80 if (OpenDatabase() != sql::INIT_OK) | 80 if (OpenDatabase() != sql::INIT_OK) |
81 return false; | 81 return false; |
82 | 82 |
83 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, | 83 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, |
84 "SELECT COUNT(*) FROM images_by_url WHERE page_url = ?")); | 84 "SELECT COUNT(*) FROM images_by_url WHERE page_url = ?")); |
85 statement.BindString(0, page_url.possibly_invalid_spec()); | 85 statement.BindString(0, page_url.possibly_invalid_spec()); |
86 | 86 |
87 int count = statement.Step() ? statement.ColumnInt(0) : 0; | 87 int count = statement.Step() ? statement.ColumnInt(0) : 0; |
88 | 88 |
89 return !!count; | 89 return !!count; |
90 } | 90 } |
91 | 91 |
92 void PersistentImageStore::Insert(const GURL& page_url, | 92 void PersistentImageStore::Insert(const GURL& page_url, |
93 const GURL& image_url, | 93 const GURL& image_url, |
94 const gfx::Image& image) { | 94 const gfx::Image& image) { |
95 DCHECK(thread_checker_.CalledOnValidThread()); | 95 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
96 if (OpenDatabase() != sql::INIT_OK) | 96 if (OpenDatabase() != sql::INIT_OK) |
97 return; | 97 return; |
98 | 98 |
99 Erase(page_url); // Remove previous image for this url, if any. | 99 Erase(page_url); // Remove previous image for this url, if any. |
100 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, | 100 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, |
101 "INSERT INTO images_by_url " | 101 "INSERT INTO images_by_url " |
102 "(page_url, image_url, image_data, width, height)" | 102 "(page_url, image_url, image_data, width, height)" |
103 "VALUES (?, ?, ?, ?, ?)")); | 103 "VALUES (?, ?, ?, ?, ?)")); |
104 | 104 |
105 statement.BindString(0, page_url.possibly_invalid_spec()); | 105 statement.BindString(0, page_url.possibly_invalid_spec()); |
106 statement.BindString(1, image_url.possibly_invalid_spec()); | 106 statement.BindString(1, image_url.possibly_invalid_spec()); |
107 | 107 |
108 scoped_refptr<base::RefCountedMemory> image_bytes = | 108 scoped_refptr<base::RefCountedMemory> image_bytes = |
109 enhanced_bookmarks::BytesForImage(image); | 109 enhanced_bookmarks::BytesForImage(image); |
110 | 110 |
111 // Insert an empty image in case encoding fails. | 111 // Insert an empty image in case encoding fails. |
112 if (!image_bytes.get()) | 112 if (!image_bytes.get()) |
113 image_bytes = enhanced_bookmarks::BytesForImage(gfx::Image()); | 113 image_bytes = enhanced_bookmarks::BytesForImage(gfx::Image()); |
114 | 114 |
115 CHECK(image_bytes.get()); | 115 CHECK(image_bytes.get()); |
116 | 116 |
117 statement.BindBlob(2, image_bytes->front(), (int)image_bytes->size()); | 117 statement.BindBlob(2, image_bytes->front(), (int)image_bytes->size()); |
118 | 118 |
119 statement.BindInt(3, image.Size().width()); | 119 statement.BindInt(3, image.Size().width()); |
120 statement.BindInt(4, image.Size().height()); | 120 statement.BindInt(4, image.Size().height()); |
121 statement.Run(); | 121 statement.Run(); |
122 } | 122 } |
123 | 123 |
124 void PersistentImageStore::Erase(const GURL& page_url) { | 124 void PersistentImageStore::Erase(const GURL& page_url) { |
125 DCHECK(thread_checker_.CalledOnValidThread()); | 125 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
126 if (OpenDatabase() != sql::INIT_OK) | 126 if (OpenDatabase() != sql::INIT_OK) |
127 return; | 127 return; |
128 | 128 |
129 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, | 129 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, |
130 "DELETE FROM images_by_url WHERE page_url = ?")); | 130 "DELETE FROM images_by_url WHERE page_url = ?")); |
131 statement.BindString(0, page_url.possibly_invalid_spec()); | 131 statement.BindString(0, page_url.possibly_invalid_spec()); |
132 statement.Run(); | 132 statement.Run(); |
133 } | 133 } |
134 | 134 |
135 std::pair<gfx::Image, GURL> PersistentImageStore::Get(const GURL& page_url) { | 135 std::pair<gfx::Image, GURL> PersistentImageStore::Get(const GURL& page_url) { |
136 DCHECK(thread_checker_.CalledOnValidThread()); | 136 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
137 if (OpenDatabase() != sql::INIT_OK) | 137 if (OpenDatabase() != sql::INIT_OK) |
138 return std::make_pair(gfx::Image(), GURL()); | 138 return std::make_pair(gfx::Image(), GURL()); |
139 | 139 |
140 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, | 140 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, |
141 "SELECT image_data, image_url FROM images_by_url WHERE page_url = ?")); | 141 "SELECT image_data, image_url FROM images_by_url WHERE page_url = ?")); |
142 | 142 |
143 statement.BindString(0, page_url.possibly_invalid_spec()); | 143 statement.BindString(0, page_url.possibly_invalid_spec()); |
144 | 144 |
145 while (statement.Step()) { | 145 while (statement.Step()) { |
146 if (statement.ColumnByteLength(0) > 0) { | 146 if (statement.ColumnByteLength(0) > 0) { |
147 scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes()); | 147 scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes()); |
148 statement.ColumnBlobAsVector(0, &data->data()); | 148 statement.ColumnBlobAsVector(0, &data->data()); |
149 | 149 |
150 return std::make_pair(enhanced_bookmarks::ImageForBytes(data), | 150 return std::make_pair(enhanced_bookmarks::ImageForBytes(data), |
151 GURL(statement.ColumnString(1))); | 151 GURL(statement.ColumnString(1))); |
152 } | 152 } |
153 } | 153 } |
154 | 154 |
155 return std::make_pair(gfx::Image(), GURL()); | 155 return std::make_pair(gfx::Image(), GURL()); |
156 } | 156 } |
157 | 157 |
158 gfx::Size PersistentImageStore::GetSize(const GURL& page_url) { | 158 gfx::Size PersistentImageStore::GetSize(const GURL& page_url) { |
159 DCHECK(thread_checker_.CalledOnValidThread()); | 159 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
160 if (OpenDatabase() != sql::INIT_OK) | 160 if (OpenDatabase() != sql::INIT_OK) |
161 return gfx::Size(); | 161 return gfx::Size(); |
162 | 162 |
163 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, | 163 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, |
164 "SELECT width, height FROM images_by_url WHERE page_url = ?")); | 164 "SELECT width, height FROM images_by_url WHERE page_url = ?")); |
165 | 165 |
166 statement.BindString(0, page_url.possibly_invalid_spec()); | 166 statement.BindString(0, page_url.possibly_invalid_spec()); |
167 | 167 |
168 while (statement.Step()) { | 168 while (statement.Step()) { |
169 if (statement.ColumnByteLength(0) > 0) { | 169 if (statement.ColumnByteLength(0) > 0) { |
170 int width = statement.ColumnInt(0); | 170 int width = statement.ColumnInt(0); |
171 int height = statement.ColumnInt(1); | 171 int height = statement.ColumnInt(1); |
172 return gfx::Size(width, height); | 172 return gfx::Size(width, height); |
173 } | 173 } |
174 } | 174 } |
175 return gfx::Size(); | 175 return gfx::Size(); |
176 } | 176 } |
177 | 177 |
178 void PersistentImageStore::GetAllPageUrls(std::set<GURL>* urls) { | 178 void PersistentImageStore::GetAllPageUrls(std::set<GURL>* urls) { |
179 DCHECK(thread_checker_.CalledOnValidThread()); | 179 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
180 DCHECK(urls->empty()); | 180 DCHECK(urls->empty()); |
181 if (OpenDatabase() != sql::INIT_OK) | 181 if (OpenDatabase() != sql::INIT_OK) |
182 return; | 182 return; |
183 | 183 |
184 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, | 184 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, |
185 "SELECT page_url FROM images_by_url")); | 185 "SELECT page_url FROM images_by_url")); |
186 while (statement.Step()) | 186 while (statement.Step()) |
187 urls->insert(GURL(statement.ColumnString(0))); | 187 urls->insert(GURL(statement.ColumnString(0))); |
188 } | 188 } |
189 | 189 |
190 void PersistentImageStore::ClearAll() { | 190 void PersistentImageStore::ClearAll() { |
191 DCHECK(thread_checker_.CalledOnValidThread()); | 191 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
192 if (OpenDatabase() != sql::INIT_OK) | 192 if (OpenDatabase() != sql::INIT_OK) |
193 return; | 193 return; |
194 | 194 |
195 sql::Statement statement(db_.GetCachedStatement( | 195 sql::Statement statement(db_.GetCachedStatement( |
196 SQL_FROM_HERE, "DELETE FROM images_by_url")); | 196 SQL_FROM_HERE, "DELETE FROM images_by_url")); |
197 statement.Run(); | 197 statement.Run(); |
198 } | 198 } |
199 | 199 |
200 PersistentImageStore::~PersistentImageStore() { | 200 PersistentImageStore::~PersistentImageStore() { |
201 DCHECK(thread_checker_.CalledOnValidThread()); | 201 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
202 } | 202 } |
203 | 203 |
204 sql::InitStatus PersistentImageStore::OpenDatabase() { | 204 sql::InitStatus PersistentImageStore::OpenDatabase() { |
205 DCHECK(thread_checker_.CalledOnValidThread()); | 205 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
206 | 206 |
207 if (db_.is_open()) | 207 if (db_.is_open()) |
208 return sql::INIT_OK; | 208 return sql::INIT_OK; |
209 | 209 |
210 const size_t kAttempts = 2; | 210 const size_t kAttempts = 2; |
211 | 211 |
212 sql::InitStatus status = sql::INIT_FAILURE; | 212 sql::InitStatus status = sql::INIT_FAILURE; |
213 for (size_t i = 0; i < kAttempts; ++i) { | 213 for (size_t i = 0; i < kAttempts; ++i) { |
214 status = OpenDatabaseImpl(db_, path_); | 214 status = OpenDatabaseImpl(db_, path_); |
215 if (status == sql::INIT_OK) | 215 if (status == sql::INIT_OK) |
216 return status; | 216 return status; |
217 | 217 |
218 // Can't open, raze(). | 218 // Can't open, raze(). |
219 if (db_.is_open()) | 219 if (db_.is_open()) |
220 db_.Raze(); | 220 db_.Raze(); |
221 db_.Close(); | 221 db_.Close(); |
222 } | 222 } |
223 | 223 |
224 DCHECK(false) << "Can't open image DB"; | 224 DCHECK(false) << "Can't open image DB"; |
225 return sql::INIT_FAILURE; | 225 return sql::INIT_FAILURE; |
226 } | 226 } |
OLD | NEW |