File: startup_launch_manager.cc

package info (click to toggle)
chromium 145.0.7632.159-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,976,224 kB
  • sloc: cpp: 36,198,469; ansic: 7,634,080; javascript: 3,564,060; python: 1,649,622; xml: 838,470; asm: 717,087; pascal: 185,708; sh: 88,786; perl: 88,718; objc: 79,984; sql: 59,811; cs: 42,452; fortran: 24,101; makefile: 21,144; tcl: 15,277; php: 14,022; yacc: 9,066; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,328; ada: 727; jsp: 228; sed: 36
file content (174 lines) | stat: -rw-r--r-- 5,869 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/startup/startup_launch_manager.h"

#include <optional>

#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/common/chrome_switches.h"

using auto_launch_util::StartupLaunchMode;

StartupLaunchManager::Client::Client(StartupLaunchReason launch_reason)
    : launch_reason_(launch_reason) {
  // Acquires a shared write lock to prevent StartupLaunchManager from
  // processing the final launch configuration until this client has
  // initialized.
  auto* launch_manager = StartupLaunchManager::From(g_browser_process);
  launch_manager->AcquireSharedWriteLock();
}

StartupLaunchManager::Client::~Client() {
  // If the client is destroyed before being fully initialized, release the lock
  // to prevent launch manager from hanging.
  auto* launch_manager = StartupLaunchManager::From(g_browser_process);
  if (launch_manager && !launch_enabled_.has_value()) {
    launch_manager->ReleaseSharedWriteLock();
  }
}

void StartupLaunchManager::Client::SetLaunchOnStartup(bool enable_launch) {
  // Do nothing if the state hasn't changed.
  if (launch_enabled_ == enable_launch) {
    return;
  }
  // If we are registering for the first time, release the startup launch
  // lock so that StartupLaunchManager can flush to the registry.
  const bool release_lock = !launch_enabled_.has_value();
  launch_enabled_.emplace(enable_launch);

  auto* launch_manager = StartupLaunchManager::From(g_browser_process);
  if (enable_launch) {
    launch_manager->RegisterLaunchOnStartup(launch_reason_);
  } else {
    launch_manager->UnregisterLaunchOnStartup(launch_reason_);
  }

  if (release_lock) {
    launch_manager->ReleaseSharedWriteLock();
  }
}

DEFINE_USER_DATA(StartupLaunchManager);

StartupLaunchManager::StartupLaunchManager(BrowserProcess* browser_process)
    : task_runner_(base::ThreadPool::CreateSequencedTaskRunner(
          {base::MayBlock(), base::TaskPriority::BEST_EFFORT,
           base::TaskShutdownBehavior::BLOCK_SHUTDOWN})),
      scoped_unowned_user_data_(browser_process->GetUnownedUserDataHost(),
                                *this) {
  // Acquire a lock so that any writes to registry are deferred until `Init()`
  // is called.
  AcquireSharedWriteLock();
}

StartupLaunchManager::~StartupLaunchManager() = default;

// static
StartupLaunchManager* StartupLaunchManager::From(
    BrowserProcess* browser_process) {
  return browser_process ? Get(browser_process->GetUnownedUserDataHost())
                         : nullptr;
}

void StartupLaunchManager::AcquireSharedWriteLock() {
  ++lock_counter_;
}

void StartupLaunchManager::ReleaseSharedWriteLock() {
  // No-op if locks were already forcefully released.
  if (lock_counter_ == 0) {
    return;
  }
  // After releasing the last lock, cancel the force release lock task and write
  // the final configuration to registry.
  if (--lock_counter_ == 0) {
    fallback_timer_.Stop();
    UpdateLaunchOnStartup(GetStartupLaunchMode());
  }
}

void StartupLaunchManager::CommitLaunchOnStartupState() {
  // Release the lock acquired in the constructor.
  ReleaseSharedWriteLock();

  // Set up a task to release all shared write locks, and writing the pending
  // changes to the registry if any client fails to do their initial
  // registration for one minute.
  fallback_timer_.Start(FROM_HERE, base::Minutes(1), this,
                        &StartupLaunchManager::ForceReleaseAllLocks);
}

// TODO(crbug.com/467376419): Record count of such occurrences.
void StartupLaunchManager::ForceReleaseAllLocks() {
  // No-op if the locks are already released.
  if (lock_counter_ == 0) {
    return;
  }
  lock_counter_ = 0;
  UpdateLaunchOnStartup(GetStartupLaunchMode());
}

std::optional<StartupLaunchMode> StartupLaunchManager::GetStartupLaunchMode()
    const {
  if (registered_launch_reasons_.empty()) {
    return std::nullopt;
  }
  return StartupLaunchMode::kBackground;
}

void StartupLaunchManager::RegisterLaunchOnStartup(StartupLaunchReason reason) {
  if (lock_counter_ > 0) {
    registered_launch_reasons_.Put(reason);
    return;
  }
  const auto previous_startup_mode = GetStartupLaunchMode();
  registered_launch_reasons_.Put(reason);
  const auto current_startup_mode = GetStartupLaunchMode();

  if (previous_startup_mode != current_startup_mode) {
    UpdateLaunchOnStartup(current_startup_mode);
  }
}

void StartupLaunchManager::UnregisterLaunchOnStartup(
    StartupLaunchReason reason) {
  if (lock_counter_ > 0) {
    registered_launch_reasons_.Remove(reason);
    return;
  }
  const auto previous_startup_mode = GetStartupLaunchMode();
  registered_launch_reasons_.Remove(reason);
  const auto current_startup_mode = GetStartupLaunchMode();

  if (!current_startup_mode.has_value() ||
      previous_startup_mode != current_startup_mode) {
    UpdateLaunchOnStartup(current_startup_mode);
  }
}

void StartupLaunchManager::UpdateLaunchOnStartup(
    std::optional<StartupLaunchMode> startup_launch_mode) {
#if BUILDFLAG(IS_WIN)
  // This functionality is only defined for default profile, currently.
  if (base::CommandLine::ForCurrentProcess()->HasSwitch(
          switches::kUserDataDir)) {
    return;
  }
  task_runner_->PostTask(
      FROM_HERE, startup_launch_mode.has_value()
                     ? base::BindOnce(auto_launch_util::EnableStartAtLogin,
                                      *startup_launch_mode)
                     : base::BindOnce(auto_launch_util::DisableStartAtLogin));
#endif  // BUILDFLAG(IS_WIN)
}