| 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.h" | 5 #include "chrome/browser/ui/metro_pin_tab_helper.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/browser/ui/metro_pinned_state_observer.h" |
| 9 #include "content/public/browser/web_contents.h" | 10 #include "content/public/browser/web_contents.h" |
| 10 | 11 |
| 11 #if defined(OS_WIN) | 12 #if defined(OS_WIN) |
| 12 #include "base/win/metro.h" | 13 #include "base/win/metro.h" |
| 13 #endif | 14 #endif |
| 14 | 15 |
| 15 MetroPinTabHelper::MetroPinTabHelper(content::WebContents* web_contents) | 16 MetroPinTabHelper::MetroPinTabHelper(content::WebContents* web_contents) |
| 16 : content::WebContentsObserver(web_contents), | 17 : content::WebContentsObserver(web_contents), |
| 17 is_pinned_(false) {} | 18 is_pinned_(false), |
| 19 observer_(NULL) {} |
| 18 | 20 |
| 19 MetroPinTabHelper::~MetroPinTabHelper() {} | 21 MetroPinTabHelper::~MetroPinTabHelper() {} |
| 20 | 22 |
| 21 void MetroPinTabHelper::TogglePinnedToStartScreen() { | 23 void MetroPinTabHelper::TogglePinnedToStartScreen() { |
| 22 #if defined(OS_WIN) | 24 #if defined(OS_WIN) |
| 23 HMODULE metro_module = base::win::GetMetroModule(); | 25 HMODULE metro_module = base::win::GetMetroModule(); |
| 24 if (metro_module) { | 26 if (metro_module) { |
| 25 typedef void (*MetroTogglePinnedToStartScreen)(const string16&, | 27 typedef void (*MetroTogglePinnedToStartScreen)(const string16&, |
| 26 const string16&); | 28 const string16&); |
| 27 MetroTogglePinnedToStartScreen metro_toggle_pinned_to_start_screen = | 29 MetroTogglePinnedToStartScreen metro_toggle_pinned_to_start_screen = |
| 28 reinterpret_cast<MetroTogglePinnedToStartScreen>( | 30 reinterpret_cast<MetroTogglePinnedToStartScreen>( |
| 29 ::GetProcAddress(metro_module, "MetroTogglePinnedToStartScreen")); | 31 ::GetProcAddress(metro_module, "MetroTogglePinnedToStartScreen")); |
| 30 if (!metro_toggle_pinned_to_start_screen) { | 32 if (!metro_toggle_pinned_to_start_screen) { |
| 31 NOTREACHED(); | 33 NOTREACHED(); |
| 32 return; | 34 return; |
| 33 } | 35 } |
| 34 | 36 |
| 35 GURL url = web_contents()->GetURL(); | 37 GURL url = web_contents()->GetURL(); |
| 36 string16 title = web_contents()->GetTitle(); | 38 string16 title = web_contents()->GetTitle(); |
| 37 VLOG(1) << __FUNCTION__ << " calling pin with title: " << title | 39 VLOG(1) << __FUNCTION__ << " calling pin with title: " << title |
| 38 << " and url " << UTF8ToUTF16(url.spec()); | 40 << " and url " << UTF8ToUTF16(url.spec()); |
| 39 metro_toggle_pinned_to_start_screen(title, UTF8ToUTF16(url.spec())); | 41 metro_toggle_pinned_to_start_screen(title, UTF8ToUTF16(url.spec())); |
| 40 // TODO(benwells): This will update the state incorrectly if the user | 42 // TODO(benwells): This will update the state incorrectly if the user |
| 41 // cancels. To fix this some sort of callback needs to be introduced as | 43 // cancels. To fix this some sort of callback needs to be introduced as |
| 42 // the pinning happens on another thread. | 44 // the pinning happens on another thread. |
| 43 is_pinned_ = !is_pinned_; | 45 SetIsPinned(!is_pinned_); |
| 44 return; | |
| 45 } | 46 } |
| 46 #endif | 47 #endif |
| 47 } | 48 } |
| 48 | 49 |
| 49 void MetroPinTabHelper::DidNavigateMainFrame( | 50 void MetroPinTabHelper::DidNavigateMainFrame( |
| 50 const content::LoadCommittedDetails& /*details*/, | 51 const content::LoadCommittedDetails& /*details*/, |
| 51 const content::FrameNavigateParams& /*params*/) { | 52 const content::FrameNavigateParams& /*params*/) { |
| 52 UpdatePinnedStateForCurrentURL(); | 53 UpdatePinnedStateForCurrentURL(); |
| 53 } | 54 } |
| 54 | 55 |
| 55 void MetroPinTabHelper::UpdatePinnedStateForCurrentURL() { | 56 void MetroPinTabHelper::UpdatePinnedStateForCurrentURL() { |
| 56 #if defined(OS_WIN) | 57 #if defined(OS_WIN) |
| 57 HMODULE metro_module = base::win::GetMetroModule(); | 58 HMODULE metro_module = base::win::GetMetroModule(); |
| 58 if (metro_module) { | 59 if (metro_module) { |
| 59 typedef BOOL (*MetroIsPinnedToStartScreen)(const string16&); | 60 typedef BOOL (*MetroIsPinnedToStartScreen)(const string16&); |
| 60 MetroIsPinnedToStartScreen metro_is_pinned_to_start_screen = | 61 MetroIsPinnedToStartScreen metro_is_pinned_to_start_screen = |
| 61 reinterpret_cast<MetroIsPinnedToStartScreen>( | 62 reinterpret_cast<MetroIsPinnedToStartScreen>( |
| 62 ::GetProcAddress(metro_module, "MetroIsPinnedToStartScreen")); | 63 ::GetProcAddress(metro_module, "MetroIsPinnedToStartScreen")); |
| 63 if (!metro_is_pinned_to_start_screen) { | 64 if (!metro_is_pinned_to_start_screen) { |
| 64 NOTREACHED(); | 65 NOTREACHED(); |
| 65 return; | 66 return; |
| 66 } | 67 } |
| 67 | 68 |
| 68 GURL url = web_contents()->GetURL(); | 69 GURL url = web_contents()->GetURL(); |
| 69 is_pinned_ = metro_is_pinned_to_start_screen(UTF8ToUTF16(url.spec())) != 0; | 70 SetIsPinned(metro_is_pinned_to_start_screen(UTF8ToUTF16(url.spec())) != 0); |
| 70 VLOG(1) << __FUNCTION__ << " with url " << UTF8ToUTF16(url.spec()) | 71 VLOG(1) << __FUNCTION__ << " with url " << UTF8ToUTF16(url.spec()) |
| 71 << " result: " << is_pinned_; | 72 << " result: " << is_pinned_; |
| 72 } | 73 } |
| 73 #endif | 74 #endif |
| 74 } | 75 } |
| 76 |
| 77 void MetroPinTabHelper::SetIsPinned(bool is_pinned) { |
| 78 bool was_pinned = is_pinned_; |
| 79 is_pinned_ = is_pinned; |
| 80 if (observer_ && is_pinned_ != was_pinned) |
| 81 observer_->MetroPinnedStateChanged(web_contents(), is_pinned_); |
| 82 } |
| OLD | NEW |