Home | History | Annotate | Download | only in browser
      1 // Copyright (c) 2013 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/process_singleton_modal_dialog_lock.h"
      6 
      7 #if defined(OS_WIN)
      8 #include <Windows.h>
      9 #endif
     10 
     11 #include "base/bind.h"
     12 #include "base/command_line.h"
     13 #include "base/files/file_path.h"
     14 #include "base/logging.h"
     15 
     16 namespace {
     17 
     18 #if !defined(OS_WIN) || defined(USE_AURA)
     19 void DoSetForegroundWindow(gfx::NativeWindow /* target_window */) {
     20   NOTREACHED();
     21 }
     22 #else
     23 void DoSetForegroundWindow(HWND target_window) {
     24   ::SetForegroundWindow(target_window);
     25 }
     26 #endif
     27 
     28 }  // namespace
     29 
     30 ProcessSingletonModalDialogLock::ProcessSingletonModalDialogLock(
     31     const ProcessSingleton::NotificationCallback& original_callback)
     32     : active_dialog_(NULL),
     33       original_callback_(original_callback),
     34       set_foreground_window_handler_(base::Bind(&DoSetForegroundWindow)) {}
     35 
     36 ProcessSingletonModalDialogLock::ProcessSingletonModalDialogLock(
     37     const ProcessSingleton::NotificationCallback& original_callback,
     38     const SetForegroundWindowHandler& set_foreground_window_handler)
     39     : active_dialog_(NULL),
     40       original_callback_(original_callback),
     41       set_foreground_window_handler_(set_foreground_window_handler) {}
     42 
     43 ProcessSingletonModalDialogLock::~ProcessSingletonModalDialogLock() {}
     44 
     45 void ProcessSingletonModalDialogLock::SetActiveModalDialog(
     46     gfx::NativeWindow active_dialog) {
     47   active_dialog_ = active_dialog;
     48 }
     49 
     50 ProcessSingleton::NotificationCallback
     51 ProcessSingletonModalDialogLock::AsNotificationCallback() {
     52   return base::Bind(&ProcessSingletonModalDialogLock::NotificationCallbackImpl,
     53                     base::Unretained(this));
     54 }
     55 
     56 bool ProcessSingletonModalDialogLock::NotificationCallbackImpl(
     57       const CommandLine& command_line,
     58       const base::FilePath& current_directory) {
     59   if (active_dialog_ != NULL) {
     60     set_foreground_window_handler_.Run(active_dialog_);
     61     return true;
     62   } else {
     63     return original_callback_.Run(command_line, current_directory);
     64   }
     65 }
     66