| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/ui/app_list/fast_show_pickler.h" | 5 #include "chrome/browser/ui/app_list/fast_show_pickler.h" |
| 6 | 6 |
| 7 #include "third_party/skia/include/core/SkBitmap.h" | 7 #include "third_party/skia/include/core/SkBitmap.h" |
| 8 #include "ui/app_list/app_list_item.h" | 8 #include "ui/app_list/app_list_item.h" |
| 9 #include "ui/gfx/image/image_skia_rep.h" | 9 #include "ui/gfx/image/image_skia_rep.h" |
| 10 | 10 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 *out = ARGB_4444; | 68 *out = ARGB_4444; |
| 69 break; | 69 break; |
| 70 case kN32_SkColorType: | 70 case kN32_SkColorType: |
| 71 *out = ARGB_8888; | 71 *out = ARGB_8888; |
| 72 break; | 72 break; |
| 73 default: return false; | 73 default: return false; |
| 74 } | 74 } |
| 75 return true; | 75 return true; |
| 76 } | 76 } |
| 77 | 77 |
| 78 bool PickleImage(Pickle* pickle, const gfx::ImageSkia& image) { | 78 bool PickleImage(base::Pickle* pickle, const gfx::ImageSkia& image) { |
| 79 std::vector<gfx::ImageSkiaRep> reps(image.image_reps()); | 79 std::vector<gfx::ImageSkiaRep> reps(image.image_reps()); |
| 80 pickle->WriteInt(static_cast<int>(reps.size())); | 80 pickle->WriteInt(static_cast<int>(reps.size())); |
| 81 for (std::vector<gfx::ImageSkiaRep>::const_iterator it = reps.begin(); | 81 for (std::vector<gfx::ImageSkiaRep>::const_iterator it = reps.begin(); |
| 82 it != reps.end(); ++it) { | 82 it != reps.end(); ++it) { |
| 83 pickle->WriteFloat(it->scale()); | 83 pickle->WriteFloat(it->scale()); |
| 84 pickle->WriteInt(it->pixel_width()); | 84 pickle->WriteInt(it->pixel_width()); |
| 85 pickle->WriteInt(it->pixel_height()); | 85 pickle->WriteInt(it->pixel_height()); |
| 86 ImageFormat format = NONE; | 86 ImageFormat format = NONE; |
| 87 if (!ColorTypeToFormat(it->sk_bitmap().colorType(), &format)) | 87 if (!ColorTypeToFormat(it->sk_bitmap().colorType(), &format)) |
| 88 return false; | 88 return false; |
| 89 pickle->WriteInt(static_cast<int>(format)); | 89 pickle->WriteInt(static_cast<int>(format)); |
| 90 int size = static_cast<int>(it->sk_bitmap().getSafeSize()); | 90 int size = static_cast<int>(it->sk_bitmap().getSafeSize()); |
| 91 pickle->WriteInt(size); | 91 pickle->WriteInt(size); |
| 92 SkBitmap bitmap = it->sk_bitmap(); | 92 SkBitmap bitmap = it->sk_bitmap(); |
| 93 SkAutoLockPixels lock(bitmap); | 93 SkAutoLockPixels lock(bitmap); |
| 94 pickle->WriteBytes(bitmap.getPixels(), size); | 94 pickle->WriteBytes(bitmap.getPixels(), size); |
| 95 } | 95 } |
| 96 return true; | 96 return true; |
| 97 } | 97 } |
| 98 | 98 |
| 99 bool UnpickleImage(PickleIterator* it, gfx::ImageSkia* out) { | 99 bool UnpickleImage(base::PickleIterator* it, gfx::ImageSkia* out) { |
| 100 int rep_count = 0; | 100 int rep_count = 0; |
| 101 if (!it->ReadInt(&rep_count)) | 101 if (!it->ReadInt(&rep_count)) |
| 102 return false; | 102 return false; |
| 103 | 103 |
| 104 gfx::ImageSkia result; | 104 gfx::ImageSkia result; |
| 105 for (int i = 0; i < rep_count; ++i) { | 105 for (int i = 0; i < rep_count; ++i) { |
| 106 float scale = 0.0f; | 106 float scale = 0.0f; |
| 107 if (!it->ReadFloat(&scale)) | 107 if (!it->ReadFloat(&scale)) |
| 108 return false; | 108 return false; |
| 109 | 109 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 140 result.AddRepresentation(gfx::ImageSkiaRep(bitmap, scale)); | 140 result.AddRepresentation(gfx::ImageSkiaRep(bitmap, scale)); |
| 141 } | 141 } |
| 142 | 142 |
| 143 *out = result; | 143 *out = result; |
| 144 return true; | 144 return true; |
| 145 } | 145 } |
| 146 | 146 |
| 147 } // namespace | 147 } // namespace |
| 148 | 148 |
| 149 scoped_ptr<AppListItem> FastShowPickler::UnpickleAppListItem( | 149 scoped_ptr<AppListItem> FastShowPickler::UnpickleAppListItem( |
| 150 PickleIterator* it) { | 150 base::PickleIterator* it) { |
| 151 std::string id; | 151 std::string id; |
| 152 if (!it->ReadString(&id)) | 152 if (!it->ReadString(&id)) |
| 153 return scoped_ptr<AppListItem>(); | 153 return scoped_ptr<AppListItem>(); |
| 154 scoped_ptr<AppListItem> result(new AppListItem(id)); | 154 scoped_ptr<AppListItem> result(new AppListItem(id)); |
| 155 std::string name; | 155 std::string name; |
| 156 if (!it->ReadString(&name)) | 156 if (!it->ReadString(&name)) |
| 157 return scoped_ptr<AppListItem>(); | 157 return scoped_ptr<AppListItem>(); |
| 158 std::string short_name; | 158 std::string short_name; |
| 159 if (!it->ReadString(&short_name)) | 159 if (!it->ReadString(&short_name)) |
| 160 return scoped_ptr<AppListItem>(); | 160 return scoped_ptr<AppListItem>(); |
| 161 result->SetNameAndShortName(name, short_name); | 161 result->SetNameAndShortName(name, short_name); |
| 162 bool has_shadow = false; | 162 bool has_shadow = false; |
| 163 if (!it->ReadBool(&has_shadow)) | 163 if (!it->ReadBool(&has_shadow)) |
| 164 return scoped_ptr<AppListItem>(); | 164 return scoped_ptr<AppListItem>(); |
| 165 gfx::ImageSkia icon; | 165 gfx::ImageSkia icon; |
| 166 if (!UnpickleImage(it, &icon)) | 166 if (!UnpickleImage(it, &icon)) |
| 167 return scoped_ptr<AppListItem>(); | 167 return scoped_ptr<AppListItem>(); |
| 168 result->SetIcon(icon, has_shadow); | 168 result->SetIcon(icon, has_shadow); |
| 169 return result.Pass(); | 169 return result.Pass(); |
| 170 } | 170 } |
| 171 | 171 |
| 172 bool FastShowPickler::PickleAppListItem(Pickle* pickle, AppListItem* item) { | 172 bool FastShowPickler::PickleAppListItem(base::Pickle* pickle, |
| 173 AppListItem* item) { |
| 173 if (!pickle->WriteString(item->id())) | 174 if (!pickle->WriteString(item->id())) |
| 174 return false; | 175 return false; |
| 175 if (!pickle->WriteString(item->name())) | 176 if (!pickle->WriteString(item->name())) |
| 176 return false; | 177 return false; |
| 177 if (!pickle->WriteString(item->short_name())) | 178 if (!pickle->WriteString(item->short_name())) |
| 178 return false; | 179 return false; |
| 179 if (!pickle->WriteBool(item->has_shadow())) | 180 if (!pickle->WriteBool(item->has_shadow())) |
| 180 return false; | 181 return false; |
| 181 if (!PickleImage(pickle, item->icon())) | 182 if (!PickleImage(pickle, item->icon())) |
| 182 return false; | 183 return false; |
| 183 return true; | 184 return true; |
| 184 } | 185 } |
| 185 | 186 |
| 186 void FastShowPickler::CopyOverItem(AppListItem* src_item, | 187 void FastShowPickler::CopyOverItem(AppListItem* src_item, |
| 187 AppListItem* dest_item) { | 188 AppListItem* dest_item) { |
| 188 dest_item->SetNameAndShortName(src_item->name(), src_item->short_name()); | 189 dest_item->SetNameAndShortName(src_item->name(), src_item->short_name()); |
| 189 dest_item->SetIcon(src_item->icon(), src_item->has_shadow()); | 190 dest_item->SetIcon(src_item->icon(), src_item->has_shadow()); |
| 190 // Do not set folder_id, pass that to AppListModel::AddItemToFolder() instead. | 191 // Do not set folder_id, pass that to AppListModel::AddItemToFolder() instead. |
| 191 } | 192 } |
| 192 | 193 |
| 193 // The version of the pickle format defined here. This needs to be incremented | 194 // The version of the pickle format defined here. This needs to be incremented |
| 194 // whenever this format is changed so new clients can invalidate old versions. | 195 // whenever this format is changed so new clients can invalidate old versions. |
| 195 const int FastShowPickler::kVersion = 3; | 196 const int FastShowPickler::kVersion = 3; |
| 196 | 197 |
| 197 scoped_ptr<Pickle> FastShowPickler::PickleAppListModelForFastShow( | 198 scoped_ptr<base::Pickle> FastShowPickler::PickleAppListModelForFastShow( |
| 198 AppListModel* model) { | 199 AppListModel* model) { |
| 199 scoped_ptr<Pickle> result(new Pickle); | 200 scoped_ptr<base::Pickle> result(new Pickle); |
| 200 if (!result->WriteInt(kVersion)) | 201 if (!result->WriteInt(kVersion)) |
| 201 return scoped_ptr<Pickle>(); | 202 return scoped_ptr<base::Pickle>(); |
| 202 if (!result->WriteInt((int)model->top_level_item_list()->item_count())) | 203 if (!result->WriteInt((int)model->top_level_item_list()->item_count())) |
| 203 return scoped_ptr<Pickle>(); | 204 return scoped_ptr<base::Pickle>(); |
| 204 for (size_t i = 0; i < model->top_level_item_list()->item_count(); ++i) { | 205 for (size_t i = 0; i < model->top_level_item_list()->item_count(); ++i) { |
| 205 if (!PickleAppListItem(result.get(), | 206 if (!PickleAppListItem(result.get(), |
| 206 model->top_level_item_list()->item_at(i))) { | 207 model->top_level_item_list()->item_at(i))) { |
| 207 return scoped_ptr<Pickle>(); | 208 return scoped_ptr<base::Pickle>(); |
| 208 } | 209 } |
| 209 } | 210 } |
| 210 return result.Pass(); | 211 return result.Pass(); |
| 211 } | 212 } |
| 212 | 213 |
| 213 void FastShowPickler::CopyOver(AppListModel* src, AppListModel* dest) { | 214 void FastShowPickler::CopyOver(AppListModel* src, AppListModel* dest) { |
| 214 DCHECK_EQ(0u, dest->top_level_item_list()->item_count()); | 215 DCHECK_EQ(0u, dest->top_level_item_list()->item_count()); |
| 215 for (size_t i = 0; i < src->top_level_item_list()->item_count(); i++) { | 216 for (size_t i = 0; i < src->top_level_item_list()->item_count(); i++) { |
| 216 AppListItem* src_item = src->top_level_item_list()->item_at(i); | 217 AppListItem* src_item = src->top_level_item_list()->item_at(i); |
| 217 scoped_ptr<AppListItem> dest_item(new AppListItem(src_item->id())); | 218 scoped_ptr<AppListItem> dest_item(new AppListItem(src_item->id())); |
| 218 CopyOverItem(src_item, dest_item.get()); | 219 CopyOverItem(src_item, dest_item.get()); |
| 219 dest->AddItemToFolder(dest_item.Pass(), src_item->folder_id()); | 220 dest->AddItemToFolder(dest_item.Pass(), src_item->folder_id()); |
| 220 } | 221 } |
| 221 } | 222 } |
| 222 | 223 |
| 223 scoped_ptr<AppListModel> | 224 scoped_ptr<AppListModel> FastShowPickler::UnpickleAppListModelForFastShow( |
| 224 FastShowPickler::UnpickleAppListModelForFastShow(Pickle* pickle) { | 225 base::Pickle* pickle) { |
| 225 PickleIterator it(*pickle); | 226 base::PickleIterator it(*pickle); |
| 226 int read_version = 0; | 227 int read_version = 0; |
| 227 if (!it.ReadInt(&read_version)) | 228 if (!it.ReadInt(&read_version)) |
| 228 return scoped_ptr<AppListModel>(); | 229 return scoped_ptr<AppListModel>(); |
| 229 if (read_version != kVersion) | 230 if (read_version != kVersion) |
| 230 return scoped_ptr<AppListModel>(); | 231 return scoped_ptr<AppListModel>(); |
| 231 int app_count = 0; | 232 int app_count = 0; |
| 232 if (!it.ReadInt(&app_count)) | 233 if (!it.ReadInt(&app_count)) |
| 233 return scoped_ptr<AppListModel>(); | 234 return scoped_ptr<AppListModel>(); |
| 234 | 235 |
| 235 scoped_ptr<AppListModel> model(new AppListModel); | 236 scoped_ptr<AppListModel> model(new AppListModel); |
| 236 for (int i = 0; i < app_count; ++i) { | 237 for (int i = 0; i < app_count; ++i) { |
| 237 scoped_ptr<AppListItem> item(UnpickleAppListItem(&it).Pass()); | 238 scoped_ptr<AppListItem> item(UnpickleAppListItem(&it).Pass()); |
| 238 if (!item) | 239 if (!item) |
| 239 return scoped_ptr<AppListModel>(); | 240 return scoped_ptr<AppListModel>(); |
| 240 std::string folder_id = item->folder_id(); | 241 std::string folder_id = item->folder_id(); |
| 241 model->AddItemToFolder(item.Pass(), folder_id); | 242 model->AddItemToFolder(item.Pass(), folder_id); |
| 242 } | 243 } |
| 243 | 244 |
| 244 return model.Pass(); | 245 return model.Pass(); |
| 245 } | 246 } |
| OLD | NEW |