Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(282)

Side by Side Diff: chrome/browser/ui/metro_pin_tab_helper_win.cc

Issue 11198025: Site specific secondary tiles for Windows 8. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Compile and cleanup Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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/metro_pin_tab_helper_win.h"
6
7 #include "base/base_paths.h"
8 #include "base/bind.h"
9 #include "base/file_path.h"
10 #include "base/file_util.h"
11 #include "base/logging.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/ref_counted_memory.h"
14 #include "base/path_service.h"
15 #include "base/string_number_conversions.h"
16 #include "base/utf_string_conversions.h"
17 #include "base/win/metro.h"
18 #include "chrome/browser/favicon/favicon_tab_helper.h"
19 #include "chrome/browser/ui/tab_contents/tab_contents.h"
20 #include "chrome/common/chrome_paths.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "content/public/browser/web_contents.h"
23 #include "crypto/sha2.h"
24 #include "ui/gfx/canvas.h"
25 #include "ui/gfx/codec/png_codec.h"
26 #include "ui/gfx/color_analysis.h"
27 #include "ui/gfx/color_utils.h"
28 #include "ui/gfx/image/image.h"
29 #include "ui/gfx/rect.h"
30 #include "ui/gfx/size.h"
31
32 DEFINE_WEB_CONTENTS_USER_DATA_KEY(MetroPinTabHelper)
33
34 namespace {
35
36 // Generate an ID for the tile based on |url_str|. The ID is simply a hash of
37 // the URL.
38 string16 GenerateTileId(const string16& url_str) {
39 uint8 hash[crypto::kSHA256Length];
40 crypto::SHA256HashString(UTF16ToUTF8(url_str), hash, sizeof(hash));
41 std::string hash_str = base::HexEncode(hash, sizeof(hash));
42 return UTF8ToUTF16(hash_str);
43 }
44
45 // Create a tile logo from |icon_image|, and save it to |tile_path|.
46 bool CreateLogoFromFavicon(const gfx::ImageSkia& icon_image,
47 const FilePath& tile_path) {
48 static int kLogoWidth = 120;
cpu_(ooo_6.6-7.5) 2012/10/23 20:17:01 you mean const int xxx = yy ?
benwells 2012/10/24 00:48:18 Done.
49 static int kLogoHeight = 120;
50 static int kBoxWidth = 40;
51 static int kBoxHeight = 40;
52 static int kCaptionHeight = 20;
53 static double kBoxFade = 0.75;
54
55 // Use a canvas to paint the tile logo.
56 gfx::Canvas canvas(gfx::Size(kLogoWidth, kLogoHeight), ui::SCALE_FACTOR_100P,
57 true);
58
59 // Fill the tile logo with the average color from bitmap. To do this we need
60 // to work out the 'average color' which is calculated using PNG encoded data
61 // of the bitmap.
62 SkPaint paint;
63 std::vector<unsigned char> icon_png;
64 if (!gfx::PNGCodec::EncodeBGRASkBitmap(*icon_image.bitmap(), true, &icon_png))
65 return false;
66
67 scoped_refptr<base::RefCountedStaticMemory> icon_mem(
68 new base::RefCountedStaticMemory(&icon_png.front(), icon_png.size()));
69 color_utils::GridSampler sampler;
70 SkColor mean_color =
71 color_utils::CalculateKMeanColorOfPNG(icon_mem, 100, 665, sampler);
cpu_(ooo_6.6-7.5) 2012/10/23 20:17:01 100 and 665 would be nice to have in constants abo
benwells 2012/10/24 00:48:18 Done.
72 paint.setColor(mean_color);
73 canvas.DrawRect(gfx::Rect(0, 0, kLogoWidth, kLogoHeight), paint);
74
75 // Now paint a faded square for the favicon to go in.
76 color_utils::HSL shift = {-1, -1, kBoxFade};
77 paint.setColor(color_utils::HSLShift(mean_color, shift));
78 int box_left = (kLogoWidth - kBoxWidth) / 2;
79 int box_top = (kLogoHeight - kCaptionHeight - kBoxHeight) / 2;
80 canvas.DrawRect(gfx::Rect(box_left, box_top, kBoxWidth, kBoxHeight), paint);
81
82 // Now paint the favicon into the tile, leaving some room at the bottom for
83 // the caption.
84 int left = (kLogoWidth - icon_image.width()) / 2;
85 int top = (kLogoHeight - kCaptionHeight - icon_image.height()) / 2;
86 canvas.DrawImageInt(icon_image, left, top);
87
88 SkBitmap logo_bitmap = canvas.ExtractImageRep().sk_bitmap();
89 std::vector<unsigned char> logo_png;
90 if (!gfx::PNGCodec::EncodeBGRASkBitmap(logo_bitmap, true, &logo_png))
91 return false;
92
93 return file_util::WriteFile(tile_path,
94 reinterpret_cast<char*>(&logo_png[0]),
95 logo_png.size()) > 0;
96 }
97
98 // For the given |image| and |tile_id|, return the path to a tile image. This
99 // will either generate a site specific logo or return a path to the backup tile
100 // image.
101 FilePath GetLogoPath(const gfx::ImageSkia& image,
cpu_(ooo_6.6-7.5) 2012/10/23 20:17:01 seems the name could be better, like MakeLogo, as
benwells 2012/10/24 00:48:18 Coming up with a name was hard as it did a variety
102 const string16& tile_id) {
103 FilePath logo_dir;
104 DCHECK(PathService::Get(chrome::DIR_USER_DATA, &logo_dir));
105 logo_dir = logo_dir.Append(L"TileImages");
106 if (!file_util::DirectoryExists(logo_dir) &&
107 !file_util::CreateDirectory(logo_dir))
108 return FilePath();
109
110 if (!image.isNull()) {
111 FilePath logo_path = logo_dir.Append(tile_id)
112 .ReplaceExtension(L".png");
113 if (CreateLogoFromFavicon(image, logo_path))
114 return logo_path;
115 }
116
117 // Use default tile image. If it doesn't exist, copy it out of the install
118 // folder. The version in the install folder is not used as it may disappear
119 // after an upgrade, causing tiles to lose their images if Windows rebuilds
120 // its tile image cache.
121 static const wchar_t kDefaultLogoFileName[] = L"SecondaryTile.png";
cpu_(ooo_6.6-7.5) 2012/10/23 20:17:01 don't need the static and its not used in most of
benwells 2012/10/24 00:48:18 Done.
122 FilePath logo_path = logo_dir.Append(kDefaultLogoFileName);
123 if (!file_util::PathExists(logo_path)) {
124 FilePath default_logo_path;
125 DCHECK(PathService::Get(base::DIR_MODULE, &default_logo_path));
126 default_logo_path = default_logo_path.Append(kDefaultLogoFileName);
127 if (!file_util::CopyFile(default_logo_path, logo_path))
128 return FilePath();
cpu_(ooo_6.6-7.5) 2012/10/23 20:17:01 please extract 121-129 into a freestanding functio
benwells 2012/10/24 00:48:18 Done.
129 }
130
131 return logo_path;
132 }
133
134 } // namespace
135
136 class MetroPinTabHelper::TaskRunner
137 : public base::RefCountedThreadSafe<TaskRunner> {
138 public:
139 TaskRunner();
140
141 void PinPageToStartScreen(const string16& title,
142 const string16& url,
143 const gfx::ImageSkia& image);
144
145 private:
146 ~TaskRunner();
147
148 friend class base::RefCountedThreadSafe<TaskRunner>;
149 DISALLOW_COPY_AND_ASSIGN(TaskRunner);
150 };
151
152 MetroPinTabHelper::TaskRunner::TaskRunner() {}
153
154 MetroPinTabHelper::TaskRunner::~TaskRunner() {}
155
cpu_(ooo_6.6-7.5) 2012/10/23 20:17:01 you can fold the do nothing funtions like 152 and
156 void MetroPinTabHelper::TaskRunner::PinPageToStartScreen(
157 const string16& title,
158 const string16& url,
159 const gfx::ImageSkia& image) {
160 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
161
162 string16 tile_id = GenerateTileId(url);
163 FilePath logo_path = GetLogoPath(image, tile_id);
164 if (logo_path.empty()) {
165 LOG(ERROR) << "Count not create logo tile.";
166 return;
167 }
168
169 HMODULE metro_module = base::win::GetMetroModule();
170 if (metro_module) {
171 typedef void (*MetroPinToStartScreen)(const string16&, const string16&,
172 const string16&, const FilePath&);
173 MetroPinToStartScreen metro_pin_to_start_screen =
174 reinterpret_cast<MetroPinToStartScreen>(
175 ::GetProcAddress(metro_module, "MetroPinToStartScreen"));
176 if (!metro_pin_to_start_screen) {
177 NOTREACHED();
178 return;
179 }
180
181 VLOG(1) << __FUNCTION__ << " calling pin with title: " << title
182 << " and url: " << url;
183 metro_pin_to_start_screen(tile_id, title, url, logo_path);
184 }
185 }
186
187 MetroPinTabHelper::MetroPinTabHelper(content::WebContents* web_contents)
188 : content::WebContentsObserver(web_contents),
189 is_pinned_(false),
190 task_runner_(new TaskRunner) {}
191
192 MetroPinTabHelper::~MetroPinTabHelper() {}
193
194 void MetroPinTabHelper::TogglePinnedToStartScreen() {
195 UpdatePinnedStateForCurrentURL();
196 bool was_pinned = is_pinned_;
197
198 // TODO(benwells): This will update the state incorrectly if the user
199 // cancels. To fix this some sort of callback needs to be introduced as
200 // the pinning happens on another thread.
201 is_pinned_ = !is_pinned_;
202
203 if (was_pinned) {
204 UnPinPageFromStartScreen();
205 return;
206 }
207
208 // TODO(benwells): Handle downloading a larger favicon if there is one.
209 GURL url = web_contents()->GetURL();
210 string16 url_str = UTF8ToUTF16(url.spec());
211 string16 title = web_contents()->GetTitle();
212 TabContents* tab_contents = TabContents::FromWebContents(web_contents());
213 DCHECK(tab_contents);
214 FaviconTabHelper* favicon_tab_helper = FaviconTabHelper::FromWebContents(
215 tab_contents->web_contents());
216 if (favicon_tab_helper->FaviconIsValid()) {
217 gfx::Image favicon = favicon_tab_helper->GetFavicon();
218 gfx::ImageSkia favicon_skia = favicon.AsImageSkia().DeepCopy();
219 content::BrowserThread::PostTask(
220 content::BrowserThread::FILE,
221 FROM_HERE,
222 base::Bind(&TaskRunner::PinPageToStartScreen,
223 task_runner_,
224 title,
225 url_str,
226 favicon_skia));
227 return;
228 }
229
230 content::BrowserThread::PostTask(
231 content::BrowserThread::FILE,
232 FROM_HERE,
233 base::Bind(&TaskRunner::PinPageToStartScreen,
234 task_runner_,
235 title,
236 url_str,
237 gfx::ImageSkia()));
cpu_(ooo_6.6-7.5) 2012/10/23 20:17:01 I don't know how cheap is to make a gfx::ImageSkia
benwells 2012/10/24 00:48:18 As discussed over chat, ImageSkia is cheap. The Im
238 }
239
240 void MetroPinTabHelper::DidNavigateMainFrame(
241 const content::LoadCommittedDetails& /*details*/,
242 const content::FrameNavigateParams& /*params*/) {
243 UpdatePinnedStateForCurrentURL();
244 }
245
246 void MetroPinTabHelper::UpdatePinnedStateForCurrentURL() {
247 HMODULE metro_module = base::win::GetMetroModule();
248 if (metro_module) {
cpu_(ooo_6.6-7.5) 2012/10/23 20:17:01 rather do if (!metro_module) return; To have le
benwells 2012/10/24 00:48:18 Done.
249 typedef BOOL (*MetroIsPinnedToStartScreen)(const string16&);
250 MetroIsPinnedToStartScreen metro_is_pinned_to_start_screen =
251 reinterpret_cast<MetroIsPinnedToStartScreen>(
252 ::GetProcAddress(metro_module, "MetroIsPinnedToStartScreen"));
253 if (!metro_is_pinned_to_start_screen) {
254 NOTREACHED();
255 return;
256 }
257
258 GURL url = web_contents()->GetURL();
259 string16 tile_id = GenerateTileId(UTF8ToUTF16(url.spec()));
260 is_pinned_ = metro_is_pinned_to_start_screen(tile_id) != 0;
261 VLOG(1) << __FUNCTION__ << " with url " << UTF8ToUTF16(url.spec())
262 << " result: " << is_pinned_;
263 }
264 }
265
266 void MetroPinTabHelper::UnPinPageFromStartScreen() {
267 HMODULE metro_module = base::win::GetMetroModule();
268 if (metro_module) {
269 typedef void (*MetroUnPinFromStartScreen)(const string16&);
270 MetroUnPinFromStartScreen metro_un_pin_from_start_screen =
271 reinterpret_cast<MetroUnPinFromStartScreen>(
272 ::GetProcAddress(metro_module, "MetroUnPinFromStartScreen"));
273 if (!metro_un_pin_from_start_screen) {
274 NOTREACHED();
275 return;
276 }
277
278 GURL url = web_contents()->GetURL();
279 VLOG(1) << __FUNCTION__ << " calling unpin with url: "
280 << UTF8ToUTF16(url.spec());
281 string16 tile_id = GenerateTileId(UTF8ToUTF16(url.spec()));
282 metro_un_pin_from_start_screen(tile_id);
283 }
284 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/metro_pin_tab_helper_win.h ('k') | chrome/browser/ui/tab_contents/tab_contents.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698