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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/metro_pin_tab_helper.cc
diff --git a/chrome/browser/ui/metro_pin_tab_helper.cc b/chrome/browser/ui/metro_pin_tab_helper.cc
index 7644355c6a5b8506d5e333410e3cdf638f928b9b..ca2416ce007ed7011f1d7c80cac4909bfcd4d079 100644
--- a/chrome/browser/ui/metro_pin_tab_helper.cc
+++ b/chrome/browser/ui/metro_pin_tab_helper.cc
@@ -4,9 +4,28 @@
#include "chrome/browser/ui/metro_pin_tab_helper.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_memory.h"
+#include "base/path_service.h"
+#include "base/string_number_conversions.h"
#include "base/utf_string_conversions.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"
#if defined(OS_WIN)
#include "base/win/metro.h"
@@ -14,40 +33,195 @@
DEFINE_WEB_CONTENTS_USER_DATA_KEY(MetroPinTabHelper)
-MetroPinTabHelper::MetroPinTabHelper(content::WebContents* web_contents)
- : content::WebContentsObserver(web_contents),
- is_pinned_(false) {}
+namespace {
-MetroPinTabHelper::~MetroPinTabHelper() {}
+#if defined(OS_WIN)
-void MetroPinTabHelper::TogglePinnedToStartScreen() {
+string16 GenerateTileId(const string16& url_str) {
sky 2012/10/22 21:18:00 Add descriptions.
benwells 2012/10/23 10:34:25 Done.
+ 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);
+}
+
+bool CreateLogoFromFavicon(const gfx::ImageSkia& icon_image,
+ const FilePath& tile_path) {
+ static int kLogoWidth = 120;
+ 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);
+ 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;
+}
+
+FilePath GetLogoPath(const gfx::Image& image,
+ 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.IsEmpty()) {
+ scoped_ptr<gfx::ImageSkia> image_skia(image.CopyImageSkia());
+ image_skia->SetReadOnly();
+ FilePath logo_path = logo_dir.Append(tile_id)
+ .ReplaceExtension(L".png");
+ if (CreateLogoFromFavicon(*image_skia, 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";
+ 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();
+ }
+
+ return logo_path;
+}
+
+#endif
+
+} // namespace
+
+MetroPinTabHelper::TaskRunner::TaskRunner() {}
+
+MetroPinTabHelper::TaskRunner::~TaskRunner() {}
+
+void MetroPinTabHelper::TaskRunner::PinPageToStartScreen(
+ const string16& title,
+ const string16& url,
+ const gfx::Image& image) {
#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.
+ 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 (*MetroTogglePinnedToStartScreen)(const string16&,
- const string16&);
- MetroTogglePinnedToStartScreen metro_toggle_pinned_to_start_screen =
- reinterpret_cast<MetroTogglePinnedToStartScreen>(
- ::GetProcAddress(metro_module, "MetroTogglePinnedToStartScreen"));
- if (!metro_toggle_pinned_to_start_screen) {
+ 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;
}
- GURL url = web_contents()->GetURL();
- string16 title = web_contents()->GetTitle();
VLOG(1) << __FUNCTION__ << " calling pin with title: " << title
- << " and url " << UTF8ToUTF16(url.spec());
- metro_toggle_pinned_to_start_screen(title, UTF8ToUTF16(url.spec()));
- // 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_;
- return;
+ << " and url: " << url;
+ metro_pin_to_start_screen(tile_id, title, url, logo_path);
}
#endif
}
+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();
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.
+ content::BrowserThread::PostTask(
+ content::BrowserThread::FILE,
+ FROM_HERE,
+ base::Bind(&TaskRunner::PinPageToStartScreen,
+ task_runner_,
+ title,
+ url_str,
+ favicon));
+ return;
+ }
+
+ content::BrowserThread::PostTask(
+ content::BrowserThread::FILE,
+ FROM_HERE,
+ base::Bind(&TaskRunner::PinPageToStartScreen,
+ task_runner_,
+ title,
+ url_str,
+ gfx::Image()));
+}
+
void MetroPinTabHelper::DidNavigateMainFrame(
const content::LoadCommittedDetails& /*details*/,
const content::FrameNavigateParams& /*params*/) {
@@ -68,9 +242,32 @@ void MetroPinTabHelper::UpdatePinnedStateForCurrentURL() {
}
GURL url = web_contents()->GetURL();
- is_pinned_ = metro_is_pinned_to_start_screen(UTF8ToUTF16(url.spec())) != 0;
+ 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_;
}
#endif
}
+
+void MetroPinTabHelper::UnPinPageFromStartScreen() {
+#if defined(OS_WIN)
+ 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);
+ }
+#endif
+}

Powered by Google App Engine
This is Rietveld 408576698