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