web_apps_table.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
1// Copyright (c) 2011 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 "chrome/browser/webdata/web_apps_table.h" 6 7#include "base/logging.h" 8#include "chrome/browser/history/history_database.h" 9#include "googleurl/src/gurl.h" 10#include "sql/statement.h" 11#include "third_party/skia/include/core/SkBitmap.h" 12#include "ui/gfx/codec/png_codec.h" 13 14bool WebAppsTable::Init() { 15 return (InitWebAppIconsTable() && InitWebAppsTable()); 16} 17 18bool WebAppsTable::IsSyncable() { 19 return true; 20} 21 22bool WebAppsTable::InitWebAppIconsTable() { 23 if (!db_->DoesTableExist("web_app_icons")) { 24 if (!db_->Execute("CREATE TABLE web_app_icons (" 25 "url LONGVARCHAR," 26 "width int," 27 "height int," 28 "image BLOB, UNIQUE (url, width, height))")) { 29 NOTREACHED(); 30 return false; 31 } 32 } 33 return true; 34} 35 36bool WebAppsTable::InitWebAppsTable() { 37 if (!db_->DoesTableExist("web_apps")) { 38 if (!db_->Execute("CREATE TABLE web_apps (" 39 "url LONGVARCHAR UNIQUE," 40 "has_all_images INTEGER NOT NULL)")) { 41 NOTREACHED(); 42 return false; 43 } 44 if (!db_->Execute("CREATE INDEX web_apps_url_index ON web_apps (url)")) { 45 NOTREACHED(); 46 return false; 47 } 48 } 49 return true; 50} 51 52bool WebAppsTable::SetWebAppImage(const GURL& url, const SkBitmap& image) { 53 // Don't bother with a cached statement since this will be a relatively 54 // infrequent operation. 55 sql::Statement s(db_->GetUniqueStatement( 56 "INSERT OR REPLACE INTO web_app_icons " 57 "(url, width, height, image) VALUES (?, ?, ?, ?)")); 58 std::vector<unsigned char> image_data; 59 gfx::PNGCodec::EncodeBGRASkBitmap(image, false, &image_data); 60 s.BindString(0, history::HistoryDatabase::GURLToDatabaseURL(url)); 61 s.BindInt(1, image.width()); 62 s.BindInt(2, image.height()); 63 s.BindBlob(3, &image_data.front(), static_cast<int>(image_data.size())); 64 65 return s.Run(); 66} 67 68bool WebAppsTable::GetWebAppImages(const GURL& url, 69 std::vector<SkBitmap>* images) { 70 sql::Statement s(db_->GetUniqueStatement( 71 "SELECT image FROM web_app_icons WHERE url=?")); 72 s.BindString(0, history::HistoryDatabase::GURLToDatabaseURL(url)); 73 74 while (s.Step()) { 75 SkBitmap image; 76 int col_bytes = s.ColumnByteLength(0); 77 if (col_bytes > 0) { 78 if (gfx::PNGCodec::Decode( 79 reinterpret_cast<const unsigned char*>(s.ColumnBlob(0)), 80 col_bytes, &image)) { 81 images->push_back(image); 82 } else { 83 // Should only have valid image data in the db. 84 NOTREACHED(); 85 } 86 } 87 } 88 return s.Succeeded(); 89} 90 91bool WebAppsTable::SetWebAppHasAllImages(const GURL& url, 92 bool has_all_images) { 93 sql::Statement s(db_->GetUniqueStatement( 94 "INSERT OR REPLACE INTO web_apps (url, has_all_images) VALUES (?, ?)")); 95 s.BindString(0, history::HistoryDatabase::GURLToDatabaseURL(url)); 96 s.BindInt(1, has_all_images ? 1 : 0); 97 98 return s.Run(); 99} 100 101bool WebAppsTable::GetWebAppHasAllImages(const GURL& url) { 102 sql::Statement s(db_->GetUniqueStatement( 103 "SELECT has_all_images FROM web_apps WHERE url=?")); 104 s.BindString(0, history::HistoryDatabase::GURLToDatabaseURL(url)); 105 106 return (s.Step() && s.ColumnInt(0) == 1); 107} 108 109bool WebAppsTable::RemoveWebApp(const GURL& url) { 110 sql::Statement delete_s(db_->GetUniqueStatement( 111 "DELETE FROM web_app_icons WHERE url = ?")); 112 delete_s.BindString(0, history::HistoryDatabase::GURLToDatabaseURL(url)); 113 114 if (!delete_s.Run()) 115 return false; 116 117 sql::Statement delete_s2(db_->GetUniqueStatement( 118 "DELETE FROM web_apps WHERE url = ?")); 119 delete_s2.BindString(0, history::HistoryDatabase::GURLToDatabaseURL(url)); 120 121 return delete_s2.Run(); 122} 123