| OLD | NEW |
| 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_win.h" | 5 #include "chrome/browser/ui/metro_pin_tab_helper_win.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/base_paths.h" | 9 #include "base/base_paths.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 // Once this happens this downloader has done its job, so delete it. | 356 // Once this happens this downloader has done its job, so delete it. |
| 357 if (in_progress_requests_.empty()) { | 357 if (in_progress_requests_.empty()) { |
| 358 scoped_refptr<PinPageTaskRunner> runner( | 358 scoped_refptr<PinPageTaskRunner> runner( |
| 359 new PinPageTaskRunner(title_, url_, best_candidate_)); | 359 new PinPageTaskRunner(title_, url_, best_candidate_)); |
| 360 runner->Run(); | 360 runner->Run(); |
| 361 helper_->FaviconDownloaderFinished(); | 361 helper_->FaviconDownloaderFinished(); |
| 362 } | 362 } |
| 363 } | 363 } |
| 364 | 364 |
| 365 MetroPinTabHelper::MetroPinTabHelper(content::WebContents* web_contents) | 365 MetroPinTabHelper::MetroPinTabHelper(content::WebContents* web_contents) |
| 366 : content::WebContentsObserver(web_contents), | 366 : content::WebContentsObserver(web_contents) {} |
| 367 is_pinned_(false) {} | |
| 368 | 367 |
| 369 MetroPinTabHelper::~MetroPinTabHelper() {} | 368 MetroPinTabHelper::~MetroPinTabHelper() {} |
| 370 | 369 |
| 370 bool MetroPinTabHelper::IsPinned() const { |
| 371 HMODULE metro_module = base::win::GetMetroModule(); |
| 372 if (!metro_module) |
| 373 return false; |
| 374 |
| 375 typedef BOOL (*MetroIsPinnedToStartScreen)(const string16&); |
| 376 MetroIsPinnedToStartScreen metro_is_pinned_to_start_screen = |
| 377 reinterpret_cast<MetroIsPinnedToStartScreen>( |
| 378 ::GetProcAddress(metro_module, "MetroIsPinnedToStartScreen")); |
| 379 if (!metro_is_pinned_to_start_screen) { |
| 380 NOTREACHED(); |
| 381 return false; |
| 382 } |
| 383 |
| 384 GURL url = web_contents()->GetURL(); |
| 385 string16 tile_id = GenerateTileId(UTF8ToUTF16(url.spec())); |
| 386 return metro_is_pinned_to_start_screen(tile_id) != 0; |
| 387 } |
| 388 |
| 371 void MetroPinTabHelper::TogglePinnedToStartScreen() { | 389 void MetroPinTabHelper::TogglePinnedToStartScreen() { |
| 372 UpdatePinnedStateForCurrentURL(); | 390 if (IsPinned()) { |
| 373 bool was_pinned = is_pinned_; | |
| 374 | |
| 375 // TODO(benwells): This will update the state incorrectly if the user | |
| 376 // cancels. To fix this some sort of callback needs to be introduced as | |
| 377 // the pinning happens on another thread. | |
| 378 is_pinned_ = !is_pinned_; | |
| 379 | |
| 380 if (was_pinned) { | |
| 381 UnPinPageFromStartScreen(); | 391 UnPinPageFromStartScreen(); |
| 382 return; | 392 return; |
| 383 } | 393 } |
| 384 | 394 |
| 385 GURL url = web_contents()->GetURL(); | 395 GURL url = web_contents()->GetURL(); |
| 386 string16 url_str = UTF8ToUTF16(url.spec()); | 396 string16 url_str = UTF8ToUTF16(url.spec()); |
| 387 string16 title = web_contents()->GetTitle(); | 397 string16 title = web_contents()->GetTitle(); |
| 388 gfx::ImageSkia favicon; | 398 gfx::ImageSkia favicon; |
| 389 FaviconTabHelper* favicon_tab_helper = FaviconTabHelper::FromWebContents( | 399 FaviconTabHelper* favicon_tab_helper = FaviconTabHelper::FromWebContents( |
| 390 web_contents()); | 400 web_contents()); |
| 391 if (favicon_tab_helper->FaviconIsValid()) | 401 if (favicon_tab_helper->FaviconIsValid()) |
| 392 favicon = favicon_tab_helper->GetFavicon().AsImageSkia().DeepCopy(); | 402 favicon = favicon_tab_helper->GetFavicon().AsImageSkia().DeepCopy(); |
| 393 | 403 |
| 394 favicon_downloader_.reset(new FaviconDownloader(this, title, url_str, | 404 favicon_downloader_.reset(new FaviconDownloader(this, title, url_str, |
| 395 favicon)); | 405 favicon)); |
| 396 favicon_downloader_->Start(web_contents()->GetRenderViewHost(), | 406 favicon_downloader_->Start(web_contents()->GetRenderViewHost(), |
| 397 favicon_url_candidates_); | 407 favicon_url_candidates_); |
| 398 } | 408 } |
| 399 | 409 |
| 400 void MetroPinTabHelper::DidNavigateMainFrame( | 410 void MetroPinTabHelper::DidNavigateMainFrame( |
| 401 const content::LoadCommittedDetails& /*details*/, | 411 const content::LoadCommittedDetails& /*details*/, |
| 402 const content::FrameNavigateParams& /*params*/) { | 412 const content::FrameNavigateParams& /*params*/) { |
| 403 UpdatePinnedStateForCurrentURL(); | |
| 404 // Cancel any outstanding pin operations once the user navigates away from | 413 // Cancel any outstanding pin operations once the user navigates away from |
| 405 // the page. | 414 // the page. |
| 406 if (favicon_downloader_.get()) | 415 if (favicon_downloader_.get()) |
| 407 favicon_downloader_.reset(); | 416 favicon_downloader_.reset(); |
| 408 // Any candidate favicons we have are now out of date so clear them. | 417 // Any candidate favicons we have are now out of date so clear them. |
| 409 favicon_url_candidates_.clear(); | 418 favicon_url_candidates_.clear(); |
| 410 } | 419 } |
| 411 | 420 |
| 412 bool MetroPinTabHelper::OnMessageReceived(const IPC::Message& message) { | 421 bool MetroPinTabHelper::OnMessageReceived(const IPC::Message& message) { |
| 413 bool message_handled = false; // Allow other handlers to receive these. | 422 bool message_handled = false; // Allow other handlers to receive these. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 429 int id, | 438 int id, |
| 430 const GURL& image_url, | 439 const GURL& image_url, |
| 431 bool errored, | 440 bool errored, |
| 432 int requested_size, | 441 int requested_size, |
| 433 const std::vector<SkBitmap>& bitmaps) { | 442 const std::vector<SkBitmap>& bitmaps) { |
| 434 if (favicon_downloader_.get()) | 443 if (favicon_downloader_.get()) |
| 435 favicon_downloader_->OnDidDownloadFavicon(id, image_url, errored, | 444 favicon_downloader_->OnDidDownloadFavicon(id, image_url, errored, |
| 436 requested_size, bitmaps); | 445 requested_size, bitmaps); |
| 437 } | 446 } |
| 438 | 447 |
| 439 void MetroPinTabHelper::UpdatePinnedStateForCurrentURL() { | |
| 440 HMODULE metro_module = base::win::GetMetroModule(); | |
| 441 if (!metro_module) | |
| 442 return; | |
| 443 | |
| 444 typedef BOOL (*MetroIsPinnedToStartScreen)(const string16&); | |
| 445 MetroIsPinnedToStartScreen metro_is_pinned_to_start_screen = | |
| 446 reinterpret_cast<MetroIsPinnedToStartScreen>( | |
| 447 ::GetProcAddress(metro_module, "MetroIsPinnedToStartScreen")); | |
| 448 if (!metro_is_pinned_to_start_screen) { | |
| 449 NOTREACHED(); | |
| 450 return; | |
| 451 } | |
| 452 | |
| 453 GURL url = web_contents()->GetURL(); | |
| 454 string16 tile_id = GenerateTileId(UTF8ToUTF16(url.spec())); | |
| 455 is_pinned_ = metro_is_pinned_to_start_screen(tile_id) != 0; | |
| 456 } | |
| 457 | |
| 458 void MetroPinTabHelper::UnPinPageFromStartScreen() { | 448 void MetroPinTabHelper::UnPinPageFromStartScreen() { |
| 459 HMODULE metro_module = base::win::GetMetroModule(); | 449 HMODULE metro_module = base::win::GetMetroModule(); |
| 460 if (!metro_module) | 450 if (!metro_module) |
| 461 return; | 451 return; |
| 462 | 452 |
| 463 typedef void (*MetroUnPinFromStartScreen)(const string16&); | 453 typedef void (*MetroUnPinFromStartScreen)(const string16&); |
| 464 MetroUnPinFromStartScreen metro_un_pin_from_start_screen = | 454 MetroUnPinFromStartScreen metro_un_pin_from_start_screen = |
| 465 reinterpret_cast<MetroUnPinFromStartScreen>( | 455 reinterpret_cast<MetroUnPinFromStartScreen>( |
| 466 ::GetProcAddress(metro_module, "MetroUnPinFromStartScreen")); | 456 ::GetProcAddress(metro_module, "MetroUnPinFromStartScreen")); |
| 467 if (!metro_un_pin_from_start_screen) { | 457 if (!metro_un_pin_from_start_screen) { |
| 468 NOTREACHED(); | 458 NOTREACHED(); |
| 469 return; | 459 return; |
| 470 } | 460 } |
| 471 | 461 |
| 472 GURL url = web_contents()->GetURL(); | 462 GURL url = web_contents()->GetURL(); |
| 473 string16 tile_id = GenerateTileId(UTF8ToUTF16(url.spec())); | 463 string16 tile_id = GenerateTileId(UTF8ToUTF16(url.spec())); |
| 474 metro_un_pin_from_start_screen(tile_id); | 464 metro_un_pin_from_start_screen(tile_id); |
| 475 } | 465 } |
| 476 | 466 |
| 477 void MetroPinTabHelper::FaviconDownloaderFinished() { | 467 void MetroPinTabHelper::FaviconDownloaderFinished() { |
| 478 favicon_downloader_.reset(); | 468 favicon_downloader_.reset(); |
| 479 } | 469 } |
| OLD | NEW |