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 "stdafx.h" | 5 #include "stdafx.h" |
6 #include "secondary_tile.h" | 6 #include "secondary_tile.h" |
7 | 7 |
8 #include <windows.ui.startscreen.h> | 8 #include <windows.ui.startscreen.h> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
13 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
14 #include "win8/metro_driver/chrome_app_view.h" | 14 #include "win8/metro_driver/chrome_app_view.h" |
15 #include "win8/metro_driver/winrt_utils.h" | 15 #include "win8/metro_driver/winrt_utils.h" |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 void DeleteTileFromStartScreen(const string16& tile_id) { | 19 using base::win::MetroPinUmaResultCallback; |
| 20 |
| 21 // Callback for asynchronous pin requests. |
| 22 class TileRequestCompleter { |
| 23 public: |
| 24 enum PinType { |
| 25 PIN, |
| 26 UNPIN |
| 27 }; |
| 28 TileRequestCompleter(PinType type, const MetroPinUmaResultCallback& callback) |
| 29 : type_(type), callback_(callback) {} |
| 30 |
| 31 void Complete(mswr::ComPtr<winfoundtn::IAsyncOperation<bool>>& completion); |
| 32 |
| 33 private: |
| 34 // Callback that responds to user input on the pin request pop-up. This will |
| 35 // run |callback_|, then delete |this| before returning. |
| 36 HRESULT Respond(winfoundtn::IAsyncOperation<bool>* async, |
| 37 AsyncStatus status); |
| 38 |
| 39 PinType type_; |
| 40 MetroPinUmaResultCallback callback_; |
| 41 }; |
| 42 |
| 43 void TileRequestCompleter::Complete( |
| 44 mswr::ComPtr<winfoundtn::IAsyncOperation<bool>>& completion) { |
| 45 typedef winfoundtn::IAsyncOperationCompletedHandler<bool> RequestDoneType; |
| 46 mswr::ComPtr<RequestDoneType> handler(mswr::Callback<RequestDoneType>( |
| 47 this, &TileRequestCompleter::Respond)); |
| 48 DCHECK(handler.Get() != NULL); |
| 49 HRESULT hr = completion->put_Completed(handler.Get()); |
| 50 CheckHR(hr, "Failed to put_Completed"); |
| 51 } |
| 52 |
| 53 HRESULT TileRequestCompleter::Respond(winfoundtn::IAsyncOperation<bool>* async, |
| 54 AsyncStatus status) { |
| 55 base::win::MetroSecondaryTilePinUmaResult pin_state = |
| 56 base::win::METRO_PIN_STATE_NONE; |
| 57 |
| 58 if (status == Completed) { |
| 59 unsigned char result; |
| 60 CheckHR(async->GetResults(&result)); |
| 61 LOG(INFO) << __FUNCTION__ << " result " << static_cast<int>(result); |
| 62 switch (result) { |
| 63 case 0: |
| 64 pin_state = type_ == PIN ? |
| 65 base::win::METRO_PIN_RESULT_CANCEL : |
| 66 base::win::METRO_UNPIN_RESULT_CANCEL; |
| 67 break; |
| 68 case 1: |
| 69 pin_state = type_ == PIN ? |
| 70 base::win::METRO_PIN_RESULT_OK : |
| 71 base::win::METRO_UNPIN_RESULT_OK; |
| 72 break; |
| 73 default: |
| 74 pin_state = type_ == PIN ? |
| 75 base::win::METRO_PIN_RESULT_OTHER : |
| 76 base::win::METRO_UNPIN_RESULT_OTHER; |
| 77 break; |
| 78 } |
| 79 } else { |
| 80 LOG(ERROR) << __FUNCTION__ << " Unexpected async status " << status; |
| 81 pin_state = type_ == PIN ? |
| 82 base::win::METRO_PIN_RESULT_ERROR : |
| 83 base::win::METRO_UNPIN_RESULT_ERROR; |
| 84 } |
| 85 callback_.Run(pin_state); |
| 86 |
| 87 delete this; |
| 88 return S_OK; |
| 89 } |
| 90 |
| 91 void DeleteTileFromStartScreen(const string16& tile_id, |
| 92 const MetroPinUmaResultCallback& callback) { |
20 DVLOG(1) << __FUNCTION__; | 93 DVLOG(1) << __FUNCTION__; |
21 mswr::ComPtr<winui::StartScreen::ISecondaryTileFactory> tile_factory; | 94 mswr::ComPtr<winui::StartScreen::ISecondaryTileFactory> tile_factory; |
22 HRESULT hr = winrt_utils::CreateActivationFactory( | 95 HRESULT hr = winrt_utils::CreateActivationFactory( |
23 RuntimeClass_Windows_UI_StartScreen_SecondaryTile, | 96 RuntimeClass_Windows_UI_StartScreen_SecondaryTile, |
24 tile_factory.GetAddressOf()); | 97 tile_factory.GetAddressOf()); |
25 CheckHR(hr, "Failed to create instance of ISecondaryTileFactory"); | 98 CheckHR(hr, "Failed to create instance of ISecondaryTileFactory"); |
26 | 99 |
27 mswrw::HString id; | 100 mswrw::HString id; |
28 id.Attach(MakeHString(tile_id)); | 101 id.Attach(MakeHString(tile_id)); |
29 | 102 |
30 mswr::ComPtr<winui::StartScreen::ISecondaryTile> tile; | 103 mswr::ComPtr<winui::StartScreen::ISecondaryTile> tile; |
31 hr = tile_factory->CreateWithId(id.Get(), tile.GetAddressOf()); | 104 hr = tile_factory->CreateWithId(id.Get(), tile.GetAddressOf()); |
32 CheckHR(hr, "Failed to create tile"); | 105 CheckHR(hr, "Failed to create tile"); |
33 | 106 |
34 mswr::ComPtr<winfoundtn::IAsyncOperation<bool>> completion; | 107 mswr::ComPtr<winfoundtn::IAsyncOperation<bool>> completion; |
35 hr = tile->RequestDeleteAsync(completion.GetAddressOf()); | 108 hr = tile->RequestDeleteAsync(completion.GetAddressOf()); |
36 CheckHR(hr, "RequestDeleteAsync failed"); | 109 CheckHR(hr, "RequestDeleteAsync failed"); |
37 | 110 |
38 typedef winfoundtn::IAsyncOperationCompletedHandler<bool> RequestDoneType; | 111 if (FAILED(hr)) { |
39 mswr::ComPtr<RequestDoneType> handler(mswr::Callback<RequestDoneType>( | 112 callback.Run(base::win::METRO_UNPIN_REQUEST_SHOW_ERROR); |
40 globals.view, &ChromeAppView::TileRequestCreateDone)); | 113 return; |
41 DCHECK(handler.Get() != NULL); | 114 } |
42 hr = completion->put_Completed(handler.Get()); | 115 |
43 CheckHR(hr, "Failed to put_Completed"); | 116 // Deleted in TileRequestCompleter::Respond when the async operation |
| 117 // completes. |
| 118 TileRequestCompleter* completer = |
| 119 new TileRequestCompleter(TileRequestCompleter::UNPIN, callback); |
| 120 completer->Complete(completion); |
44 } | 121 } |
45 | 122 |
46 void CreateTileOnStartScreen(const string16& tile_id, | 123 void CreateTileOnStartScreen(const string16& tile_id, |
47 const string16& title_str, | 124 const string16& title_str, |
48 const string16& url_str, | 125 const string16& url_str, |
49 const FilePath& logo_path) { | 126 const FilePath& logo_path, |
| 127 const MetroPinUmaResultCallback& callback) { |
50 VLOG(1) << __FUNCTION__; | 128 VLOG(1) << __FUNCTION__; |
51 | 129 |
52 mswr::ComPtr<winui::StartScreen::ISecondaryTileFactory> tile_factory; | 130 mswr::ComPtr<winui::StartScreen::ISecondaryTileFactory> tile_factory; |
53 HRESULT hr = winrt_utils::CreateActivationFactory( | 131 HRESULT hr = winrt_utils::CreateActivationFactory( |
54 RuntimeClass_Windows_UI_StartScreen_SecondaryTile, | 132 RuntimeClass_Windows_UI_StartScreen_SecondaryTile, |
55 tile_factory.GetAddressOf()); | 133 tile_factory.GetAddressOf()); |
56 CheckHR(hr, "Failed to create instance of ISecondaryTileFactory"); | 134 CheckHR(hr, "Failed to create instance of ISecondaryTileFactory"); |
57 | 135 |
58 winui::StartScreen::TileOptions options = | 136 winui::StartScreen::TileOptions options = |
59 winui::StartScreen::TileOptions_ShowNameOnLogo; | 137 winui::StartScreen::TileOptions_ShowNameOnLogo; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 tile.GetAddressOf()); | 170 tile.GetAddressOf()); |
93 CheckHR(hr, "Failed to create tile"); | 171 CheckHR(hr, "Failed to create tile"); |
94 | 172 |
95 hr = tile->put_ForegroundText(winui::StartScreen::ForegroundText_Light); | 173 hr = tile->put_ForegroundText(winui::StartScreen::ForegroundText_Light); |
96 CheckHR(hr, "Failed to change foreground text color"); | 174 CheckHR(hr, "Failed to change foreground text color"); |
97 | 175 |
98 mswr::ComPtr<winfoundtn::IAsyncOperation<bool>> completion; | 176 mswr::ComPtr<winfoundtn::IAsyncOperation<bool>> completion; |
99 hr = tile->RequestCreateAsync(completion.GetAddressOf()); | 177 hr = tile->RequestCreateAsync(completion.GetAddressOf()); |
100 CheckHR(hr, "RequestCreateAsync failed"); | 178 CheckHR(hr, "RequestCreateAsync failed"); |
101 | 179 |
102 typedef winfoundtn::IAsyncOperationCompletedHandler<bool> RequestDoneType; | 180 if (FAILED(hr)) { |
103 mswr::ComPtr<RequestDoneType> handler(mswr::Callback<RequestDoneType>( | 181 callback.Run(base::win::METRO_PIN_REQUEST_SHOW_ERROR); |
104 globals.view, &ChromeAppView::TileRequestCreateDone)); | 182 return; |
105 DCHECK(handler.Get() != NULL); | 183 } |
106 hr = completion->put_Completed(handler.Get()); | 184 |
107 CheckHR(hr, "Failed to put_Completed"); | 185 // Deleted in TileRequestCompleter::Respond when the async operation |
| 186 // completes. |
| 187 TileRequestCompleter* completer = |
| 188 new TileRequestCompleter(TileRequestCompleter::PIN, callback); |
| 189 completer->Complete(completion); |
108 } | 190 } |
109 | 191 |
110 } // namespace | 192 } // namespace |
111 | 193 |
112 BOOL MetroIsPinnedToStartScreen(const string16& tile_id) { | 194 BOOL MetroIsPinnedToStartScreen(const string16& tile_id) { |
113 mswr::ComPtr<winui::StartScreen::ISecondaryTileStatics> tile_statics; | 195 mswr::ComPtr<winui::StartScreen::ISecondaryTileStatics> tile_statics; |
114 HRESULT hr = winrt_utils::CreateActivationFactory( | 196 HRESULT hr = winrt_utils::CreateActivationFactory( |
115 RuntimeClass_Windows_UI_StartScreen_SecondaryTile, | 197 RuntimeClass_Windows_UI_StartScreen_SecondaryTile, |
116 tile_statics.GetAddressOf()); | 198 tile_statics.GetAddressOf()); |
117 CheckHR(hr, "Failed to create instance of ISecondaryTileStatics"); | 199 CheckHR(hr, "Failed to create instance of ISecondaryTileStatics"); |
118 | 200 |
119 boolean exists; | 201 boolean exists; |
120 hr = tile_statics->Exists(MakeHString(tile_id), &exists); | 202 hr = tile_statics->Exists(MakeHString(tile_id), &exists); |
121 CheckHR(hr, "ISecondaryTileStatics.Exists failed"); | 203 CheckHR(hr, "ISecondaryTileStatics.Exists failed"); |
122 return exists; | 204 return exists; |
123 } | 205 } |
124 | 206 |
125 void MetroUnPinFromStartScreen(const string16& tile_id) { | 207 void MetroUnPinFromStartScreen(const string16& tile_id, |
| 208 const MetroPinUmaResultCallback& callback) { |
126 globals.appview_msg_loop->PostTask( | 209 globals.appview_msg_loop->PostTask( |
127 FROM_HERE, base::Bind(&DeleteTileFromStartScreen, tile_id)); | 210 FROM_HERE, base::Bind(&DeleteTileFromStartScreen, |
| 211 tile_id, |
| 212 callback)); |
128 } | 213 } |
129 | 214 |
130 void MetroPinToStartScreen(const string16& tile_id, | 215 void MetroPinToStartScreen(const string16& tile_id, |
131 const string16& title, | 216 const string16& title, |
132 const string16& url, | 217 const string16& url, |
133 const FilePath& logo_path) { | 218 const FilePath& logo_path, |
| 219 const MetroPinUmaResultCallback& callback) { |
134 globals.appview_msg_loop->PostTask( | 220 globals.appview_msg_loop->PostTask( |
135 FROM_HERE, base::Bind(&CreateTileOnStartScreen, | 221 FROM_HERE, base::Bind(&CreateTileOnStartScreen, |
136 tile_id, | 222 tile_id, |
137 title, | 223 title, |
138 url, | 224 url, |
139 logo_path)); | 225 logo_path, |
| 226 callback)); |
140 } | 227 } |
OLD | NEW |