OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 #ifndef CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_TOAST_HELPER_WIN_H_ |
| 6 #define CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_TOAST_HELPER_WIN_H_ |
| 7 |
| 8 #include <activation.h> |
| 9 #include <d2d1_1.h> |
| 10 #include <d3d11_1.h> |
| 11 #include <roapi.h> |
| 12 #include <shlwapi.h> |
| 13 #include <wincodec.h> |
| 14 #include <windows.h> |
| 15 #include <windows.applicationModel.core.h> |
| 16 #include <windows.applicationModel.datatransfer.h> |
| 17 #include <windows.data.xml.dom.h> |
| 18 #include <windows.graphics.printing.h> |
| 19 #include <windows.storage.pickers.h> |
| 20 #include <windows.ui.notifications.h> |
| 21 #include <wrl/implements.h> |
| 22 #include <wrl/module.h> |
| 23 #include <wrl/event.h> |
| 24 #include <wrl/wrappers/corewrappers.h> |
| 25 |
| 26 #include "base/macros.h" |
| 27 #include "base/strings/string16.h" |
| 28 |
| 29 namespace mswr = Microsoft::WRL; |
| 30 namespace mswrw = Microsoft::WRL::Wrappers; |
| 31 |
| 32 namespace winapp = ABI::Windows::ApplicationModel; |
| 33 namespace windata = ABI::Windows::Data; |
| 34 namespace winxml = ABI::Windows::Data::Xml; |
| 35 namespace windevs = ABI::Windows::Devices; |
| 36 namespace winfoundtn = ABI::Windows::Foundation; |
| 37 namespace wingfx = ABI::Windows::Graphics; |
| 38 namespace winui = ABI::Windows::UI; |
| 39 namespace winsys = ABI::Windows::System; |
| 40 namespace winstorage = ABI::Windows::Storage; |
| 41 |
| 42 namespace base { |
| 43 class FilePath; |
| 44 } // namespace base |
| 45 |
| 46 typedef winfoundtn::ITypedEventHandler< |
| 47 winui::Notifications::ToastNotification*, IInspectable*> |
| 48 ToastActivatedHandler; |
| 49 |
| 50 typedef winfoundtn::ITypedEventHandler< |
| 51 winui::Notifications::ToastNotification*, |
| 52 winui::Notifications::ToastDismissedEventArgs*> ToastDismissedHandler; |
| 53 |
| 54 typedef winfoundtn::ITypedEventHandler< |
| 55 winui::Notifications::ToastNotification*, |
| 56 winui::Notifications::ToastFailedEventArgs*> ToastFailedHandler; |
| 57 |
| 58 // This class provides an abstraction to COM calls to display and process |
| 59 // Notification Toast in Windows. Features: |
| 60 // - Stores HRESULT |hr_|, and coarse accessors HasFailed() or StillOkay(). |
| 61 // - Failure is permanent, and on failure all subsequent calls become no-op. |
| 62 // - An implicit "current element" is stored, which is the target of various |
| 63 // XML operations. |
| 64 class NotificationToastHelperWin { |
| 65 public: |
| 66 NotificationToastHelperWin() = default; |
| 67 ~NotificationToastHelperWin() = default; |
| 68 |
| 69 bool StillOkay() const { return SUCCEEDED(hr_); } |
| 70 bool HasFailed() const { return !SUCCEEDED(hr_); } |
| 71 |
| 72 // Creates |toast_manager_|. |
| 73 void CreateToastManager(); |
| 74 |
| 75 // Loads |toast_xml_| from template, also loads |document_element_|. |
| 76 void LoadXMLFromTemplate(winui::Notifications::ToastTemplateType type); |
| 77 |
| 78 // For debugging. |
| 79 // TODO(huangs): Remove. |
| 80 // Loads |toast_xml_| directly from |xml_str|. |
| 81 void LoadXMLFromString(const base::string16& xml_str); |
| 82 |
| 83 // For debugging. |
| 84 // TODO(huangs): Remove. |
| 85 void AlertToastXml(); |
| 86 |
| 87 // Selects |document_element_| as the "current element". |
| 88 void SelectDocument(); |
| 89 |
| 90 // Selects element with |tag_name| and at |index| as the "current element". |
| 91 void SelectElementByTagNameAndIndex(const base::string16& tag_name, |
| 92 unsigned int index); |
| 93 |
| 94 // Converts a file path, e.g., "C:\\User\\test\\AppData\\Local\\Temp\\a.tmp" |
| 95 // to a file URL, e.g., "file:///C:/User/test/AppData/Local/Temp/a.tmp". |
| 96 base::string16 FilePathToFileUrl(const base::FilePath& file_path); |
| 97 |
| 98 // Removes the current element, and selects the parent. |
| 99 void RemoveElement(); |
| 100 |
| 101 // Appends a text node to the "current element". |
| 102 void AppendText(const base::string16& text); |
| 103 |
| 104 // Sets an attribute of the "current element". |
| 105 void SetAttribute(const base::string16& name, const base::string16& value); |
| 106 |
| 107 // Reads an attribute of the "current element". |
| 108 base::string16 GetAttribute(const base::string16& name); |
| 109 |
| 110 // Creates a toast notifier. |
| 111 void CreateToastNotifier(); |
| 112 |
| 113 // Creates a toast notification. |
| 114 void CreateToastNotification(); |
| 115 |
| 116 // Loads an existing |toast_notification|. Also loads |toast_xml_| and |
| 117 // |document_element_|. |
| 118 void LoadNotificationAndXml( |
| 119 winui::Notifications::IToastNotification* toast_notification); |
| 120 |
| 121 // Displays a toast notification, using |toast_xml_|. |
| 122 void Show(mswr::ComPtr<ToastActivatedHandler> activated_handler, |
| 123 mswr::ComPtr<ToastDismissedHandler> dismissed_handler, |
| 124 mswr::ComPtr<ToastFailedHandler> failed_handler, |
| 125 EventRegistrationToken* activated_token, |
| 126 EventRegistrationToken* dismissed_token, |
| 127 EventRegistrationToken* failed_token); |
| 128 |
| 129 private: |
| 130 // Returned HSTRING should be deallocated by ::WindowsDeleteString(). This can |
| 131 // be done by attaching the return value to mswrw::HString. |
| 132 HSTRING MakeHString(const base::string16& str); |
| 133 |
| 134 // Creates an intermediate text node. |
| 135 void CreateTextNode(const base::string16& text, |
| 136 mswr::ComPtr<winxml::Dom::IXmlNode>* node); |
| 137 |
| 138 HRESULT hr_ = S_OK; |
| 139 mswr::ComPtr<winui::Notifications::IToastNotificationManagerStatics> |
| 140 toast_manager_; |
| 141 mswr::ComPtr<winxml::Dom::IXmlDocument> toast_xml_; |
| 142 mswr::ComPtr<winxml::Dom::IXmlElement> document_element_; |
| 143 mswr::ComPtr<winxml::Dom::IXmlElement> cur_element_; |
| 144 |
| 145 mswr::ComPtr<winui::Notifications::IToastNotifier> notifier_; |
| 146 mswr::ComPtr<winui::Notifications::IToastNotification> toast_notification_; |
| 147 |
| 148 DISALLOW_COPY_AND_ASSIGN(NotificationToastHelperWin); |
| 149 }; |
| 150 |
| 151 #endif // CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_TOAST_HELPER_WIN_H_ |
OLD | NEW |