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.
5 #include "chrome/browser/ui/views/status_icons/status_tray_win.h"
10 #include "base/location.h"
11 #include "base/profiler/scoped_tracker.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/threading/non_thread_safe.h"
14 #include "base/threading/thread.h"
15 #include "base/win/wrapped_window_proc.h"
16 #include "chrome/browser/lifetime/application_lifetime.h"
17 #include "chrome/browser/ui/views/status_icons/status_icon_win.h"
18 #include "chrome/browser/ui/views/status_icons/status_tray_state_changer_win.h"
19 #include "chrome/common/chrome_constants.h"
20 #include "components/browser_watcher/exit_funnel_win.h"
21 #include "ui/gfx/screen.h"
22 #include "ui/gfx/win/hwnd_util.h"
24 static const UINT kStatusIconMessage
= WM_APP
+ 1;
27 // |kBaseIconId| is 2 to avoid conflicts with plugins that hard-code id 1.
28 const UINT kBaseIconId
= 2;
30 UINT
ReservedIconId(StatusTray::StatusIconType type
) {
31 return kBaseIconId
+ static_cast<UINT
>(type
);
34 // See http://crbug.com/412384.
35 void TraceSessionEnding(LPARAM lparam
) {
36 browser_watcher::ExitFunnel funnel
;
37 if (!funnel
.Init(chrome::kBrowserExitCodesRegistryPath
,
38 base::GetCurrentProcessHandle())) {
42 // This exit path is the prime suspect for most our unclean shutdowns.
43 // Trace all the possible options to WM_ENDSESSION. This may result in
44 // multiple events for a single shutdown, but that's fine.
45 funnel
.RecordEvent(L
"TraybarEndSession");
47 if (lparam
& ENDSESSION_CLOSEAPP
)
48 funnel
.RecordEvent(L
"ES_CloseApp");
49 if (lparam
& ENDSESSION_CRITICAL
)
50 funnel
.RecordEvent(L
"ES_Critical");
51 if (lparam
& ENDSESSION_LOGOFF
)
52 funnel
.RecordEvent(L
"ES_Logoff");
53 const LPARAM kKnownBits
=
54 ENDSESSION_CLOSEAPP
| ENDSESSION_CRITICAL
| ENDSESSION_LOGOFF
;
55 if (lparam
& ~kKnownBits
)
56 funnel
.RecordEvent(L
"ES_Other");
61 // Default implementation for StatusTrayStateChanger that communicates to
62 // Exporer.exe via COM. It spawns a background thread with a fresh COM
63 // apartment and requests that the visibility be increased unless the user
64 // has explicitly set the icon to be hidden.
65 class StatusTrayStateChangerProxyImpl
: public StatusTrayStateChangerProxy
,
66 public base::NonThreadSafe
{
68 StatusTrayStateChangerProxyImpl()
69 : pending_requests_(0),
70 worker_thread_("StatusIconCOMWorkerThread"),
72 worker_thread_
.init_com_with_mta(false);
75 void EnqueueChange(UINT icon_id
, HWND window
) override
{
76 DCHECK(CalledOnValidThread());
77 if (pending_requests_
== 0)
78 worker_thread_
.Start();
81 worker_thread_
.task_runner()->PostTaskAndReply(
84 &StatusTrayStateChangerProxyImpl::EnqueueChangeOnWorkerThread
,
86 base::Bind(&StatusTrayStateChangerProxyImpl::ChangeDone
,
87 weak_factory_
.GetWeakPtr()));
91 // Must be called only on |worker_thread_|, to ensure the correct COM
93 static void EnqueueChangeOnWorkerThread(UINT icon_id
, HWND window
) {
94 // It appears that IUnknowns are coincidentally compatible with
95 // scoped_refptr. Normally I wouldn't depend on that but it seems that
96 // base::win::IUnknownImpl itself depends on that coincidence so it's
97 // already being assumed elsewhere.
98 scoped_refptr
<StatusTrayStateChangerWin
> status_tray_state_changer(
99 new StatusTrayStateChangerWin(icon_id
, window
));
100 status_tray_state_changer
->EnsureTrayIconVisible();
103 // Called on UI thread.
105 DCHECK(CalledOnValidThread());
106 DCHECK_GT(pending_requests_
, 0);
108 if (--pending_requests_
== 0)
109 worker_thread_
.Stop();
113 int pending_requests_
;
114 base::Thread worker_thread_
;
115 base::WeakPtrFactory
<StatusTrayStateChangerProxyImpl
> weak_factory_
;
117 DISALLOW_COPY_AND_ASSIGN(StatusTrayStateChangerProxyImpl
);
120 StatusTrayWin::StatusTrayWin()
125 // Register our window class
126 WNDCLASSEX window_class
;
127 base::win::InitializeWindowClass(
128 chrome::kStatusTrayWindowClass
,
129 &base::win::WrappedWindowProc
<StatusTrayWin::WndProcStatic
>,
130 0, 0, 0, NULL
, NULL
, NULL
, NULL
, NULL
,
132 instance_
= window_class
.hInstance
;
133 atom_
= RegisterClassEx(&window_class
);
136 // If the taskbar is re-created after we start up, we have to rebuild all of
138 taskbar_created_message_
= RegisterWindowMessage(TEXT("TaskbarCreated"));
140 // Create an offscreen window for handling messages for the status icons. We
141 // create a hidden WS_POPUP window instead of an HWND_MESSAGE window, because
142 // only top-level windows such as popups can receive broadcast messages like
144 window_
= CreateWindow(MAKEINTATOM(atom_
),
145 0, WS_POPUP
, 0, 0, 0, 0, 0, 0, instance_
, 0);
146 gfx::CheckWindowCreated(window_
);
147 gfx::SetWindowUserData(window_
, this);
150 StatusTrayWin::~StatusTrayWin() {
152 DestroyWindow(window_
);
155 UnregisterClass(MAKEINTATOM(atom_
), instance_
);
158 void StatusTrayWin::UpdateIconVisibilityInBackground(
159 StatusIconWin
* status_icon
) {
160 if (!state_changer_proxy_
.get())
161 state_changer_proxy_
.reset(new StatusTrayStateChangerProxyImpl
);
163 state_changer_proxy_
->EnqueueChange(status_icon
->icon_id(),
164 status_icon
->window());
167 LRESULT CALLBACK
StatusTrayWin::WndProcStatic(HWND hwnd
,
171 StatusTrayWin
* msg_wnd
= reinterpret_cast<StatusTrayWin
*>(
172 GetWindowLongPtr(hwnd
, GWLP_USERDATA
));
174 return msg_wnd
->WndProc(hwnd
, message
, wparam
, lparam
);
176 return ::DefWindowProc(hwnd
, message
, wparam
, lparam
);
179 LRESULT CALLBACK
StatusTrayWin::WndProc(HWND hwnd
,
183 if (message
== taskbar_created_message_
) {
184 // We need to reset all of our icons because the taskbar went away.
185 for (StatusIcons::const_iterator
i(status_icons().begin());
186 i
!= status_icons().end(); ++i
) {
187 StatusIconWin
* win_icon
= static_cast<StatusIconWin
*>(*i
);
188 win_icon
->ResetIcon();
191 } else if (message
== kStatusIconMessage
) {
192 StatusIconWin
* win_icon
= NULL
;
194 // Find the selected status icon.
195 for (StatusIcons::const_iterator
i(status_icons().begin());
196 i
!= status_icons().end();
198 StatusIconWin
* current_win_icon
= static_cast<StatusIconWin
*>(*i
);
199 if (current_win_icon
->icon_id() == wparam
) {
200 win_icon
= current_win_icon
;
205 // It is possible for this procedure to be called with an obsolete icon
206 // id. In that case we should just return early before handling any
212 case TB_INDETERMINATE
:
213 win_icon
->HandleBalloonClickEvent();
219 // Walk our icons, find which one was clicked on, and invoke its
220 // HandleClickEvent() method.
221 gfx::Point
cursor_pos(
222 gfx::Screen::GetNativeScreen()->GetCursorScreenPoint());
223 win_icon
->HandleClickEvent(cursor_pos
, lparam
== WM_LBUTTONDOWN
);
226 } else if (message
== WM_ENDSESSION
) {
227 // If Chrome is in background-only mode, this is the only notification
228 // it gets that Windows is exiting. Make sure we shutdown in an orderly
230 TraceSessionEnding(lparam
);
231 chrome::SessionEnding();
233 return ::DefWindowProc(hwnd
, message
, wparam
, lparam
);
236 StatusIcon
* StatusTrayWin::CreatePlatformStatusIcon(
237 StatusTray::StatusIconType type
,
238 const gfx::ImageSkia
& image
,
239 const base::string16
& tool_tip
) {
241 if (type
== StatusTray::OTHER_ICON
)
242 next_icon_id
= NextIconId();
244 next_icon_id
= ReservedIconId(type
);
247 new StatusIconWin(this, next_icon_id
, window_
, kStatusIconMessage
);
249 icon
->SetImage(image
);
250 icon
->SetToolTip(tool_tip
);
254 UINT
StatusTrayWin::NextIconId() {
255 UINT icon_id
= next_icon_id_
++;
256 return kBaseIconId
+ static_cast<UINT
>(NAMED_STATUS_ICON_COUNT
) + icon_id
;
259 void StatusTrayWin::SetStatusTrayStateChangerProxyForTest(
260 scoped_ptr
<StatusTrayStateChangerProxy
> proxy
) {
261 state_changer_proxy_
= proxy
.Pass();
264 StatusTray
* StatusTray::Create() {
265 return new StatusTrayWin();