OLD | NEW |
(Empty) | |
| 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 #include "chrome/browser/ui/views/base_shell_dialog_win.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/threading/thread.h" |
| 10 |
| 11 // Helpers to show certain types of Windows shell dialogs in a way that doesn't |
| 12 // block the UI of the entire app. |
| 13 |
| 14 class ShellDialogThread : public base::Thread { |
| 15 public: |
| 16 ShellDialogThread() : base::Thread("Chrome_ShellDialogThread") { } |
| 17 ~ShellDialogThread(); |
| 18 |
| 19 protected: |
| 20 void Init(); |
| 21 |
| 22 void CleanUp(); |
| 23 |
| 24 private: |
| 25 DISALLOW_COPY_AND_ASSIGN(ShellDialogThread); |
| 26 }; |
| 27 |
| 28 ShellDialogThread::~ShellDialogThread() { |
| 29 Stop(); |
| 30 } |
| 31 |
| 32 void ShellDialogThread::Init() { |
| 33 // Initializes the COM library on the current thread. |
| 34 CoInitialize(NULL); |
| 35 } |
| 36 |
| 37 void ShellDialogThread::CleanUp() { |
| 38 // Closes the COM library on the current thread. CoInitialize must |
| 39 // be balanced by a corresponding call to CoUninitialize. |
| 40 CoUninitialize(); |
| 41 } |
| 42 |
| 43 // static |
| 44 BaseShellDialogImpl::Owners BaseShellDialogImpl::owners_; |
| 45 int BaseShellDialogImpl::instance_count_ = 0; |
| 46 |
| 47 BaseShellDialogImpl::BaseShellDialogImpl() { |
| 48 ++instance_count_; |
| 49 } |
| 50 |
| 51 BaseShellDialogImpl::~BaseShellDialogImpl() { |
| 52 // All runs should be complete by the time this is called! |
| 53 if (--instance_count_ == 0) |
| 54 DCHECK(owners_.empty()); |
| 55 } |
| 56 |
| 57 BaseShellDialogImpl::RunState BaseShellDialogImpl::BeginRun(HWND owner) { |
| 58 // Cannot run a modal shell dialog if one is already running for this owner. |
| 59 DCHECK(!IsRunningDialogForOwner(owner)); |
| 60 // The owner must be a top level window, otherwise we could end up with two |
| 61 // entries in our map for the same top level window. |
| 62 DCHECK(!owner || owner == GetAncestor(owner, GA_ROOT)); |
| 63 RunState run_state; |
| 64 run_state.dialog_thread = CreateDialogThread(); |
| 65 run_state.owner = owner; |
| 66 if (owner) { |
| 67 owners_.insert(owner); |
| 68 DisableOwner(owner); |
| 69 } |
| 70 return run_state; |
| 71 } |
| 72 |
| 73 void BaseShellDialogImpl::EndRun(RunState run_state) { |
| 74 if (run_state.owner) { |
| 75 DCHECK(IsRunningDialogForOwner(run_state.owner)); |
| 76 EnableOwner(run_state.owner); |
| 77 DCHECK(owners_.find(run_state.owner) != owners_.end()); |
| 78 owners_.erase(run_state.owner); |
| 79 } |
| 80 DCHECK(run_state.dialog_thread); |
| 81 delete run_state.dialog_thread; |
| 82 } |
| 83 |
| 84 bool BaseShellDialogImpl::IsRunningDialogForOwner(HWND owner) const { |
| 85 return (owner && owners_.find(owner) != owners_.end()); |
| 86 } |
| 87 |
| 88 void BaseShellDialogImpl::DisableOwner(HWND owner) { |
| 89 if (IsWindow(owner)) |
| 90 EnableWindow(owner, FALSE); |
| 91 } |
| 92 |
| 93 // static |
| 94 base::Thread* BaseShellDialogImpl::CreateDialogThread() { |
| 95 base::Thread* thread = new ShellDialogThread; |
| 96 bool started = thread->Start(); |
| 97 DCHECK(started); |
| 98 return thread; |
| 99 } |
| 100 |
| 101 void BaseShellDialogImpl::EnableOwner(HWND owner) { |
| 102 if (IsWindow(owner)) |
| 103 EnableWindow(owner, TRUE); |
| 104 } |
OLD | NEW |