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#ifndef CHROME_BROWSER_CHROME_PROCESS_SINGLETON_H_
6#define CHROME_BROWSER_CHROME_PROCESS_SINGLETON_H_
7
8#include "base/basictypes.h"
9#include "base/files/file_path.h"
10#include "chrome/browser/process_singleton.h"
11#include "chrome/browser/process_singleton_modal_dialog_lock.h"
12#include "chrome/browser/process_singleton_startup_lock.h"
13#include "ui/gfx/native_widget_types.h"
14
15// Composes a basic ProcessSingleton with ProcessSingletonStartupLock and
16// ProcessSingletonModalDialogLock.
17//
18// Notifications from ProcessSingleton will be discarded if a modal dialog is
19// active. Otherwise, until |Unlock()| is called, they will be queued up.
20// Once unlocked, notifications will be passed to the client-supplied
21// NotificationCallback.
22//
23// The client must ensure that SetActiveModalDialog is called appropriately when
24// dialogs are displayed or dismissed during startup. While a dialog is active:
25// 1. Neither this process nor the invoking process will handle the command
26//    line.
27// 2. The active dialog is brought to the foreground and/or the taskbar icon
28//    flashed (using ::SetForegroundWindow on Windows).
29class ChromeProcessSingleton {
30 public:
31  ChromeProcessSingleton(
32      const base::FilePath& user_data_dir,
33      const ProcessSingleton::NotificationCallback& notification_callback);
34
35  ChromeProcessSingleton(
36      const base::FilePath& user_data_dir,
37      const ProcessSingleton::NotificationCallback& notification_callback,
38      const ProcessSingletonModalDialogLock::SetForegroundWindowHandler&
39          set_foreground_window_handler);
40
41  ~ChromeProcessSingleton();
42
43  // Notify another process, if available. Otherwise sets ourselves as the
44  // singleton instance. Returns PROCESS_NONE if we became the singleton
45  // instance. Callers are guaranteed to either have notified an existing
46  // process or have grabbed the singleton (unless the profile is locked by an
47  // unreachable process).
48  ProcessSingleton::NotifyResult NotifyOtherProcessOrCreate();
49
50  // Clear any lock state during shutdown.
51  void Cleanup();
52
53  // Receives a handle to the active modal dialog, or NULL if the active dialog
54  // is dismissed.
55  void SetActiveModalDialog(gfx::NativeWindow active_dialog);
56
57  // Executes previously queued command-line invocations and allows future
58  // invocations to be executed immediately.
59  // This only has an effect the first time it is called.
60  void Unlock();
61
62 private:
63  // We compose these two locks with the client-supplied notification callback.
64  // First |modal_dialog_lock_| will discard any notifications that arrive while
65  // a modal dialog is active. Otherwise, it will pass the notification to
66  // |startup_lock_|, which will queue notifications until |Unlock()| is called.
67  // Notifications passing through both locks are finally delivered to our
68  // client.
69  ProcessSingletonStartupLock startup_lock_;
70  ProcessSingletonModalDialogLock modal_dialog_lock_;
71
72  // The basic ProcessSingleton
73  ProcessSingleton process_singleton_;
74
75  DISALLOW_COPY_AND_ASSIGN(ChromeProcessSingleton);
76};
77
78#endif  // CHROME_BROWSER_CHROME_PROCESS_SINGLETON_H_
79