Index: chrome/browser/ui/views/status_icons/status_tray_win.cc |
diff --git a/chrome/browser/ui/views/status_icons/status_tray_win.cc b/chrome/browser/ui/views/status_icons/status_tray_win.cc |
index 8c66cd42489e3af3b0fb4480ac522047b9f7f9ce..1b25e4a098e8949f3d3173a4bb364c6d3cc09f54 100644 |
--- a/chrome/browser/ui/views/status_icons/status_tray_win.cc |
+++ b/chrome/browser/ui/views/status_icons/status_tray_win.cc |
@@ -6,8 +6,11 @@ |
#include <commctrl.h> |
+#include "base/bind.h" |
+#include "base/threading/thread.h" |
#include "base/win/wrapped_window_proc.h" |
#include "chrome/browser/ui/views/status_icons/status_icon_win.h" |
+#include "chrome/browser/ui/views/status_icons/status_tray_state_changer_win.h" |
#include "chrome/common/chrome_constants.h" |
#include "ui/gfx/screen.h" |
#include "ui/gfx/win/hwnd_util.h" |
@@ -24,6 +27,63 @@ UINT ReservedIconId(StatusTray::StatusIconType type) { |
} |
} // namespace |
+class StatusTrayStateChangerProxyImpl : public StatusTrayStateChangerProxy { |
+ public: |
+ StatusTrayStateChangerProxyImpl() |
+ : pending_requests_(0), |
+ worker_thread_("StatusIconCOMWorkerThread"), |
+ weak_factory_(this) { |
+ worker_thread_.init_com_with_mta(false); |
+ } |
+ |
+ virtual void EnqueueChange(UINT icon_id, HWND window) { |
+ if (pending_requests_ == 0) { |
+ worker_thread_.Start(); |
+ } |
+ |
+ ++pending_requests_; |
+ worker_thread_.message_loop_proxy()->PostTaskAndReply( |
+ FROM_HERE, |
+ base::Bind( |
+ &StatusTrayStateChangerProxyImpl::EnqueueChangeOnWorkerThread, |
+ icon_id, |
+ window), |
+ base::Bind(&StatusTrayStateChangerProxyImpl::ChangeDone, |
+ weak_factory_.GetWeakPtr())); |
+ } |
+ |
+ private: |
+ // Must be called only on |worker_thread_|, to ensure the correct COM |
+ // apartment. |
+ static void EnqueueChangeOnWorkerThread(UINT icon_id, HWND window) { |
+ // It appears that IUnknowns are coincidentally compatible with |
cpu_(ooo_6.6-7.5)
2014/04/04 18:04:16
please add a CHECK() to make sure you are in the c
dewittj
2014/04/04 21:05:51
How can you do that in a static method? I can't f
|
+ // scoped_refptr. Normally I wouldn't depend on that but it seems that |
+ // base::win::IUnknownImpl itself depends on that coincidence so it's |
+ // already being assumed elsewhere. |
+ scoped_refptr<StatusTrayStateChangerWin> status_tray_state_changer( |
+ new StatusTrayStateChangerWin(icon_id, window)); |
+ status_tray_state_changer->EnsureTrayIconVisible(); |
+ } |
+ |
+ // Called on UI thread. |
+ void ChangeDone() { |
+ DCHECK_GT(pending_requests_, 0); |
+ |
+ if (--pending_requests_ == 0) { |
+ worker_thread_.Stop(); |
cpu_(ooo_6.6-7.5)
2014/04/04 18:04:16
remind me, does Stop() kill the actual native thre
dewittj
2014/04/04 21:05:51
Stop() calls QuitWhenIdle on the worker thread, an
|
+ } |
+ } |
+ |
+ private: |
+ int pending_requests_; |
+ |
cpu_(ooo_6.6-7.5)
2014/04/04 18:04:16
remove the spaces 79, 81 unless this is a common s
dewittj
2014/04/04 21:05:51
Done.
|
+ base::Thread worker_thread_; |
+ |
+ base::WeakPtrFactory<StatusTrayStateChangerProxyImpl> weak_factory_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(StatusTrayStateChangerProxyImpl); |
+}; |
+ |
StatusTrayWin::StatusTrayWin() |
: next_icon_id_(1), |
atom_(0), |
@@ -54,6 +114,24 @@ StatusTrayWin::StatusTrayWin() |
gfx::SetWindowUserData(window_, this); |
} |
+StatusTrayWin::~StatusTrayWin() { |
+ if (window_) |
+ DestroyWindow(window_); |
+ |
+ if (atom_) |
+ UnregisterClass(MAKEINTATOM(atom_), instance_); |
+} |
+ |
+void StatusTrayWin::UpdateIconVisibilityInBackground( |
+ StatusIconWin* status_icon) { |
+ if (!state_changer_proxy_.get()) { |
+ state_changer_proxy_.reset(new StatusTrayStateChangerProxyImpl); |
+ } |
+ |
+ state_changer_proxy_->EnqueueChange(status_icon->icon_id(), |
+ status_icon->window()); |
+} |
+ |
LRESULT CALLBACK StatusTrayWin::WndProcStatic(HWND hwnd, |
UINT message, |
WPARAM wparam, |
@@ -117,14 +195,6 @@ LRESULT CALLBACK StatusTrayWin::WndProc(HWND hwnd, |
return ::DefWindowProc(hwnd, message, wparam, lparam); |
} |
-StatusTrayWin::~StatusTrayWin() { |
- if (window_) |
- DestroyWindow(window_); |
- |
- if (atom_) |
- UnregisterClass(MAKEINTATOM(atom_), instance_); |
-} |
- |
StatusIcon* StatusTrayWin::CreatePlatformStatusIcon( |
StatusTray::StatusIconType type, |
const gfx::ImageSkia& image, |
@@ -139,7 +209,7 @@ StatusIcon* StatusTrayWin::CreatePlatformStatusIcon( |
if (win8::IsSingleWindowMetroMode()) |
icon = new StatusIconMetro(next_icon_id); |
else |
- icon = new StatusIconWin(next_icon_id, window_, kStatusIconMessage); |
+ icon = new StatusIconWin(this, next_icon_id, window_, kStatusIconMessage); |
icon->SetImage(image); |
icon->SetToolTip(tool_tip); |
@@ -151,6 +221,11 @@ UINT StatusTrayWin::NextIconId() { |
return kBaseIconId + static_cast<UINT>(NAMED_STATUS_ICON_COUNT) + icon_id; |
} |
+void StatusTrayWin::SetStatusTrayStateChangerProxyForTest( |
+ scoped_ptr<StatusTrayStateChangerProxy> proxy) { |
+ state_changer_proxy_ = proxy.Pass(); |
+} |
+ |
StatusTray* StatusTray::Create() { |
return new StatusTrayWin(); |
} |