Extract locking behaviour from ProcessSingleton.

This refactoring continues the division of the behaviour of ProcessSingleton into two parts:
* The protocol for establishing a server process and communicating between the client and server.
* How the server processes command-line invocations.

Very small behavioural change:
* If an error occurs while parsing the command-line received via COPY_DATA, the modal dialog (if any) is no longer flashed and raised to foreground.

The motivation for this change is that I wish to introduce some more sophisticated behaviour when queuing messages during startup. See the follow-up CL (in-progress) at https://codereview.chromium.org/12674028/ .

BUG=170726,170734,225693

Review URL: https://chromiumcodereview.appspot.com/12096114

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@195264 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/chrome/browser/process_singleton_modal_dialog_lock.h b/chrome/browser/process_singleton_modal_dialog_lock.h
new file mode 100644
index 0000000..d287865d
--- /dev/null
+++ b/chrome/browser/process_singleton_modal_dialog_lock.h
@@ -0,0 +1,64 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_PROCESS_SINGLETON_MODAL_DIALOG_LOCK_H_
+#define CHROME_BROWSER_PROCESS_SINGLETON_MODAL_DIALOG_LOCK_H_
+
+#include "base/basictypes.h"
+#include "base/callback_forward.h"
+#include "chrome/browser/process_singleton.h"
+#include "ui/gfx/native_widget_types.h"
+
+class CommandLine;
+
+namespace base {
+class FilePath;
+}
+
+// Provides a ProcessSingleton::NotificationCallback that prevents
+// command-line handling when a modal dialog is active during startup. The
+// client must ensure that SetActiveModalDialog is called appropriately when
+// such dialogs are displayed or dismissed.
+//
+// While a dialog is active, the ProcessSingleton notification
+// callback will handle but ignore notifications:
+// 1. Neither this process nor the invoking process will handle the command
+//    line.
+// 2. The active dialog is brought to the foreground and/or the taskbar icon
+//    flashed (using ::SetForegroundWindow on Windows).
+//
+// Otherwise, the notification is forwarded to a wrapped NotificationCallback.
+class ProcessSingletonModalDialogLock {
+ public:
+  typedef base::Callback<void(gfx::NativeWindow)> SetForegroundWindowHandler;
+  explicit ProcessSingletonModalDialogLock(
+      const ProcessSingleton::NotificationCallback& original_callback);
+
+  ProcessSingletonModalDialogLock(
+      const ProcessSingleton::NotificationCallback& original_callback,
+      const SetForegroundWindowHandler& set_foreground_window_handler);
+
+  ~ProcessSingletonModalDialogLock();
+
+  // Receives a handle to the active modal dialog, or NULL if the active dialog
+  // is dismissed.
+  void SetActiveModalDialog(gfx::NativeWindow active_dialog);
+
+  // Returns the ProcessSingleton::NotificationCallback.
+  // The callback is only valid during the lifetime of the
+  // ProcessSingletonModalDialogLock instance.
+  ProcessSingleton::NotificationCallback AsNotificationCallback();
+
+ private:
+  bool NotificationCallbackImpl(const CommandLine& command_line,
+                                const base::FilePath& current_directory);
+
+  gfx::NativeWindow active_dialog_;
+  ProcessSingleton::NotificationCallback original_callback_;
+  SetForegroundWindowHandler set_foreground_window_handler_;
+
+  DISALLOW_COPY_AND_ASSIGN(ProcessSingletonModalDialogLock);
+};
+
+#endif  // CHROME_BROWSER_PROCESS_SINGLETON_MODAL_DIALOG_LOCK_H_