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