1 // Copyright 2019 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_UPDATER_WIN_UI_UI_H_
6 #define CHROME_UPDATER_WIN_UI_UI_H_
7 
8 #include <vector>
9 
10 #include "base/strings/string16.h"
11 #include "base/threading/thread_checker.h"
12 #include "base/win/atl.h"
13 #include "base/win/scoped_gdi_object.h"
14 #include "chrome/updater/win/ui/owner_draw_controls.h"
15 #include "chrome/updater/win/ui/resources/resources.grh"
16 
17 namespace updater {
18 namespace ui {
19 
20 void EnableFlatButtons(HWND hwnd_parent);
21 
22 void HideWindowChildren(HWND hwnd_parent);
23 
24 class OmahaWndEvents {
25  public:
26   virtual ~OmahaWndEvents() = default;
27   virtual void DoClose() = 0;
28   virtual void DoExit() = 0;
29 };
30 
31 // Implements the UI progress window.
32 class OmahaWnd : public CAxDialogImpl<OmahaWnd>,
33                  public OwnerDrawTitleBar,
34                  public CustomDlgColors,
35                  public WTL::CMessageFilter {
36   using Base = CAxDialogImpl<OmahaWnd>;
37 
38  public:
39   const int IDD;
40 
41   OmahaWnd(const OmahaWnd&) = delete;
42   OmahaWnd& operator=(const OmahaWnd&) = delete;
43   ~OmahaWnd() override;
44 
45   virtual HRESULT Initialize();
46 
47   // Overrides for CMessageFilter.
48   BOOL PreTranslateMessage(MSG* msg) override;
49 
SetEventSink(OmahaWndEvents * ev)50   void SetEventSink(OmahaWndEvents* ev) { events_sink_ = ev; }
51 
set_is_machine(bool is_machine)52   void set_is_machine(bool is_machine) { is_machine_ = is_machine; }
set_bundle_name(const base::string16 & bundle_name)53   void set_bundle_name(const base::string16& bundle_name) {
54     bundle_name_ = bundle_name;
55   }
56 
57   virtual void Show();
58 
59   BEGIN_MSG_MAP(OmahaWnd)
60     MESSAGE_HANDLER(WM_CLOSE, OnClose)
61     MESSAGE_HANDLER(WM_NCDESTROY, OnNCDestroy)
62     COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
63     CHAIN_MSG_MAP(Base)
64     CHAIN_MSG_MAP(OwnerDrawTitleBar)
65     CHAIN_MSG_MAP(CustomDlgColors)
66   END_MSG_MAP()
67 
68  protected:
69   struct ControlAttributes {
70     const bool is_ignore_entry = false;
71     const bool is_visible = false;
72     const bool is_enabled = false;
73     const bool is_button = false;
74     const bool is_default = false;
75   };
76 
77   OmahaWnd(int dialog_id, WTL::CMessageLoop* message_loop, HWND parent);
78 
79   // Message and command handlers.
80   LRESULT OnClose(UINT msg,
81                   WPARAM wparam,
82                   LPARAM lparam,
83                   BOOL& handled);  // NOLINT
84   LRESULT OnNCDestroy(UINT msg,
85                       WPARAM wparam,
86                       LPARAM lparam,
87                       BOOL& handled);  // NOLINT
88   LRESULT OnCancel(WORD notify_code,
89                    WORD id,
90                    HWND wnd_ctl,
91                    BOOL& handled);  // NOLINT
92 
93   // Returns true if the window is closed.
94   virtual bool MaybeCloseWindow() = 0;
95 
96   // Returns true to indicate that the client continues handling OnComplete.
97   bool OnComplete();
98 
99   HRESULT CloseWindow();
100   void InitializeDialog();
101 
102   HRESULT EnableClose(bool enable);
103   HRESULT EnableSystemCloseButton(bool enable);
104 
105   void SetControlAttributes(int control_id,
106                             const ControlAttributes& attributes);
107 
SetVisible(bool visible)108   void SetVisible(bool visible) {
109     ShowWindow(visible ? SW_SHOWNORMAL : SW_HIDE);
110   }
111 
message_loop()112   WTL::CMessageLoop* message_loop() { return message_loop_; }
is_complete()113   bool is_complete() { return is_complete_; }
is_close_enabled()114   bool is_close_enabled() { return is_close_enabled_; }
is_machine()115   bool is_machine() { return is_machine_; }
bundle_name()116   const base::string16& bundle_name() { return bundle_name_; }
117 
118   static const ControlAttributes kVisibleTextAttributes;
119   static const ControlAttributes kDefaultActiveButtonAttributes;
120   static const ControlAttributes kDisabledButtonAttributes;
121   static const ControlAttributes kNonDefaultActiveButtonAttributes;
122   static const ControlAttributes kVisibleImageAttributes;
123   static const ControlAttributes kDisabledNonButtonAttributes;
124 
125  private:
126   HRESULT SetWindowIcon();
127 
128   void MaybeRequestExitProcess();
129   void RequestExitProcess();
130 
131   THREAD_CHECKER(thread_checker_);
132 
133   WTL::CMessageLoop* message_loop_;
134   HWND parent_;
135 
136   bool is_complete_;
137   bool is_close_enabled_;
138 
139   OmahaWndEvents* events_sink_;
140 
141   bool is_machine_;
142   base::string16 bundle_name_;
143 
144   // Handle to large icon to show when ALT-TAB
145   base::win::ScopedGDIObject<HICON> hicon_;
146 
147   WTL::CFont default_font_;
148   WTL::CFont font_;
149   WTL::CFont error_font_;
150 
151   CustomProgressBarCtrl progress_bar_;
152 };
153 
154 // Registers the specified common control classes from the common control DLL.
155 // Calls are cumulative, meaning control_classes are added to existing classes.
156 // UIs that use common controls should call this function to ensure that the UI
157 // supports visual styles.
158 HRESULT InitializeCommonControls(DWORD control_classes);
159 
160 }  // namespace ui
161 }  // namespace updater
162 
163 #endif  // CHROME_UPDATER_WIN_UI_UI_H_
164