Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2015 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/ui/app_list/arc_app_item.h" | |
| 6 | |
| 7 #include "base/files/file_path.h" | |
| 8 #include "base/files/file_util.h" | |
| 9 #include "base/task_runner_util.h" | |
| 10 #include "chrome/browser/ui/app_list/arc_app_prefs.h" | |
| 11 #include "chromeos/dbus/arc_bridge_client.h" | |
| 12 #include "chromeos/dbus/dbus_thread_manager.h" | |
| 13 #include "content/public/browser/browser_thread.h" | |
| 14 #include "extensions/grit/extensions_browser_resources.h" | |
| 15 #include "ui/base/resource/resource_bundle.h" | |
| 16 #include "ui/gfx/codec/png_codec.h" | |
| 17 #include "ui/gfx/color_utils.h" | |
| 18 #include "ui/gfx/image/image_skia_operations.h" | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 gfx::ImageSkia CreateDisabledIcon(const gfx::ImageSkia& icon) { | |
| 23 const color_utils::HSL shift = {-1, 0, 0.6}; | |
| 24 return gfx::ImageSkiaOperations::CreateHSLShiftedImage(icon, shift); | |
| 25 } | |
| 26 | |
| 27 } | |
| 28 | |
| 29 // static | |
| 30 const char ArcAppItem::kItemType[] = "ArcAppItem"; | |
| 31 | |
| 32 ArcAppItem::ArcAppItem(content::BrowserContext* context, | |
| 33 const std::string& id, | |
| 34 const std::string& name, | |
| 35 bool ready) | |
| 36 : app_list::AppListItem(id), | |
| 37 context_(context), | |
| 38 ready_(ready), | |
| 39 weak_ptr_factory_(this) { | |
| 40 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 41 | |
| 42 SetName(name); | |
| 43 set_position(syncer::StringOrdinal("nq")); | |
|
elijahtaylor1
2015/11/03 22:38:19
I had a comment on the earlier patch, can you comm
| |
| 44 | |
| 45 UpdateIcon(ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | |
| 46 IDR_APP_DEFAULT_ICON)); | |
| 47 } | |
| 48 | |
| 49 ArcAppItem::~ArcAppItem() { | |
| 50 } | |
| 51 | |
| 52 const char* ArcAppItem::GetItemType() const { | |
| 53 return ArcAppItem::kItemType; | |
| 54 } | |
| 55 | |
| 56 void ArcAppItem::Activate(int event_flags) { | |
| 57 ArcAppPrefs* prefs = ArcAppPrefs::Get(context_); | |
| 58 scoped_ptr<ArcAppPrefs::AppInfo> app_info = prefs->GetApp(id()); | |
| 59 DCHECK(app_info != nullptr); | |
| 60 if (!app_info) { | |
| 61 return; | |
| 62 } | |
| 63 | |
| 64 chromeos::DBusThreadManager::Get()->GetArcBridgeClient()-> | |
| 65 LaunchApp(app_info->package, app_info->activity); | |
| 66 } | |
| 67 | |
| 68 void ArcAppItem::SetReady(bool ready) { | |
| 69 if (ready_ == ready) { | |
| 70 return; | |
| 71 } | |
| 72 ready_ = ready; | |
| 73 UpdateIcon(); | |
| 74 } | |
| 75 | |
| 76 void ArcAppItem::SetName(const std::string& name) { | |
| 77 SetNameAndShortName(name, name); | |
| 78 } | |
| 79 | |
| 80 void ArcAppItem::UpdateIcon() { | |
| 81 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 82 | |
| 83 gfx::ImageSkia icon = image_skia_; | |
| 84 if (!ready_) { | |
| 85 icon = CreateDisabledIcon(icon); | |
| 86 } | |
| 87 | |
| 88 SetIcon(icon); | |
| 89 } | |
| 90 | |
| 91 void ArcAppItem::UpdateIcon(const gfx::ImageSkia* image) { | |
| 92 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 93 | |
| 94 if (!image || image->isNull()) { | |
| 95 return; | |
| 96 } | |
| 97 | |
| 98 std::vector<gfx::ImageSkiaRep> reps = image->image_reps(); | |
| 99 for (const auto& image_rep : reps) { | |
| 100 if (ui::IsSupportedScale(image_rep.scale())) { | |
| 101 image_skia_.RemoveRepresentation(image_rep.scale()); | |
| 102 image_skia_.AddRepresentation(image_rep); | |
| 103 } | |
| 104 } | |
| 105 image_ = gfx::Image(image_skia_); | |
| 106 | |
| 107 UpdateIcon(); | |
| 108 } | |
| 109 | |
| 110 void ArcAppItem::UpdateIcon(ui::ScaleFactor scale_factor, | |
| 111 const base::FilePath& path) { | |
| 112 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 113 | |
| 114 base::Closure task = base::Bind(&ArcAppItem::UpdateIconOnBlockingThread, | |
| 115 base::Unretained(this), | |
| 116 scale_factor, | |
| 117 path); | |
| 118 content::BrowserThread::GetBlockingPool()->PostTask(FROM_HERE, task); | |
| 119 } | |
| 120 | |
| 121 void ArcAppItem::UpdateIconOnBlockingThread(ui::ScaleFactor scale_factor, | |
| 122 const base::FilePath& path) { | |
| 123 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
| 124 | |
| 125 // Read the file from disk. | |
| 126 std::string file_contents; | |
| 127 if (path.empty() || !base::ReadFileToString(path, &file_contents)) { | |
| 128 return; | |
| 129 } | |
| 130 | |
| 131 SkBitmap bitmap; | |
| 132 const unsigned char* data = | |
| 133 reinterpret_cast<const unsigned char*>(file_contents.data()); | |
| 134 gfx::PNGCodec::Decode(data, file_contents.length(), &bitmap); | |
|
elijahtaylor1
2015/11/03 22:38:19
I'm still skeptical that this will pass security a
khmel1
2015/11/12 08:05:29
Agree, I changed icon workflow and implemented uti
| |
| 135 | |
| 136 if (bitmap.isNull() || bitmap.empty()) { | |
| 137 return; | |
| 138 } | |
| 139 | |
| 140 gfx::ImageSkia image_skia; | |
| 141 image_skia.AddRepresentation(gfx::ImageSkiaRep( | |
| 142 bitmap, | |
| 143 ui::GetScaleForScaleFactor(scale_factor))); | |
| 144 | |
| 145 gfx::Image image; | |
| 146 if (!image_skia.isNull()) { | |
| 147 image_skia.MakeThreadSafe(); | |
| 148 image = gfx::Image(image_skia); | |
| 149 } | |
| 150 | |
| 151 base::Closure task = base::Bind(&ArcAppItem::OnIconLoaded, | |
| 152 base::Unretained(this), | |
| 153 image); | |
| 154 content::BrowserThread::PostTask(content::BrowserThread::UI, | |
| 155 FROM_HERE, | |
| 156 task); | |
| 157 } | |
| 158 | |
| 159 void ArcAppItem::OnIconLoaded(const gfx::Image& image_in) { | |
| 160 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 161 | |
| 162 if (image_in.IsEmpty()) { | |
| 163 return; | |
| 164 } | |
| 165 | |
| 166 UpdateIcon(image_in.ToImageSkia()); | |
| 167 } | |
| OLD | NEW |