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

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

Issue 11198025: Site specific secondary tiles for Windows 8. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Oops Created 8 years, 2 months 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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/metro_pin_tab_helper.h" 5 #include "chrome/browser/ui/metro_pin_tab_helper.h"
6 6
7 #include "base/base_paths.h"
8 #include "base/bind.h"
9 #include "base/file_path.h"
10 #include "base/file_util.h"
7 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/memory/ref_counted_memory.h"
13 #include "base/path_service.h"
14 #include "base/string_number_conversions.h"
8 #include "base/utf_string_conversions.h" 15 #include "base/utf_string_conversions.h"
16 #include "chrome/browser/favicon/favicon_tab_helper.h"
17 #include "chrome/browser/ui/tab_contents/tab_contents.h"
18 #include "chrome/common/chrome_paths.h"
19 #include "content/public/browser/browser_thread.h"
9 #include "content/public/browser/web_contents.h" 20 #include "content/public/browser/web_contents.h"
21 #include "crypto/sha2.h"
22 #include "ui/gfx/canvas.h"
23 #include "ui/gfx/codec/png_codec.h"
24 #include "ui/gfx/color_analysis.h"
25 #include "ui/gfx/color_utils.h"
26 #include "ui/gfx/image/image.h"
27 #include "ui/gfx/rect.h"
28 #include "ui/gfx/size.h"
10 29
11 #if defined(OS_WIN) 30 #if defined(OS_WIN)
12 #include "base/win/metro.h" 31 #include "base/win/metro.h"
13 #endif 32 #endif
14 33
15 DEFINE_WEB_CONTENTS_USER_DATA_KEY(MetroPinTabHelper) 34 DEFINE_WEB_CONTENTS_USER_DATA_KEY(MetroPinTabHelper)
16 35
36 namespace {
37
38 #if defined(OS_WIN)
39
40 string16 GenerateTileId(const string16& url_str) {
sky 2012/10/22 21:18:00 Add descriptions.
benwells 2012/10/23 10:34:25 Done.
41 uint8 hash[crypto::kSHA256Length];
42 crypto::SHA256HashString(UTF16ToUTF8(url_str), hash, sizeof(hash));
43 std::string hash_str = base::HexEncode(hash, sizeof(hash));
44 return UTF8ToUTF16(hash_str);
45 }
46
47 bool CreateLogoFromFavicon(const gfx::ImageSkia& icon_image,
48 const FilePath& tile_path) {
49 static int kLogoWidth = 120;
50 static int kLogoHeight = 120;
51 static int kBoxWidth = 40;
52 static int kBoxHeight = 40;
53 static int kCaptionHeight = 20;
54 static double kBoxFade = 0.75;
55
56 // Use a canvas to paint the tile logo.
57 gfx::Canvas canvas(gfx::Size(kLogoWidth, kLogoHeight), ui::SCALE_FACTOR_100P,
58 true);
59
60 // Fill the tile logo with the average color from bitmap. To do this we need
61 // to work out the 'average color' which is calculated using PNG encoded data
62 // of the bitmap.
63 SkPaint paint;
64 std::vector<unsigned char> icon_png;
65 if (!gfx::PNGCodec::EncodeBGRASkBitmap(*icon_image.bitmap(), true, &icon_png))
66 return false;
67
68 scoped_refptr<base::RefCountedStaticMemory> icon_mem(
69 new base::RefCountedStaticMemory(&icon_png.front(), icon_png.size()));
70 color_utils::GridSampler sampler;
71 SkColor mean_color =
72 color_utils::CalculateKMeanColorOfPNG(icon_mem, 100, 665, sampler);
73 paint.setColor(mean_color);
74 canvas.DrawRect(gfx::Rect(0, 0, kLogoWidth, kLogoHeight), paint);
75
76 // Now paint a faded square for the favicon to go in.
77 color_utils::HSL shift = {-1, -1, kBoxFade};
78 paint.setColor(color_utils::HSLShift(mean_color, shift));
79 int box_left = (kLogoWidth - kBoxWidth) / 2;
80 int box_top = (kLogoHeight - kCaptionHeight - kBoxHeight) / 2;
81 canvas.DrawRect(gfx::Rect(box_left, box_top, kBoxWidth, kBoxHeight), paint);
82
83 // Now paint the favicon into the tile, leaving some room at the bottom for
84 // the caption.
85 int left = (kLogoWidth - icon_image.width()) / 2;
86 int top = (kLogoHeight - kCaptionHeight - icon_image.height()) / 2;
87 canvas.DrawImageInt(icon_image, left, top);
88
89 SkBitmap logo_bitmap = canvas.ExtractImageRep().sk_bitmap();
90 std::vector<unsigned char> logo_png;
91 if (!gfx::PNGCodec::EncodeBGRASkBitmap(logo_bitmap, true, &logo_png))
92 return false;
93
94 return file_util::WriteFile(tile_path,
95 reinterpret_cast<char*>(&logo_png[0]),
96 logo_png.size()) > 0;
97 }
98
99 FilePath GetLogoPath(const gfx::Image& image,
100 const string16& tile_id) {
101 FilePath logo_dir;
102 DCHECK(PathService::Get(chrome::DIR_USER_DATA, &logo_dir));
103 logo_dir = logo_dir.Append(L"TileImages");
104 if (!file_util::DirectoryExists(logo_dir) &&
105 !file_util::CreateDirectory(logo_dir))
106 return FilePath();
107
108 if (!image.IsEmpty()) {
109 scoped_ptr<gfx::ImageSkia> image_skia(image.CopyImageSkia());
110 image_skia->SetReadOnly();
111 FilePath logo_path = logo_dir.Append(tile_id)
112 .ReplaceExtension(L".png");
113 if (CreateLogoFromFavicon(*image_skia, 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";
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();
129 }
130
131 return logo_path;
132 }
133
134 #endif
135
136 } // namespace
137
138 MetroPinTabHelper::TaskRunner::TaskRunner() {}
139
140 MetroPinTabHelper::TaskRunner::~TaskRunner() {}
141
142 void MetroPinTabHelper::TaskRunner::PinPageToStartScreen(
143 const string16& title,
144 const string16& url,
145 const gfx::Image& image) {
146 #if defined(OS_WIN)
sky 2012/10/22 21:18:00 It would be nice if you add some asserts around th
benwells 2012/10/23 10:34:25 Done.
147 string16 tile_id = GenerateTileId(url);
148 FilePath logo_path = GetLogoPath(image, tile_id);
149 if (logo_path.empty()) {
150 LOG(ERROR) << "Count not create logo tile.";
151 return;
152 }
153
154 HMODULE metro_module = base::win::GetMetroModule();
155 if (metro_module) {
156 typedef void (*MetroPinToStartScreen)(const string16&, const string16&,
157 const string16&, const FilePath&);
158 MetroPinToStartScreen metro_pin_to_start_screen =
159 reinterpret_cast<MetroPinToStartScreen>(
160 ::GetProcAddress(metro_module, "MetroPinToStartScreen"));
161 if (!metro_pin_to_start_screen) {
162 NOTREACHED();
163 return;
164 }
165
166 VLOG(1) << __FUNCTION__ << " calling pin with title: " << title
167 << " and url: " << url;
168 metro_pin_to_start_screen(tile_id, title, url, logo_path);
169 }
170 #endif
171 }
172
17 MetroPinTabHelper::MetroPinTabHelper(content::WebContents* web_contents) 173 MetroPinTabHelper::MetroPinTabHelper(content::WebContents* web_contents)
18 : content::WebContentsObserver(web_contents), 174 : content::WebContentsObserver(web_contents),
19 is_pinned_(false) {} 175 is_pinned_(false),
176 task_runner_(new TaskRunner) {}
20 177
21 MetroPinTabHelper::~MetroPinTabHelper() {} 178 MetroPinTabHelper::~MetroPinTabHelper() {}
22 179
23 void MetroPinTabHelper::TogglePinnedToStartScreen() { 180 void MetroPinTabHelper::TogglePinnedToStartScreen() {
24 #if defined(OS_WIN) 181 UpdatePinnedStateForCurrentURL();
25 HMODULE metro_module = base::win::GetMetroModule(); 182 bool was_pinned = is_pinned_;
26 if (metro_module) {
27 typedef void (*MetroTogglePinnedToStartScreen)(const string16&,
28 const string16&);
29 MetroTogglePinnedToStartScreen metro_toggle_pinned_to_start_screen =
30 reinterpret_cast<MetroTogglePinnedToStartScreen>(
31 ::GetProcAddress(metro_module, "MetroTogglePinnedToStartScreen"));
32 if (!metro_toggle_pinned_to_start_screen) {
33 NOTREACHED();
34 return;
35 }
36 183
37 GURL url = web_contents()->GetURL(); 184 // TODO(benwells): This will update the state incorrectly if the user
38 string16 title = web_contents()->GetTitle(); 185 // cancels. To fix this some sort of callback needs to be introduced as
39 VLOG(1) << __FUNCTION__ << " calling pin with title: " << title 186 // the pinning happens on another thread.
40 << " and url " << UTF8ToUTF16(url.spec()); 187 is_pinned_ = !is_pinned_;
41 metro_toggle_pinned_to_start_screen(title, UTF8ToUTF16(url.spec())); 188
42 // TODO(benwells): This will update the state incorrectly if the user 189 if (was_pinned) {
43 // cancels. To fix this some sort of callback needs to be introduced as 190 UnPinPageFromStartScreen();
44 // the pinning happens on another thread.
45 is_pinned_ = !is_pinned_;
46 return; 191 return;
47 } 192 }
48 #endif 193
194 // TODO(benwells): Handle downloading a larger favicon if there is one.
195 GURL url = web_contents()->GetURL();
196 string16 url_str = UTF8ToUTF16(url.spec());
197 string16 title = web_contents()->GetTitle();
198 TabContents* tab_contents = TabContents::FromWebContents(web_contents());
199 DCHECK(tab_contents);
200 FaviconTabHelper* favicon_tab_helper = FaviconTabHelper::FromWebContents(
201 tab_contents->web_contents());
202 if (favicon_tab_helper->FaviconIsValid()) {
203 gfx::Image favicon = favicon_tab_helper->GetFavicon();
sky 2012/10/22 21:18:00 I believe you want to DeepCopy the ImageSkia and p
benwells 2012/10/23 10:34:25 Done.
204 content::BrowserThread::PostTask(
205 content::BrowserThread::FILE,
206 FROM_HERE,
207 base::Bind(&TaskRunner::PinPageToStartScreen,
208 task_runner_,
209 title,
210 url_str,
211 favicon));
212 return;
213 }
214
215 content::BrowserThread::PostTask(
216 content::BrowserThread::FILE,
217 FROM_HERE,
218 base::Bind(&TaskRunner::PinPageToStartScreen,
219 task_runner_,
220 title,
221 url_str,
222 gfx::Image()));
49 } 223 }
50 224
51 void MetroPinTabHelper::DidNavigateMainFrame( 225 void MetroPinTabHelper::DidNavigateMainFrame(
52 const content::LoadCommittedDetails& /*details*/, 226 const content::LoadCommittedDetails& /*details*/,
53 const content::FrameNavigateParams& /*params*/) { 227 const content::FrameNavigateParams& /*params*/) {
54 UpdatePinnedStateForCurrentURL(); 228 UpdatePinnedStateForCurrentURL();
55 } 229 }
56 230
57 void MetroPinTabHelper::UpdatePinnedStateForCurrentURL() { 231 void MetroPinTabHelper::UpdatePinnedStateForCurrentURL() {
58 #if defined(OS_WIN) 232 #if defined(OS_WIN)
59 HMODULE metro_module = base::win::GetMetroModule(); 233 HMODULE metro_module = base::win::GetMetroModule();
60 if (metro_module) { 234 if (metro_module) {
61 typedef BOOL (*MetroIsPinnedToStartScreen)(const string16&); 235 typedef BOOL (*MetroIsPinnedToStartScreen)(const string16&);
62 MetroIsPinnedToStartScreen metro_is_pinned_to_start_screen = 236 MetroIsPinnedToStartScreen metro_is_pinned_to_start_screen =
63 reinterpret_cast<MetroIsPinnedToStartScreen>( 237 reinterpret_cast<MetroIsPinnedToStartScreen>(
64 ::GetProcAddress(metro_module, "MetroIsPinnedToStartScreen")); 238 ::GetProcAddress(metro_module, "MetroIsPinnedToStartScreen"));
65 if (!metro_is_pinned_to_start_screen) { 239 if (!metro_is_pinned_to_start_screen) {
66 NOTREACHED(); 240 NOTREACHED();
67 return; 241 return;
68 } 242 }
69 243
70 GURL url = web_contents()->GetURL(); 244 GURL url = web_contents()->GetURL();
71 is_pinned_ = metro_is_pinned_to_start_screen(UTF8ToUTF16(url.spec())) != 0; 245 string16 tile_id = GenerateTileId(UTF8ToUTF16(url.spec()));
246 is_pinned_ = metro_is_pinned_to_start_screen(tile_id) != 0;
72 VLOG(1) << __FUNCTION__ << " with url " << UTF8ToUTF16(url.spec()) 247 VLOG(1) << __FUNCTION__ << " with url " << UTF8ToUTF16(url.spec())
73 << " result: " << is_pinned_; 248 << " result: " << is_pinned_;
74 } 249 }
75 #endif 250 #endif
76 } 251 }
252
253 void MetroPinTabHelper::UnPinPageFromStartScreen() {
254 #if defined(OS_WIN)
255 HMODULE metro_module = base::win::GetMetroModule();
256 if (metro_module) {
257 typedef void (*MetroUnPinFromStartScreen)(const string16&);
258 MetroUnPinFromStartScreen metro_un_pin_from_start_screen =
259 reinterpret_cast<MetroUnPinFromStartScreen>(
260 ::GetProcAddress(metro_module, "MetroUnPinFromStartScreen"));
261 if (!metro_un_pin_from_start_screen) {
262 NOTREACHED();
263 return;
264 }
265
266 GURL url = web_contents()->GetURL();
267 VLOG(1) << __FUNCTION__ << " calling unpin with url: "
268 << UTF8ToUTF16(url.spec());
269 string16 tile_id = GenerateTileId(UTF8ToUTF16(url.spec()));
270 metro_un_pin_from_start_screen(tile_id);
271 }
272 #endif
273 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698