Home | History | Annotate | Download | only in shell_dialogs
      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 "ui/shell_dialogs/base_shell_dialog_win.h"
      6 
      7 #include <algorithm>
      8 
      9 #include "base/threading/thread.h"
     10 #include "base/win/scoped_com_initializer.h"
     11 
     12 namespace ui {
     13 
     14 // static
     15 BaseShellDialogImpl::Owners BaseShellDialogImpl::owners_;
     16 int BaseShellDialogImpl::instance_count_ = 0;
     17 
     18 BaseShellDialogImpl::BaseShellDialogImpl() {
     19   ++instance_count_;
     20 }
     21 
     22 BaseShellDialogImpl::~BaseShellDialogImpl() {
     23   // All runs should be complete by the time this is called!
     24   if (--instance_count_ == 0)
     25     DCHECK(owners_.empty());
     26 }
     27 
     28 BaseShellDialogImpl::RunState BaseShellDialogImpl::BeginRun(HWND owner) {
     29   // Cannot run a modal shell dialog if one is already running for this owner.
     30   DCHECK(!IsRunningDialogForOwner(owner));
     31   // The owner must be a top level window, otherwise we could end up with two
     32   // entries in our map for the same top level window.
     33   DCHECK(!owner || owner == GetAncestor(owner, GA_ROOT));
     34   RunState run_state;
     35   run_state.dialog_thread = CreateDialogThread();
     36   run_state.owner = owner;
     37   if (owner) {
     38     owners_.insert(owner);
     39     DisableOwner(owner);
     40   }
     41   return run_state;
     42 }
     43 
     44 void BaseShellDialogImpl::EndRun(RunState run_state) {
     45   if (run_state.owner) {
     46     DCHECK(IsRunningDialogForOwner(run_state.owner));
     47     EnableOwner(run_state.owner);
     48     DCHECK(owners_.find(run_state.owner) != owners_.end());
     49     owners_.erase(run_state.owner);
     50   }
     51   DCHECK(run_state.dialog_thread);
     52   delete run_state.dialog_thread;
     53 }
     54 
     55 bool BaseShellDialogImpl::IsRunningDialogForOwner(HWND owner) const {
     56   return (owner && owners_.find(owner) != owners_.end());
     57 }
     58 
     59 void BaseShellDialogImpl::DisableOwner(HWND owner) {
     60   if (IsWindow(owner))
     61     EnableWindow(owner, FALSE);
     62 }
     63 
     64 // static
     65 base::Thread* BaseShellDialogImpl::CreateDialogThread() {
     66   base::Thread* thread = new base::Thread("Chrome_ShellDialogThread");
     67   thread->init_com_with_mta(false);
     68   bool started = thread->Start();
     69   DCHECK(started);
     70   return thread;
     71 }
     72 
     73 void BaseShellDialogImpl::EnableOwner(HWND owner) {
     74   if (IsWindow(owner))
     75     EnableWindow(owner, TRUE);
     76 }
     77 
     78 }  // namespace ui
     79