blob: 939fb2365ec6405678d7181378c674ee7b4f9bcb [file] [log] [blame]
[email protected]9a47c432013-04-19 20:33:551// 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_startup_lock.h"
6
7#include "base/bind.h"
8#include "base/logging.h"
9
10ProcessSingletonStartupLock::ProcessSingletonStartupLock(
11 const ProcessSingleton::NotificationCallback& original_callback)
12 : locked_(true),
13 original_callback_(original_callback) {}
14
gab25894fe2017-05-30 03:40:3615ProcessSingletonStartupLock::~ProcessSingletonStartupLock() {
16 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
17}
[email protected]9a47c432013-04-19 20:33:5518
19ProcessSingleton::NotificationCallback
20ProcessSingletonStartupLock::AsNotificationCallback() {
21 return base::Bind(&ProcessSingletonStartupLock::NotificationCallbackImpl,
22 base::Unretained(this));
23}
24
25void ProcessSingletonStartupLock::Unlock() {
gab25894fe2017-05-30 03:40:3626 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
[email protected]9a47c432013-04-19 20:33:5527 locked_ = false;
28
29 // Replay the command lines of the messages which were received while the
30 // ProcessSingleton was locked. Only replay each message once.
31 std::set<DelayedStartupMessage> replayed_messages;
32 for (std::vector<DelayedStartupMessage>::const_iterator it =
33 saved_startup_messages_.begin();
34 it != saved_startup_messages_.end(); ++it) {
35 if (replayed_messages.find(*it) != replayed_messages.end())
36 continue;
avi556c05022014-12-22 23:31:4337 original_callback_.Run(base::CommandLine(it->first), it->second);
[email protected]9a47c432013-04-19 20:33:5538 replayed_messages.insert(*it);
39 }
40 saved_startup_messages_.clear();
41}
42
43bool ProcessSingletonStartupLock::NotificationCallbackImpl(
avi556c05022014-12-22 23:31:4344 const base::CommandLine& command_line,
[email protected]9a47c432013-04-19 20:33:5545 const base::FilePath& current_directory) {
46 if (locked_) {
47 // If locked, it means we are not ready to process this message because
48 // we are probably in a first run critical phase.
49 saved_startup_messages_.push_back(
50 std::make_pair(command_line.argv(), current_directory));
51 return true;
52 } else {
53 return original_callback_.Run(command_line, current_directory);
54 }
55}