[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 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_startup_lock.h" |
| 6 | |
| 7 | #include "base/bind.h" |
| 8 | #include "base/logging.h" |
| 9 | |
| 10 | ProcessSingletonStartupLock::ProcessSingletonStartupLock( |
| 11 | const ProcessSingleton::NotificationCallback& original_callback) |
| 12 | : locked_(true), |
| 13 | original_callback_(original_callback) {} |
| 14 | |
gab | 25894fe | 2017-05-30 03:40:36 | [diff] [blame] | 15 | ProcessSingletonStartupLock::~ProcessSingletonStartupLock() { |
| 16 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 17 | } |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 18 | |
| 19 | ProcessSingleton::NotificationCallback |
| 20 | ProcessSingletonStartupLock::AsNotificationCallback() { |
| 21 | return base::Bind(&ProcessSingletonStartupLock::NotificationCallbackImpl, |
| 22 | base::Unretained(this)); |
| 23 | } |
| 24 | |
| 25 | void ProcessSingletonStartupLock::Unlock() { |
gab | 25894fe | 2017-05-30 03:40:36 | [diff] [blame] | 26 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 27 | 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; |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 37 | original_callback_.Run(base::CommandLine(it->first), it->second); |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 38 | replayed_messages.insert(*it); |
| 39 | } |
| 40 | saved_startup_messages_.clear(); |
| 41 | } |
| 42 | |
| 43 | bool ProcessSingletonStartupLock::NotificationCallbackImpl( |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 44 | const base::CommandLine& command_line, |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 45 | 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 | } |