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 #ifndef WIN8_METRO_DRIVER_CHROME_APP_VIEW_ASH_H_ 6 #define WIN8_METRO_DRIVER_CHROME_APP_VIEW_ASH_H_ 7 8 #include <windows.applicationmodel.core.h> 9 #include <windows.ui.core.h> 10 #include <windows.ui.input.h> 11 #include <windows.ui.viewmanagement.h> 12 13 #include "base/memory/scoped_ptr.h" 14 #include "base/message_loop/message_loop.h" 15 #include "base/strings/string16.h" 16 #include "ui/events/event_constants.h" 17 #include "win8/metro_driver/direct3d_helper.h" 18 #include "win8/metro_driver/ime/ime_popup_observer.h" 19 #include "win8/metro_driver/ime/input_source_observer.h" 20 #include "win8/metro_driver/ime/text_service_delegate.h" 21 22 namespace base { 23 class FilePath; 24 } 25 26 namespace IPC { 27 class Listener; 28 class ChannelProxy; 29 } 30 31 namespace metro_driver { 32 class InputSource; 33 class TextService; 34 } 35 36 namespace metro_viewer { 37 struct CharacterBounds; 38 struct UnderlineInfo; 39 } 40 41 class OpenFilePickerSession; 42 class SaveFilePickerSession; 43 class FolderPickerSession; 44 class FilePickerSessionBase; 45 46 struct MetroViewerHostMsg_SaveAsDialogParams; 47 48 enum MetroTerminateMethod { 49 TERMINATE_USING_KEY_SEQUENCE = 1, 50 TERMINATE_USING_PROCESS_EXIT = 2, 51 }; 52 53 class ChromeAppViewAsh 54 : public mswr::RuntimeClass<winapp::Core::IFrameworkView>, 55 public metro_driver::ImePopupObserver, 56 public metro_driver::InputSourceObserver, 57 public metro_driver::TextServiceDelegate { 58 public: 59 ChromeAppViewAsh(); 60 ~ChromeAppViewAsh(); 61 62 // IViewProvider overrides. 63 IFACEMETHOD(Initialize)(winapp::Core::ICoreApplicationView* view); 64 IFACEMETHOD(SetWindow)(winui::Core::ICoreWindow* window); 65 IFACEMETHOD(Load)(HSTRING entryPoint); 66 IFACEMETHOD(Run)(); 67 IFACEMETHOD(Uninitialize)(); 68 69 // Helper function to unsnap the chrome metro app if it is snapped. 70 // Returns S_OK on success. 71 static HRESULT Unsnap(); 72 73 void OnActivateDesktop(const base::FilePath& file_path, bool ash_exit); 74 void OnOpenURLOnDesktop(const base::FilePath& shortcut, 75 const base::string16& url); 76 void OnSetCursor(HCURSOR cursor); 77 void OnDisplayFileOpenDialog(const base::string16& title, 78 const base::string16& filter, 79 const base::FilePath& default_path, 80 bool allow_multiple_files); 81 void OnDisplayFileSaveAsDialog( 82 const MetroViewerHostMsg_SaveAsDialogParams& params); 83 void OnDisplayFolderPicker(const base::string16& title); 84 void OnSetCursorPos(int x, int y); 85 86 // This function is invoked when the open file operation completes. The 87 // result of the operation is passed in along with the OpenFilePickerSession 88 // instance which is deleted after we read the required information from 89 // the OpenFilePickerSession class. 90 void OnOpenFileCompleted(OpenFilePickerSession* open_file_picker, 91 bool success); 92 93 // This function is invoked when the save file operation completes. The 94 // result of the operation is passed in along with the SaveFilePickerSession 95 // instance which is deleted after we read the required information from 96 // the SaveFilePickerSession class. 97 void OnSaveFileCompleted(SaveFilePickerSession* save_file_picker, 98 bool success); 99 100 // This function is invoked when the folder picker operation completes. The 101 // result of the operation is passed in along with the FolderPickerSession 102 // instance which is deleted after we read the required information from 103 // the FolderPickerSession class. 104 void OnFolderPickerCompleted(FolderPickerSession* folder_picker, 105 bool success); 106 107 void OnImeCancelComposition(); 108 void OnImeUpdateTextInputClient( 109 const std::vector<int32>& input_scopes, 110 const std::vector<metro_viewer::CharacterBounds>& character_bounds); 111 112 void OnMetroExit(MetroTerminateMethod method); 113 114 HWND core_window_hwnd() const { return core_window_hwnd_; } 115 116 117 private: 118 class PointerInfoHandler; 119 120 // ImePopupObserver overrides. 121 virtual void OnImePopupChanged(ImePopupObserver::EventType event) OVERRIDE; 122 123 // InputSourceObserver overrides. 124 virtual void OnInputSourceChanged() OVERRIDE; 125 126 // TextServiceDelegate overrides. 127 virtual void OnCompositionChanged( 128 const base::string16& text, 129 int32 selection_start, 130 int32 selection_end, 131 const std::vector<metro_viewer::UnderlineInfo>& underlines) OVERRIDE; 132 virtual void OnTextCommitted(const base::string16& text) OVERRIDE; 133 134 // Convenience for sending a MetroViewerHostMsg_MouseButton with the specified 135 // parameters. 136 void SendMouseButton(int x, 137 int y, 138 int extra, 139 ui::EventType event_type, 140 uint32 flags, 141 ui::EventFlags changed_button, 142 bool is_horizontal_wheel); 143 144 // Win8 only generates a mouse press for the initial button that goes down and 145 // a release when the last button is released. Any intermediary presses (or 146 // releases) do not result in a new press/release event. Instead a move is 147 // generated with the winui::Input::PointerUpdateKind identifying what 148 // changed. This function generates the necessary intermediary events (as 149 // necessary). 150 void GenerateMouseEventFromMoveIfNecessary(const PointerInfoHandler& pointer); 151 152 HRESULT OnActivate(winapp::Core::ICoreApplicationView* view, 153 winapp::Activation::IActivatedEventArgs* args); 154 155 HRESULT OnPointerMoved(winui::Core::ICoreWindow* sender, 156 winui::Core::IPointerEventArgs* args); 157 158 HRESULT OnPointerPressed(winui::Core::ICoreWindow* sender, 159 winui::Core::IPointerEventArgs* args); 160 161 HRESULT OnPointerReleased(winui::Core::ICoreWindow* sender, 162 winui::Core::IPointerEventArgs* args); 163 164 HRESULT OnWheel(winui::Core::ICoreWindow* sender, 165 winui::Core::IPointerEventArgs* args); 166 167 HRESULT OnKeyDown(winui::Core::ICoreWindow* sender, 168 winui::Core::IKeyEventArgs* args); 169 170 HRESULT OnKeyUp(winui::Core::ICoreWindow* sender, 171 winui::Core::IKeyEventArgs* args); 172 173 // Invoked for system keys like Alt, etc. 174 HRESULT OnAcceleratorKeyDown(winui::Core::ICoreDispatcher* sender, 175 winui::Core::IAcceleratorKeyEventArgs* args); 176 177 HRESULT OnCharacterReceived(winui::Core::ICoreWindow* sender, 178 winui::Core::ICharacterReceivedEventArgs* args); 179 180 HRESULT OnWindowActivated(winui::Core::ICoreWindow* sender, 181 winui::Core::IWindowActivatedEventArgs* args); 182 183 // Helper to handle search requests received via the search charm in ASH. 184 HRESULT HandleSearchRequest(winapp::Activation::IActivatedEventArgs* args); 185 // Helper to handle http/https url requests in ASH. 186 HRESULT HandleProtocolRequest(winapp::Activation::IActivatedEventArgs* args); 187 188 HRESULT OnEdgeGestureCompleted(winui::Input::IEdgeGesture* gesture, 189 winui::Input::IEdgeGestureEventArgs* args); 190 191 // Tasks posted to the UI thread to initiate the search/url navigation 192 // requests. 193 void OnSearchRequest(const base::string16& search_string); 194 void OnNavigateToUrl(const base::string16& url); 195 196 HRESULT OnSizeChanged(winui::Core::ICoreWindow* sender, 197 winui::Core::IWindowSizeChangedEventArgs* args); 198 199 mswr::ComPtr<winui::Core::ICoreWindow> window_; 200 mswr::ComPtr<winapp::Core::ICoreApplicationView> view_; 201 EventRegistrationToken activated_token_; 202 EventRegistrationToken pointermoved_token_; 203 EventRegistrationToken pointerpressed_token_; 204 EventRegistrationToken pointerreleased_token_; 205 EventRegistrationToken wheel_token_; 206 EventRegistrationToken keydown_token_; 207 EventRegistrationToken keyup_token_; 208 EventRegistrationToken character_received_token_; 209 EventRegistrationToken accel_keydown_token_; 210 EventRegistrationToken accel_keyup_token_; 211 EventRegistrationToken window_activated_token_; 212 EventRegistrationToken sizechange_token_; 213 EventRegistrationToken edgeevent_token_; 214 215 // Keep state about which button is currently down, if any, as PointerMoved 216 // events do not contain that state, but Ash's MouseEvents need it. Value is 217 // as a bitmask of ui::EventFlags. 218 uint32 mouse_down_flags_; 219 220 // Set the D3D swap chain and nothing else. 221 metro_driver::Direct3DHelper direct3d_helper_; 222 223 // The channel to Chrome, in particular to the MetroViewerProcessHost. 224 IPC::ChannelProxy* ui_channel_; 225 226 // The actual window behind the view surface. 227 HWND core_window_hwnd_; 228 229 // UI message loop to allow message passing into this thread. 230 base::MessageLoopForUI ui_loop_; 231 232 // For IME support. 233 scoped_ptr<metro_driver::InputSource> input_source_; 234 scoped_ptr<metro_driver::TextService> text_service_; 235 236 // The metro device scale factor as reported by the winrt interfaces. 237 float metro_dpi_scale_; 238 // The win32 dpi scale which is queried via GetDeviceCaps. Please refer to 239 // ui/gfx/win/dpi.cc for more information. 240 float win32_dpi_scale_; 241 }; 242 243 #endif // WIN8_METRO_DRIVER_CHROME_APP_VIEW_ASH_H_ 244