Index: chrome/browser/ui/metro_pin_tab_helper_win.cc |
diff --git a/chrome/browser/ui/metro_pin_tab_helper_win.cc b/chrome/browser/ui/metro_pin_tab_helper_win.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..e7d3277239e778b84488a23fd18fdd3cc7862c8e |
--- /dev/null |
+++ b/chrome/browser/ui/metro_pin_tab_helper_win.cc |
@@ -0,0 +1,284 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/ui/metro_pin_tab_helper_win.h" |
+ |
+#include "base/base_paths.h" |
+#include "base/bind.h" |
+#include "base/file_path.h" |
+#include "base/file_util.h" |
+#include "base/logging.h" |
+#include "base/memory/ref_counted.h" |
+#include "base/memory/ref_counted_memory.h" |
+#include "base/path_service.h" |
+#include "base/string_number_conversions.h" |
+#include "base/utf_string_conversions.h" |
+#include "base/win/metro.h" |
+#include "chrome/browser/favicon/favicon_tab_helper.h" |
+#include "chrome/browser/ui/tab_contents/tab_contents.h" |
+#include "chrome/common/chrome_paths.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "content/public/browser/web_contents.h" |
+#include "crypto/sha2.h" |
+#include "ui/gfx/canvas.h" |
+#include "ui/gfx/codec/png_codec.h" |
+#include "ui/gfx/color_analysis.h" |
+#include "ui/gfx/color_utils.h" |
+#include "ui/gfx/image/image.h" |
+#include "ui/gfx/rect.h" |
+#include "ui/gfx/size.h" |
+ |
+DEFINE_WEB_CONTENTS_USER_DATA_KEY(MetroPinTabHelper) |
+ |
+namespace { |
+ |
+// Generate an ID for the tile based on |url_str|. The ID is simply a hash of |
+// the URL. |
+string16 GenerateTileId(const string16& url_str) { |
+ uint8 hash[crypto::kSHA256Length]; |
+ crypto::SHA256HashString(UTF16ToUTF8(url_str), hash, sizeof(hash)); |
+ std::string hash_str = base::HexEncode(hash, sizeof(hash)); |
+ return UTF8ToUTF16(hash_str); |
+} |
+ |
+// Create a tile logo from |icon_image|, and save it to |tile_path|. |
+bool CreateLogoFromFavicon(const gfx::ImageSkia& icon_image, |
+ const FilePath& tile_path) { |
+ 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.
|
+ static int kLogoHeight = 120; |
+ static int kBoxWidth = 40; |
+ static int kBoxHeight = 40; |
+ static int kCaptionHeight = 20; |
+ static double kBoxFade = 0.75; |
+ |
+ // Use a canvas to paint the tile logo. |
+ gfx::Canvas canvas(gfx::Size(kLogoWidth, kLogoHeight), ui::SCALE_FACTOR_100P, |
+ true); |
+ |
+ // Fill the tile logo with the average color from bitmap. To do this we need |
+ // to work out the 'average color' which is calculated using PNG encoded data |
+ // of the bitmap. |
+ SkPaint paint; |
+ std::vector<unsigned char> icon_png; |
+ if (!gfx::PNGCodec::EncodeBGRASkBitmap(*icon_image.bitmap(), true, &icon_png)) |
+ return false; |
+ |
+ scoped_refptr<base::RefCountedStaticMemory> icon_mem( |
+ new base::RefCountedStaticMemory(&icon_png.front(), icon_png.size())); |
+ color_utils::GridSampler sampler; |
+ SkColor mean_color = |
+ 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.
|
+ paint.setColor(mean_color); |
+ canvas.DrawRect(gfx::Rect(0, 0, kLogoWidth, kLogoHeight), paint); |
+ |
+ // Now paint a faded square for the favicon to go in. |
+ color_utils::HSL shift = {-1, -1, kBoxFade}; |
+ paint.setColor(color_utils::HSLShift(mean_color, shift)); |
+ int box_left = (kLogoWidth - kBoxWidth) / 2; |
+ int box_top = (kLogoHeight - kCaptionHeight - kBoxHeight) / 2; |
+ canvas.DrawRect(gfx::Rect(box_left, box_top, kBoxWidth, kBoxHeight), paint); |
+ |
+ // Now paint the favicon into the tile, leaving some room at the bottom for |
+ // the caption. |
+ int left = (kLogoWidth - icon_image.width()) / 2; |
+ int top = (kLogoHeight - kCaptionHeight - icon_image.height()) / 2; |
+ canvas.DrawImageInt(icon_image, left, top); |
+ |
+ SkBitmap logo_bitmap = canvas.ExtractImageRep().sk_bitmap(); |
+ std::vector<unsigned char> logo_png; |
+ if (!gfx::PNGCodec::EncodeBGRASkBitmap(logo_bitmap, true, &logo_png)) |
+ return false; |
+ |
+ return file_util::WriteFile(tile_path, |
+ reinterpret_cast<char*>(&logo_png[0]), |
+ logo_png.size()) > 0; |
+} |
+ |
+// For the given |image| and |tile_id|, return the path to a tile image. This |
+// will either generate a site specific logo or return a path to the backup tile |
+// image. |
+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
|
+ const string16& tile_id) { |
+ FilePath logo_dir; |
+ DCHECK(PathService::Get(chrome::DIR_USER_DATA, &logo_dir)); |
+ logo_dir = logo_dir.Append(L"TileImages"); |
+ if (!file_util::DirectoryExists(logo_dir) && |
+ !file_util::CreateDirectory(logo_dir)) |
+ return FilePath(); |
+ |
+ if (!image.isNull()) { |
+ FilePath logo_path = logo_dir.Append(tile_id) |
+ .ReplaceExtension(L".png"); |
+ if (CreateLogoFromFavicon(image, logo_path)) |
+ return logo_path; |
+ } |
+ |
+ // Use default tile image. If it doesn't exist, copy it out of the install |
+ // folder. The version in the install folder is not used as it may disappear |
+ // after an upgrade, causing tiles to lose their images if Windows rebuilds |
+ // its tile image cache. |
+ 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.
|
+ FilePath logo_path = logo_dir.Append(kDefaultLogoFileName); |
+ if (!file_util::PathExists(logo_path)) { |
+ FilePath default_logo_path; |
+ DCHECK(PathService::Get(base::DIR_MODULE, &default_logo_path)); |
+ default_logo_path = default_logo_path.Append(kDefaultLogoFileName); |
+ if (!file_util::CopyFile(default_logo_path, logo_path)) |
+ 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.
|
+ } |
+ |
+ return logo_path; |
+} |
+ |
+} // namespace |
+ |
+class MetroPinTabHelper::TaskRunner |
+ : public base::RefCountedThreadSafe<TaskRunner> { |
+ public: |
+ TaskRunner(); |
+ |
+ void PinPageToStartScreen(const string16& title, |
+ const string16& url, |
+ const gfx::ImageSkia& image); |
+ |
+ private: |
+ ~TaskRunner(); |
+ |
+ friend class base::RefCountedThreadSafe<TaskRunner>; |
+ DISALLOW_COPY_AND_ASSIGN(TaskRunner); |
+}; |
+ |
+MetroPinTabHelper::TaskRunner::TaskRunner() {} |
+ |
+MetroPinTabHelper::TaskRunner::~TaskRunner() {} |
+ |
cpu_(ooo_6.6-7.5)
2012/10/23 20:17:01
you can fold the do nothing funtions like 152 and
|
+void MetroPinTabHelper::TaskRunner::PinPageToStartScreen( |
+ const string16& title, |
+ const string16& url, |
+ const gfx::ImageSkia& image) { |
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
+ |
+ string16 tile_id = GenerateTileId(url); |
+ FilePath logo_path = GetLogoPath(image, tile_id); |
+ if (logo_path.empty()) { |
+ LOG(ERROR) << "Count not create logo tile."; |
+ return; |
+ } |
+ |
+ HMODULE metro_module = base::win::GetMetroModule(); |
+ if (metro_module) { |
+ typedef void (*MetroPinToStartScreen)(const string16&, const string16&, |
+ const string16&, const FilePath&); |
+ MetroPinToStartScreen metro_pin_to_start_screen = |
+ reinterpret_cast<MetroPinToStartScreen>( |
+ ::GetProcAddress(metro_module, "MetroPinToStartScreen")); |
+ if (!metro_pin_to_start_screen) { |
+ NOTREACHED(); |
+ return; |
+ } |
+ |
+ VLOG(1) << __FUNCTION__ << " calling pin with title: " << title |
+ << " and url: " << url; |
+ metro_pin_to_start_screen(tile_id, title, url, logo_path); |
+ } |
+} |
+ |
+MetroPinTabHelper::MetroPinTabHelper(content::WebContents* web_contents) |
+ : content::WebContentsObserver(web_contents), |
+ is_pinned_(false), |
+ task_runner_(new TaskRunner) {} |
+ |
+MetroPinTabHelper::~MetroPinTabHelper() {} |
+ |
+void MetroPinTabHelper::TogglePinnedToStartScreen() { |
+ UpdatePinnedStateForCurrentURL(); |
+ bool was_pinned = is_pinned_; |
+ |
+ // TODO(benwells): This will update the state incorrectly if the user |
+ // cancels. To fix this some sort of callback needs to be introduced as |
+ // the pinning happens on another thread. |
+ is_pinned_ = !is_pinned_; |
+ |
+ if (was_pinned) { |
+ UnPinPageFromStartScreen(); |
+ return; |
+ } |
+ |
+ // TODO(benwells): Handle downloading a larger favicon if there is one. |
+ GURL url = web_contents()->GetURL(); |
+ string16 url_str = UTF8ToUTF16(url.spec()); |
+ string16 title = web_contents()->GetTitle(); |
+ TabContents* tab_contents = TabContents::FromWebContents(web_contents()); |
+ DCHECK(tab_contents); |
+ FaviconTabHelper* favicon_tab_helper = FaviconTabHelper::FromWebContents( |
+ tab_contents->web_contents()); |
+ if (favicon_tab_helper->FaviconIsValid()) { |
+ gfx::Image favicon = favicon_tab_helper->GetFavicon(); |
+ gfx::ImageSkia favicon_skia = favicon.AsImageSkia().DeepCopy(); |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::FILE, |
+ FROM_HERE, |
+ base::Bind(&TaskRunner::PinPageToStartScreen, |
+ task_runner_, |
+ title, |
+ url_str, |
+ favicon_skia)); |
+ return; |
+ } |
+ |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::FILE, |
+ FROM_HERE, |
+ base::Bind(&TaskRunner::PinPageToStartScreen, |
+ task_runner_, |
+ title, |
+ url_str, |
+ 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
|
+} |
+ |
+void MetroPinTabHelper::DidNavigateMainFrame( |
+ const content::LoadCommittedDetails& /*details*/, |
+ const content::FrameNavigateParams& /*params*/) { |
+ UpdatePinnedStateForCurrentURL(); |
+} |
+ |
+void MetroPinTabHelper::UpdatePinnedStateForCurrentURL() { |
+ HMODULE metro_module = base::win::GetMetroModule(); |
+ 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.
|
+ typedef BOOL (*MetroIsPinnedToStartScreen)(const string16&); |
+ MetroIsPinnedToStartScreen metro_is_pinned_to_start_screen = |
+ reinterpret_cast<MetroIsPinnedToStartScreen>( |
+ ::GetProcAddress(metro_module, "MetroIsPinnedToStartScreen")); |
+ if (!metro_is_pinned_to_start_screen) { |
+ NOTREACHED(); |
+ return; |
+ } |
+ |
+ GURL url = web_contents()->GetURL(); |
+ string16 tile_id = GenerateTileId(UTF8ToUTF16(url.spec())); |
+ is_pinned_ = metro_is_pinned_to_start_screen(tile_id) != 0; |
+ VLOG(1) << __FUNCTION__ << " with url " << UTF8ToUTF16(url.spec()) |
+ << " result: " << is_pinned_; |
+ } |
+} |
+ |
+void MetroPinTabHelper::UnPinPageFromStartScreen() { |
+ HMODULE metro_module = base::win::GetMetroModule(); |
+ if (metro_module) { |
+ typedef void (*MetroUnPinFromStartScreen)(const string16&); |
+ MetroUnPinFromStartScreen metro_un_pin_from_start_screen = |
+ reinterpret_cast<MetroUnPinFromStartScreen>( |
+ ::GetProcAddress(metro_module, "MetroUnPinFromStartScreen")); |
+ if (!metro_un_pin_from_start_screen) { |
+ NOTREACHED(); |
+ return; |
+ } |
+ |
+ GURL url = web_contents()->GetURL(); |
+ VLOG(1) << __FUNCTION__ << " calling unpin with url: " |
+ << UTF8ToUTF16(url.spec()); |
+ string16 tile_id = GenerateTileId(UTF8ToUTF16(url.spec())); |
+ metro_un_pin_from_start_screen(tile_id); |
+ } |
+} |