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_H_
6#define WIN8_METRO_DRIVER_CHROME_APP_VIEW_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 <list>
14#include <map>
15#include <string>
16#include <utility>
17
18#include "base/memory/scoped_ptr.h"
19#include "base/message_loop/message_loop.h"
20#include "base/synchronization/lock.h"
21#include "win8/metro_driver/chrome_url_launch_handler.h"
22#include "win8/metro_driver/devices_handler.h"
23#include "win8/metro_driver/metro_dialog_box.h"
24#include "win8/metro_driver/settings_handler.h"
25#include "win8/metro_driver/toast_notification_handler.h"
26
27namespace IPC {
28  class Listener;
29  class ChannelProxy;
30}
31
32class ChromeAppView
33    : public mswr::RuntimeClass<winapp::Core::IFrameworkView> {
34 public:
35  ChromeAppView();
36  ~ChromeAppView();
37
38  // IViewProvider overrides.
39  IFACEMETHOD(Initialize)(winapp::Core::ICoreApplicationView* view);
40  IFACEMETHOD(SetWindow)(winui::Core::ICoreWindow* window);
41  IFACEMETHOD(Load)(HSTRING entryPoint);
42  IFACEMETHOD(Run)();
43  IFACEMETHOD(Uninitialize)();
44
45  static LRESULT CALLBACK CoreWindowProc(HWND window, UINT message, WPARAM wp,
46                                         LPARAM lp);
47
48  bool osk_visible_notification_received() const {
49    return osk_visible_notification_received_;
50  }
51
52  // Displays the notification.
53  void DisplayNotification(
54      const ToastNotificationHandler::DesktopNotification& notification);
55
56  // Cancels the notification.
57  void CancelNotification(const std::string& notification);
58
59  // Returns true if the notification passed in is valid.
60  bool IsValidNotification(const std::string& notification);
61
62  // Displays a dialog box.
63  void ShowDialogBox(const MetroDialogBox::DialogBoxInfo& dialog_box_info);
64  // Dismisses the dialog box.
65  void DismissDialogBox();
66
67  // Helper function to unsnap the chrome metro app if it is snapped.
68  // Returns S_OK on success.
69  static HRESULT Unsnap();
70
71  // Notification from chrome that a full screen operation is being performed.
72  void SetFullscreen(bool fullscreen);
73
74  // Returns the current view state of the chrome window.
75  winui::ViewManagement::ApplicationViewState GetViewState();
76
77  HWND core_window_hwnd() { return core_window_hwnd_; }
78
79 private:
80  HRESULT OnActivate(winapp::Core::ICoreApplicationView* view,
81                     winapp::Activation::IActivatedEventArgs* args);
82
83  HRESULT OnSizeChanged(winui::Core::ICoreWindow* sender,
84                        winui::Core::IWindowSizeChangedEventArgs* args);
85
86  HRESULT OnEdgeGestureCompleted(winui::Input::IEdgeGesture* gesture,
87                                 winui::Input::IEdgeGestureEventArgs* args);
88
89  HRESULT OnShareDataRequested(
90      winapp::DataTransfer::IDataTransferManager* data_transfer_mgr,
91      winapp::DataTransfer::IDataRequestedEventArgs* event_args);
92
93  HRESULT OnInputPaneVisible(
94      winui::ViewManagement::IInputPane* input_pane,
95      winui::ViewManagement::IInputPaneVisibilityEventArgs* event_args);
96
97  HRESULT OnInputPaneHiding(
98    winui::ViewManagement::IInputPane* input_pane,
99    winui::ViewManagement::IInputPaneVisibilityEventArgs* event_args);
100
101  HRESULT OnPositionChanged(int x, int y);
102
103  void CheckForOSKActivation();
104
105  HRESULT RegisterInputPaneNotifications();
106
107  void HandleInputPaneVisible(const RECT& osk_rect);
108  void HandleInputPaneHidden(const RECT& osk_rect);
109
110  mswr::ComPtr<winui::Core::ICoreWindow> window_;
111  mswr::ComPtr<winapp::Core::ICoreApplicationView> view_;
112  EventRegistrationToken activated_token_;
113  EventRegistrationToken edgeevent_token_;
114  EventRegistrationToken sizechange_token_;
115  EventRegistrationToken share_data_requested_token_;
116  EventRegistrationToken input_pane_visible_token_;
117  EventRegistrationToken input_pane_hiding_token_;
118  EventRegistrationToken app_exit_token_;
119
120  // The actual window behind the view surface.
121  HWND core_window_hwnd_;
122
123  ChromeUrlLaunchHandler url_launch_handler_;
124  metro_driver::DevicesHandler devices_handler_;
125  SettingsHandler settings_handler_;
126  mswr::ComPtr<winui::ViewManagement::IInputPane> input_pane_;
127  mswr::ComPtr<winui::ViewManagement::IApplicationViewStatics> app_view_;
128
129  bool osk_visible_notification_received_;
130
131  // map of notification id to the ToastNotificationHandler instance.
132  typedef std::map<std::string, scoped_ptr<ToastNotificationHandler> >
133      NotificationMap;
134  NotificationMap notification_map_;
135
136  // Synchronizes access to the notification_map_ member.
137  base::Lock notification_lock_;
138
139  // If the OSK covers the input area we scroll the window by the height of the
140  // OSK + an additional offset. This member holds this offset. Set to 0 if the
141  // window was not scrolled.
142  int osk_offset_adjustment_;
143
144  MetroDialogBox dialog_box_;
145};
146
147// Global information used across the metro driver.
148struct Globals {
149  LPTHREAD_START_ROUTINE host_main;
150  void* host_context;
151  // The pair below contains the HWND and a bool which indicates whether the
152  // window was displaced to ensure that the focused region is visible when
153  // the OSK is displayed.
154  std::list<std::pair<HWND, bool> > host_windows;
155  HANDLE host_thread;
156  ChromeAppView* view;
157  WNDPROC g_core_proc;
158  base::string16 navigation_url;
159  base::string16 search_string;
160  winapp::Activation::ApplicationExecutionState previous_state;
161  winapp::Activation::ActivationKind initial_activation_kind;
162  bool is_initial_activation;
163  // This message loop lives in the app view's thread. Some operations have
164  // to be initiated from that thread, notably spawning file pickers.
165  base::MessageLoopProxy* appview_msg_loop;
166  winapp::Core::ICoreApplicationExit* app_exit;
167  base::string16 metro_command_line_switches;
168};
169
170extern Globals globals;
171
172#endif  // WIN8_METRO_DRIVER_CHROME_APP_VIEW_H_
173