| 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 "base/files/file.h" | 7 #include "base/files/file.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "components/enhanced_bookmarks/image_store_util.h" | 9 #include "components/enhanced_bookmarks/image_store_util.h" |
| 10 #include "sql/statement.h" | 10 #include "sql/statement.h" |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 "SELECT COUNT(*) FROM images_by_url WHERE page_url = ?")); | 156 "SELECT COUNT(*) FROM images_by_url WHERE page_url = ?")); |
| 157 statement.BindString(0, page_url.possibly_invalid_spec()); | 157 statement.BindString(0, page_url.possibly_invalid_spec()); |
| 158 | 158 |
| 159 int count = statement.Step() ? statement.ColumnInt(0) : 0; | 159 int count = statement.Step() ? statement.ColumnInt(0) : 0; |
| 160 | 160 |
| 161 return !!count; | 161 return !!count; |
| 162 } | 162 } |
| 163 | 163 |
| 164 void PersistentImageStore::Insert( | 164 void PersistentImageStore::Insert( |
| 165 const GURL& page_url, | 165 const GURL& page_url, |
| 166 const enhanced_bookmarks::ImageRecord& record) { | 166 scoped_refptr<enhanced_bookmarks::ImageRecord> record) { |
| 167 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 167 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
| 168 if (OpenDatabase() != sql::INIT_OK) | 168 if (OpenDatabase() != sql::INIT_OK) |
| 169 return; | 169 return; |
| 170 | 170 |
| 171 Erase(page_url); // Remove previous image for this url, if any. | 171 Erase(page_url); // Remove previous image for this url, if any. |
| 172 sql::Statement statement(db_.GetCachedStatement( | 172 sql::Statement statement(db_.GetCachedStatement( |
| 173 SQL_FROM_HERE, | 173 SQL_FROM_HERE, |
| 174 "INSERT INTO images_by_url " | 174 "INSERT INTO images_by_url " |
| 175 "(page_url, image_url, image_data, width, height, image_dominant_color)" | 175 "(page_url, image_url, image_data, width, height, image_dominant_color)" |
| 176 "VALUES (?, ?, ?, ?, ?, ?)")); | 176 "VALUES (?, ?, ?, ?, ?, ?)")); |
| 177 | 177 |
| 178 statement.BindString(0, page_url.possibly_invalid_spec()); | 178 statement.BindString(0, page_url.possibly_invalid_spec()); |
| 179 statement.BindString(1, record.url.possibly_invalid_spec()); | 179 statement.BindString(1, record->url.possibly_invalid_spec()); |
| 180 | 180 |
| 181 scoped_refptr<base::RefCountedMemory> image_bytes = | 181 scoped_refptr<base::RefCountedMemory> image_bytes = |
| 182 enhanced_bookmarks::BytesForImage(record.image); | 182 enhanced_bookmarks::BytesForImage(*record->image); |
| 183 | 183 |
| 184 // Insert an empty image in case encoding fails. | 184 // Insert an empty image in case encoding fails. |
| 185 if (!image_bytes.get()) | 185 if (!image_bytes.get()) |
| 186 image_bytes = enhanced_bookmarks::BytesForImage(gfx::Image()); | 186 image_bytes = enhanced_bookmarks::BytesForImage(gfx::Image()); |
| 187 | 187 |
| 188 CHECK(image_bytes.get()); | 188 CHECK(image_bytes.get()); |
| 189 | 189 |
| 190 statement.BindBlob(2, image_bytes->front(), (int)image_bytes->size()); | 190 statement.BindBlob(2, image_bytes->front(), (int)image_bytes->size()); |
| 191 | 191 |
| 192 statement.BindInt(3, record.image.Size().width()); | 192 statement.BindInt(3, record->image->Size().width()); |
| 193 statement.BindInt(4, record.image.Size().height()); | 193 statement.BindInt(4, record->image->Size().height()); |
| 194 statement.BindInt(5, record.dominant_color); | 194 statement.BindInt(5, record->dominant_color); |
| 195 statement.Run(); | 195 statement.Run(); |
| 196 } | 196 } |
| 197 | 197 |
| 198 void PersistentImageStore::Erase(const GURL& page_url) { | 198 void PersistentImageStore::Erase(const GURL& page_url) { |
| 199 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 199 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
| 200 if (OpenDatabase() != sql::INIT_OK) | 200 if (OpenDatabase() != sql::INIT_OK) |
| 201 return; | 201 return; |
| 202 | 202 |
| 203 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, | 203 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, |
| 204 "DELETE FROM images_by_url WHERE page_url = ?")); | 204 "DELETE FROM images_by_url WHERE page_url = ?")); |
| 205 statement.BindString(0, page_url.possibly_invalid_spec()); | 205 statement.BindString(0, page_url.possibly_invalid_spec()); |
| 206 statement.Run(); | 206 statement.Run(); |
| 207 } | 207 } |
| 208 | 208 |
| 209 enhanced_bookmarks::ImageRecord PersistentImageStore::Get( | 209 scoped_refptr<enhanced_bookmarks::ImageRecord> PersistentImageStore::Get( |
| 210 const GURL& page_url) { | 210 const GURL& page_url) { |
| 211 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 211 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
| 212 enhanced_bookmarks::ImageRecord image_record; | 212 scoped_refptr<enhanced_bookmarks::ImageRecord> image_record( |
| 213 new enhanced_bookmarks::ImageRecord()); |
| 213 if (OpenDatabase() != sql::INIT_OK) | 214 if (OpenDatabase() != sql::INIT_OK) |
| 214 return image_record; | 215 return image_record; |
| 215 | 216 |
| 216 bool stored_image_record_needs_update = false; | 217 bool stored_image_record_needs_update = false; |
| 217 | 218 |
| 218 // Scope the SELECT statement. | 219 // Scope the SELECT statement. |
| 219 { | 220 { |
| 220 sql::Statement statement(db_.GetCachedStatement( | 221 sql::Statement statement(db_.GetCachedStatement( |
| 221 SQL_FROM_HERE, | 222 SQL_FROM_HERE, |
| 222 "SELECT image_data, image_url, image_dominant_color FROM images_by_url " | 223 "SELECT image_data, image_url, image_dominant_color FROM images_by_url " |
| 223 "WHERE page_url = ?")); | 224 "WHERE page_url = ?")); |
| 224 | 225 |
| 225 statement.BindString(0, page_url.possibly_invalid_spec()); | 226 statement.BindString(0, page_url.possibly_invalid_spec()); |
| 226 | 227 |
| 227 if (!statement.Step()) | 228 if (!statement.Step()) |
| 228 return image_record; | 229 return image_record; |
| 229 | 230 |
| 230 // Image. | 231 // Image. |
| 231 if (statement.ColumnByteLength(0) > 0) { | 232 if (statement.ColumnByteLength(0) > 0) { |
| 232 scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes()); | 233 scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes()); |
| 233 statement.ColumnBlobAsVector(0, &data->data()); | 234 statement.ColumnBlobAsVector(0, &data->data()); |
| 234 image_record.image = enhanced_bookmarks::ImageForBytes(data); | 235 *image_record->image = enhanced_bookmarks::ImageForBytes(data); |
| 235 } | 236 } |
| 236 | 237 |
| 237 // URL. | 238 // URL. |
| 238 image_record.url = GURL(statement.ColumnString(1)); | 239 image_record->url = GURL(statement.ColumnString(1)); |
| 239 | 240 |
| 240 // Dominant color. | 241 // Dominant color. |
| 241 if (statement.ColumnType(2) != sql::COLUMN_TYPE_NULL) { | 242 if (statement.ColumnType(2) != sql::COLUMN_TYPE_NULL) { |
| 242 image_record.dominant_color = SkColor(statement.ColumnInt(2)); | 243 image_record->dominant_color = SkColor(statement.ColumnInt(2)); |
| 243 } else { | 244 } else { |
| 244 // The dominant color was not computed when the image was first | 245 // The dominant color was not computed when the image was first |
| 245 // stored. | 246 // stored. |
| 246 // Compute it now. | 247 // Compute it now. |
| 247 image_record.dominant_color = | 248 image_record->dominant_color = |
| 248 enhanced_bookmarks::DominantColorForImage(image_record.image); | 249 enhanced_bookmarks::DominantColorForImage(*image_record->image); |
| 249 stored_image_record_needs_update = true; | 250 stored_image_record_needs_update = true; |
| 250 } | 251 } |
| 251 | 252 |
| 252 // Make sure there is only one record for page_url. | 253 // Make sure there is only one record for page_url. |
| 253 DCHECK(!statement.Step()); | 254 DCHECK(!statement.Step()); |
| 254 } | 255 } |
| 255 | 256 |
| 256 if (stored_image_record_needs_update) { | 257 if (stored_image_record_needs_update) { |
| 257 Erase(page_url); | 258 Erase(page_url); |
| 258 Insert(page_url, image_record); | 259 Insert(page_url, image_record); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 | 329 |
| 329 // Can't open, raze(). | 330 // Can't open, raze(). |
| 330 if (db_.is_open()) | 331 if (db_.is_open()) |
| 331 db_.Raze(); | 332 db_.Raze(); |
| 332 db_.Close(); | 333 db_.Close(); |
| 333 } | 334 } |
| 334 | 335 |
| 335 DCHECK(false) << "Can't open image DB"; | 336 DCHECK(false) << "Can't open image DB"; |
| 336 return sql::INIT_FAILURE; | 337 return sql::INIT_FAILURE; |
| 337 } | 338 } |
| OLD | NEW |