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