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 "ui/app_list/resources/grit/app_list_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 enabled) | |
36 : app_list::AppListItem(id), | |
37 context_(context), | |
38 enabled_(enabled), | |
39 weak_ptr_factory_(this) { | |
40 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
41 | |
42 SetName(name); | |
43 set_position(syncer::StringOrdinal("nq")); | |
elijahtaylor1
2015/10/28 06:32:38
can you comment how this interacts with other apps
| |
44 | |
45 UpdateIcon(ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | |
46 IDR_APP_LIST_ARC_APP_DEFAULT)); | |
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::SetEnabled(bool enabled) { | |
69 if (enabled_ == enabled) { | |
70 return; | |
71 } | |
72 enabled_ = enabled; | |
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 (!enabled_) { | |
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::UpdateIconFromFileThread, | |
115 base::Unretained(this), | |
116 scale_factor, | |
117 path); | |
118 content::BrowserThread::PostTask(content::BrowserThread::FILE, | |
119 FROM_HERE, | |
120 task); | |
121 } | |
122 | |
123 void ArcAppItem::UpdateIconFromFileThread(ui::ScaleFactor scale_factor, | |
124 const base::FilePath& path) { | |
125 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE); | |
126 | |
127 // Read the file from disk. | |
128 std::string file_contents; | |
129 if (path.empty() || !base::ReadFileToString(path, &file_contents)) { | |
130 return; | |
131 } | |
132 | |
133 SkBitmap bitmap; | |
134 const unsigned char* data = | |
135 reinterpret_cast<const unsigned char*>(file_contents.data()); | |
136 gfx::PNGCodec::Decode(data, file_contents.length(), &bitmap); | |
elijahtaylor1
2015/10/28 06:32:38
I don't know if this is considered safe to do in t
khmel1
2015/10/29 08:12:18
I changed similar to extensions::ImageLoader by us
| |
137 | |
138 if (bitmap.isNull() || bitmap.empty()) { | |
139 return; | |
140 } | |
141 | |
142 gfx::ImageSkia image_skia; | |
143 image_skia.AddRepresentation(gfx::ImageSkiaRep( | |
144 bitmap, | |
145 ui::GetScaleForScaleFactor(scale_factor))); | |
146 | |
147 gfx::Image image; | |
148 if (!image_skia.isNull()) { | |
149 image_skia.MakeThreadSafe(); | |
150 image = gfx::Image(image_skia); | |
151 } | |
152 | |
153 base::Closure task = base::Bind(&ArcAppItem::OnIconLoaded, | |
154 base::Unretained(this), | |
155 image); | |
156 content::BrowserThread::PostTask(content::BrowserThread::UI, | |
157 FROM_HERE, | |
158 task); | |
159 } | |
160 | |
161 void ArcAppItem::OnIconLoaded(const gfx::Image& image_in) { | |
162 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
163 | |
164 if (image_in.IsEmpty()) { | |
165 return; | |
166 } | |
167 | |
168 UpdateIcon(image_in.ToImageSkia()); | |
169 } | |
OLD | NEW |