| 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/base_paths.h" | |
| 11 #include "base/bind.h" | 10 #include "base/bind.h" |
| 12 #include "base/file_path.h" | |
| 13 #include "base/logging.h" | 11 #include "base/logging.h" |
| 14 #include "base/path_service.h" | |
| 15 #include "base/string_number_conversions.h" | |
| 16 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 17 #include "crypto/sha2.h" | |
| 18 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
| 19 #include "win8/metro_driver/chrome_app_view.h" | 14 #include "win8/metro_driver/chrome_app_view.h" |
| 20 #include "win8/metro_driver/winrt_utils.h" | 15 #include "win8/metro_driver/winrt_utils.h" |
| 21 | 16 |
| 22 namespace { | 17 namespace { |
| 23 | 18 |
| 24 string16 GenerateTileId(const string16& url_str) { | 19 void DeleteTileFromStartScreen(const string16& tile_id) { |
| 25 uint8 hash[crypto::kSHA256Length]; | |
| 26 crypto::SHA256HashString(UTF16ToUTF8(url_str), hash, sizeof(hash)); | |
| 27 std::string hash_str = base::HexEncode(hash, sizeof(hash)); | |
| 28 return UTF8ToUTF16(hash_str); | |
| 29 } | |
| 30 | |
| 31 string16 GetLogoUrlString() { | |
| 32 FilePath module_path; | |
| 33 PathService::Get(base::DIR_MODULE, &module_path); | |
| 34 string16 scheme(L"ms-appx:///"); | |
| 35 return scheme.append(module_path.BaseName().value()) | |
| 36 .append(L"/SecondaryTile.png"); | |
| 37 } | |
| 38 | |
| 39 BOOL IsPinnedToStartScreen(const string16& url_str) { | |
| 40 mswr::ComPtr<winui::StartScreen::ISecondaryTileStatics> tile_statics; | |
| 41 HRESULT hr = winrt_utils::CreateActivationFactory( | |
| 42 RuntimeClass_Windows_UI_StartScreen_SecondaryTile, | |
| 43 tile_statics.GetAddressOf()); | |
| 44 CheckHR(hr, "Failed to create instance of ISecondaryTileStatics"); | |
| 45 | |
| 46 boolean exists; | |
| 47 hr = tile_statics->Exists(MakeHString(GenerateTileId(url_str)), &exists); | |
| 48 CheckHR(hr, "ISecondaryTileStatics.Exists failed"); | |
| 49 return exists; | |
| 50 } | |
| 51 | |
| 52 void DeleteTileFromStartScreen(const string16& url_str) { | |
| 53 DVLOG(1) << __FUNCTION__; | 20 DVLOG(1) << __FUNCTION__; |
| 54 mswr::ComPtr<winui::StartScreen::ISecondaryTileFactory> tile_factory; | 21 mswr::ComPtr<winui::StartScreen::ISecondaryTileFactory> tile_factory; |
| 55 HRESULT hr = winrt_utils::CreateActivationFactory( | 22 HRESULT hr = winrt_utils::CreateActivationFactory( |
| 56 RuntimeClass_Windows_UI_StartScreen_SecondaryTile, | 23 RuntimeClass_Windows_UI_StartScreen_SecondaryTile, |
| 57 tile_factory.GetAddressOf()); | 24 tile_factory.GetAddressOf()); |
| 58 CheckHR(hr, "Failed to create instance of ISecondaryTileFactory"); | 25 CheckHR(hr, "Failed to create instance of ISecondaryTileFactory"); |
| 59 | 26 |
| 60 mswrw::HString id; | 27 mswrw::HString id; |
| 61 id.Attach(MakeHString(GenerateTileId(url_str))); | 28 id.Attach(MakeHString(tile_id)); |
| 62 | 29 |
| 63 mswr::ComPtr<winui::StartScreen::ISecondaryTile> tile; | 30 mswr::ComPtr<winui::StartScreen::ISecondaryTile> tile; |
| 64 hr = tile_factory->CreateWithId(id.Get(), tile.GetAddressOf()); | 31 hr = tile_factory->CreateWithId(id.Get(), tile.GetAddressOf()); |
| 65 CheckHR(hr, "Failed to create tile"); | 32 CheckHR(hr, "Failed to create tile"); |
| 66 | 33 |
| 67 mswr::ComPtr<winfoundtn::IAsyncOperation<bool>> completion; | 34 mswr::ComPtr<winfoundtn::IAsyncOperation<bool>> completion; |
| 68 hr = tile->RequestDeleteAsync(completion.GetAddressOf()); | 35 hr = tile->RequestDeleteAsync(completion.GetAddressOf()); |
| 69 CheckHR(hr, "RequestDeleteAsync failed"); | 36 CheckHR(hr, "RequestDeleteAsync failed"); |
| 70 | 37 |
| 71 typedef winfoundtn::IAsyncOperationCompletedHandler<bool> RequestDoneType; | 38 typedef winfoundtn::IAsyncOperationCompletedHandler<bool> RequestDoneType; |
| 72 mswr::ComPtr<RequestDoneType> handler(mswr::Callback<RequestDoneType>( | 39 mswr::ComPtr<RequestDoneType> handler(mswr::Callback<RequestDoneType>( |
| 73 globals.view, &ChromeAppView::TileRequestCreateDone)); | 40 globals.view, &ChromeAppView::TileRequestCreateDone)); |
| 74 DCHECK(handler.Get() != NULL); | 41 DCHECK(handler.Get() != NULL); |
| 75 hr = completion->put_Completed(handler.Get()); | 42 hr = completion->put_Completed(handler.Get()); |
| 76 CheckHR(hr, "Failed to put_Completed"); | 43 CheckHR(hr, "Failed to put_Completed"); |
| 77 } | 44 } |
| 78 | 45 |
| 79 void CreateTileOnStartScreen(const string16& title_str, | 46 void CreateTileOnStartScreen(const string16& tile_id, |
| 80 const string16& url_str) { | 47 const string16& title_str, |
| 48 const string16& url_str, |
| 49 const FilePath& logo_path) { |
| 81 VLOG(1) << __FUNCTION__; | 50 VLOG(1) << __FUNCTION__; |
| 51 |
| 82 mswr::ComPtr<winui::StartScreen::ISecondaryTileFactory> tile_factory; | 52 mswr::ComPtr<winui::StartScreen::ISecondaryTileFactory> tile_factory; |
| 83 HRESULT hr = winrt_utils::CreateActivationFactory( | 53 HRESULT hr = winrt_utils::CreateActivationFactory( |
| 84 RuntimeClass_Windows_UI_StartScreen_SecondaryTile, | 54 RuntimeClass_Windows_UI_StartScreen_SecondaryTile, |
| 85 tile_factory.GetAddressOf()); | 55 tile_factory.GetAddressOf()); |
| 86 CheckHR(hr, "Failed to create instance of ISecondaryTileFactory"); | 56 CheckHR(hr, "Failed to create instance of ISecondaryTileFactory"); |
| 87 | 57 |
| 88 winui::StartScreen::TileOptions options = | 58 winui::StartScreen::TileOptions options = |
| 89 winui::StartScreen::TileOptions_ShowNameOnLogo; | 59 winui::StartScreen::TileOptions_ShowNameOnLogo; |
| 90 mswrw::HString title; | 60 mswrw::HString title; |
| 91 title.Attach(MakeHString(title_str)); | 61 title.Attach(MakeHString(title_str)); |
| 62 |
| 92 mswrw::HString id; | 63 mswrw::HString id; |
| 93 id.Attach(MakeHString(GenerateTileId(url_str))); | 64 id.Attach(MakeHString(tile_id)); |
| 65 |
| 94 mswrw::HString args; | 66 mswrw::HString args; |
| 95 // The url is just passed into the tile agruments as is. Metro and desktop | 67 // The url is just passed into the tile agruments as is. Metro and desktop |
| 96 // chrome will see the arguments as command line parameters. | 68 // chrome will see the arguments as command line parameters. |
| 97 // A GURL is used to ensure any spaces are properly escaped. | 69 // A GURL is used to ensure any spaces are properly escaped. |
| 98 GURL url(url_str); | 70 GURL url(url_str); |
| 99 args.Attach(MakeHString(UTF8ToUTF16(url.spec()))); | 71 args.Attach(MakeHString(UTF8ToUTF16(url.spec()))); |
| 100 | 72 |
| 101 mswr::ComPtr<winfoundtn::IUriRuntimeClassFactory> uri_factory; | 73 mswr::ComPtr<winfoundtn::IUriRuntimeClassFactory> uri_factory; |
| 102 hr = winrt_utils::CreateActivationFactory( | 74 hr = winrt_utils::CreateActivationFactory( |
| 103 RuntimeClass_Windows_Foundation_Uri, | 75 RuntimeClass_Windows_Foundation_Uri, |
| 104 uri_factory.GetAddressOf()); | 76 uri_factory.GetAddressOf()); |
| 105 CheckHR(hr, "Failed to create URIFactory"); | 77 CheckHR(hr, "Failed to create URIFactory"); |
| 106 | 78 |
| 107 mswrw::HString logo_url; | 79 mswrw::HString logo_url; |
| 108 logo_url.Attach(MakeHString(GetLogoUrlString())); | 80 logo_url.Attach(MakeHString(string16(L"file:///").append(logo_path.value()))); |
| 109 mswr::ComPtr<winfoundtn::IUriRuntimeClass> uri; | 81 mswr::ComPtr<winfoundtn::IUriRuntimeClass> uri; |
| 110 hr = uri_factory->CreateUri(logo_url.Get(), &uri); | 82 hr = uri_factory->CreateUri(logo_url.Get(), &uri); |
| 111 CheckHR(hr, "Failed to create URI"); | 83 CheckHR(hr, "Failed to create URI"); |
| 112 | 84 |
| 113 mswr::ComPtr<winui::StartScreen::ISecondaryTile> tile; | 85 mswr::ComPtr<winui::StartScreen::ISecondaryTile> tile; |
| 114 hr = tile_factory->CreateTile(id.Get(), | 86 hr = tile_factory->CreateTile(id.Get(), |
| 115 title.Get(), | 87 title.Get(), |
| 116 title.Get(), | 88 title.Get(), |
| 117 args.Get(), | 89 args.Get(), |
| 118 options, | 90 options, |
| 119 uri.Get(), | 91 uri.Get(), |
| 120 tile.GetAddressOf()); | 92 tile.GetAddressOf()); |
| 121 CheckHR(hr, "Failed to create tile"); | 93 CheckHR(hr, "Failed to create tile"); |
| 122 | 94 |
| 123 hr = tile->put_ForegroundText(winui::StartScreen::ForegroundText_Light); | 95 hr = tile->put_ForegroundText(winui::StartScreen::ForegroundText_Light); |
| 124 CheckHR(hr, "Failed to change foreground text color"); | 96 CheckHR(hr, "Failed to change foreground text color"); |
| 125 | 97 |
| 126 mswr::ComPtr<winfoundtn::IAsyncOperation<bool>> completion; | 98 mswr::ComPtr<winfoundtn::IAsyncOperation<bool>> completion; |
| 127 hr = tile->RequestCreateAsync(completion.GetAddressOf()); | 99 hr = tile->RequestCreateAsync(completion.GetAddressOf()); |
| 128 CheckHR(hr, "RequestCreateAsync failed"); | 100 CheckHR(hr, "RequestCreateAsync failed"); |
| 129 | 101 |
| 130 typedef winfoundtn::IAsyncOperationCompletedHandler<bool> RequestDoneType; | 102 typedef winfoundtn::IAsyncOperationCompletedHandler<bool> RequestDoneType; |
| 131 mswr::ComPtr<RequestDoneType> handler(mswr::Callback<RequestDoneType>( | 103 mswr::ComPtr<RequestDoneType> handler(mswr::Callback<RequestDoneType>( |
| 132 globals.view, &ChromeAppView::TileRequestCreateDone)); | 104 globals.view, &ChromeAppView::TileRequestCreateDone)); |
| 133 DCHECK(handler.Get() != NULL); | 105 DCHECK(handler.Get() != NULL); |
| 134 hr = completion->put_Completed(handler.Get()); | 106 hr = completion->put_Completed(handler.Get()); |
| 135 CheckHR(hr, "Failed to put_Completed"); | 107 CheckHR(hr, "Failed to put_Completed"); |
| 136 } | 108 } |
| 137 | 109 |
| 138 void TogglePinnedToStartScreen(const string16& title_str, | |
| 139 const string16& url_str) { | |
| 140 if (IsPinnedToStartScreen(url_str)) { | |
| 141 DeleteTileFromStartScreen(url_str); | |
| 142 return; | |
| 143 } | |
| 144 | |
| 145 CreateTileOnStartScreen(title_str, url_str); | |
| 146 } | |
| 147 | |
| 148 } // namespace | 110 } // namespace |
| 149 | 111 |
| 150 BOOL MetroIsPinnedToStartScreen(const string16& url) { | 112 BOOL MetroIsPinnedToStartScreen(const string16& tile_id) { |
| 151 VLOG(1) << __FUNCTION__ << " url: " << url; | 113 mswr::ComPtr<winui::StartScreen::ISecondaryTileStatics> tile_statics; |
| 152 return IsPinnedToStartScreen(url); | 114 HRESULT hr = winrt_utils::CreateActivationFactory( |
| 115 RuntimeClass_Windows_UI_StartScreen_SecondaryTile, |
| 116 tile_statics.GetAddressOf()); |
| 117 CheckHR(hr, "Failed to create instance of ISecondaryTileStatics"); |
| 118 |
| 119 boolean exists; |
| 120 hr = tile_statics->Exists(MakeHString(tile_id), &exists); |
| 121 CheckHR(hr, "ISecondaryTileStatics.Exists failed"); |
| 122 return exists; |
| 153 } | 123 } |
| 154 | 124 |
| 155 void MetroTogglePinnedToStartScreen(const string16& title, | 125 void MetroUnPinFromStartScreen(const string16& tile_id) { |
| 156 const string16& url) { | |
| 157 DVLOG(1) << __FUNCTION__ << " title:" << title << " url: " << url; | |
| 158 globals.appview_msg_loop->PostTask( | 126 globals.appview_msg_loop->PostTask( |
| 159 FROM_HERE, base::Bind(&TogglePinnedToStartScreen, title, url)); | 127 FROM_HERE, base::Bind(&DeleteTileFromStartScreen, tile_id)); |
| 160 } | 128 } |
| 129 |
| 130 void MetroPinToStartScreen(const string16& tile_id, |
| 131 const string16& title, |
| 132 const string16& url, |
| 133 const FilePath& logo_path) { |
| 134 globals.appview_msg_loop->PostTask( |
| 135 FROM_HERE, base::Bind(&CreateTileOnStartScreen, |
| 136 tile_id, |
| 137 title, |
| 138 url, |
| 139 logo_path)); |
| 140 } |
| OLD | NEW |