File: profile_window.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (289 lines) | stat: -rw-r--r-- 10,939 bytes parent folder | download | duplicates (5)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright 2013 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/profiles/profile_window.h"

#include <stddef.h>

#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/location.h"
#include "base/metrics/user_metrics.h"
#include "base/strings/escape.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "base/trace_event/trace_event.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/about_flags.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/lifetime/application_lifetime.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_attributes_entry.h"
#include "chrome/browser/profiles/profile_attributes_storage.h"
#include "chrome/browser/profiles/profile_avatar_icon_util.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/signin/account_reconcilor_factory.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/signin/signin_ui_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_dialogs.h"
#include "chrome/browser/ui/startup/startup_types.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "components/prefs/pref_service.h"
#include "components/signin/public/base/signin_pref_names.h"
#include "components/signin/public/identity_manager/account_info.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/webui/flags/pref_service_flags_storage.h"
#include "content/public/browser/browser_thread.h"
#include "extensions/buildflags/buildflags.h"
#include "third_party/abseil-cpp/absl/cleanup/cleanup.h"

#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registrar.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_registry_factory.h"
#include "extensions/browser/extension_system.h"
#endif  // BUILDFLAG(ENABLE_EXTENSIONS)

#if !BUILDFLAG(IS_ANDROID)
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_list_observer.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/startup/startup_browser_creator.h"
#endif  // !defined (OS_ANDROID)

#if !BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/ui/profiles/profile_picker.h"
#endif  // !BUILDFLAG(IS_CHROMEOS)

using base::UserMetricsAction;
using content::BrowserThread;

namespace {

#if BUILDFLAG(ENABLE_EXTENSIONS)
void UnblockExtensions(Profile* profile) {
  extensions::ExtensionRegistrar::Get(profile)->UnblockAllExtensions();
}
#endif  // BUILDFLAG(ENABLE_EXTENSIONS)

// Helper function to run a callback on a profile once it's initialized.
void ProfileLoadedCallback(base::OnceCallback<void(Profile*)> callback,
                           Profile* profile) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  if (!profile) {
    return;
  }
  if (callback) {
    std::move(callback).Run(profile);
  }
}

}  // namespace

namespace profiles {

void FindOrCreateNewWindowForProfile(
    Profile* profile,
    chrome::startup::IsProcessStartup process_startup,
    chrome::startup::IsFirstRun is_first_run,
    bool always_create) {
  DCHECK(profile);
  TRACE_EVENT1("browser", "FindOrCreateNewWindowForProfile", "profile_path",
               profile->GetPath());

  if (!always_create) {
    Browser* browser = chrome::FindTabbedBrowser(profile, false);
    if (browser) {
      browser->window()->Activate();
      return;
    }
  }

  base::RecordAction(UserMetricsAction("NewWindow"));
  base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
  StartupBrowserCreator browser_creator;
  // This is not a browser launch from the user; don't record the launch mode.
  browser_creator.LaunchBrowser(command_line, profile, base::FilePath(),
                                process_startup, is_first_run,
                                /*restore_tabbed_browser=*/true);
}

void OpenBrowserWindowForProfile(base::OnceCallback<void(Browser*)> callback,
                                 bool always_create,
                                 bool is_new_profile,
                                 bool unblock_extensions,
                                 Profile* profile) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  TRACE_EVENT1("browser", "OpenBrowserWindowForProfile", "profile_path",
               profile->GetPath().AsUTF8Unsafe());
  // `error_closure_runner` runs the callback  with nullptr to signal an error
  // if the function reaches a return statement without consuming callback. If
  // the callback is consumed by std::move(), then `callback` will be empty
  // after that and the scoped cleanup does nothing.
  absl::Cleanup error_closure_runner([&callback] {
    if (callback) {
      std::move(callback).Run(nullptr);
    }
  });
  chrome::startup::IsProcessStartup process_startup =
      chrome::startup::IsProcessStartup::kNo;
  chrome::startup::IsFirstRun is_first_run = chrome::startup::IsFirstRun::kNo;

  // If this is a brand new profile, then start a first run window.
  if (is_new_profile) {
    process_startup = chrome::startup::IsProcessStartup::kYes;
    is_first_run = chrome::startup::IsFirstRun::kYes;
  }

#if !BUILDFLAG(IS_CHROMEOS)
  if (!profile->IsGuestSession()) {
    ProfileAttributesEntry* entry =
        g_browser_process->profile_manager()
            ->GetProfileAttributesStorage()
            .GetProfileAttributesWithPath(profile->GetPath());
    if (entry && entry->IsSigninRequired()) {
      ProfilePicker::Show(ProfilePicker::Params::FromEntryPoint(
          ProfilePicker::EntryPoint::kProfileLocked));
      return;
    }
  }
#endif  // !BUILDFLAG(IS_CHROMEOS)

#if BUILDFLAG(ENABLE_EXTENSIONS)
  if (unblock_extensions) {
    UnblockExtensions(profile);
  }
#endif  // BUILDFLAG(ENABLE_EXTENSIONS)

  // If |always_create| is false, and we have a |callback| to run, check
  // whether a browser already exists so that we can run the callback. We don't
  // want to rely on the observer listening to OnBrowserSetLastActive in this
  // case, as you could manually activate an incorrect browser and trigger
  // a false positive.
  if (!always_create) {
    Browser* browser = chrome::FindTabbedBrowser(profile, false);
    if (browser) {
      browser->window()->Activate();
      if (callback) {
        std::move(callback).Run(browser);
      }
      return;
    }
  }

  // If there is a callback, create an observer to make sure it is only
  // run when the browser has been completely created. This observer will
  // delete itself once that happens. This should not leak, because we are
  // passing |always_create| = true to FindOrCreateNewWindow below, which ends
  // up calling LaunchBrowser and opens a new window. If for whatever reason
  // that fails, either something has crashed, or the observer will be cleaned
  // up when a different browser for this profile is opened.
  if (callback) {
    new BrowserAddedForProfileObserver(profile, std::move(callback));
  }

  // We already dealt with the case when |always_create| was false and a browser
  // existed, which means that here a browser definitely needs to be created.
  // Passing true for |always_create| means we won't duplicate the code that
  // tries to find a browser.
  profiles::FindOrCreateNewWindowForProfile(profile, process_startup,
                                            is_first_run, true);
}

#if !BUILDFLAG(IS_ANDROID)

void LoadProfileAsync(const base::FilePath& path,
                      base::OnceCallback<void(Profile*)> callback) {
  g_browser_process->profile_manager()->CreateProfileAsync(
      path, base::BindOnce(&ProfileLoadedCallback, std::move(callback)));
}

void SwitchToProfile(const base::FilePath& path,
                     bool always_create,
                     base::OnceCallback<void(Browser*)> callback) {
  base::OnceCallback<void(Profile*)> open_browser_callback =
      base::BindOnce(&profiles::OpenBrowserWindowForProfile,
                     std::move(callback), always_create,
                     /*is_new_profile=*/false,
                     /*unblock_extensions=*/false);
  g_browser_process->profile_manager()->CreateProfileAsync(
      path,
      base::BindOnce(&ProfileLoadedCallback, std::move(open_browser_callback)));
}

void SwitchToGuestProfile(base::OnceCallback<void(Browser*)> callback) {
  SwitchToProfile(ProfileManager::GetGuestProfilePath(),
                  /*always_create=*/false, std::move(callback));
}
#endif

bool HasProfileSwitchTargets(Profile* profile) {
  size_t min_profiles = profile->IsGuestSession() ? 1 : 2;
  size_t number_of_profiles =
      g_browser_process->profile_manager()->GetNumberOfProfiles();
  return number_of_profiles >= min_profiles;
}

void CloseProfileWindows(Profile* profile) {
  DCHECK(profile);
  BrowserList::CloseAllBrowsersWithProfile(profile,
                                           BrowserList::CloseCallback(),
                                           BrowserList::CloseCallback(), false);
}

BrowserAddedForProfileObserver::BrowserAddedForProfileObserver(
    Profile* profile,
    base::OnceCallback<void(Browser*)> callback)
    : profile_(profile->GetWeakPtr()), callback_(std::move(callback)) {
  DCHECK(callback_);
  browser_list_observation_.Observe(BrowserList::GetInstance());
}

BrowserAddedForProfileObserver::~BrowserAddedForProfileObserver() = default;

void BrowserAddedForProfileObserver::OnBrowserAdded(Browser* browser) {
  if (browser_) {
    // Do not run the callback twice.
    return;
  }

  if (browser->profile() != profile_.get()) {
    // The profile has been deleted, or this is a different profile.
    return;
  }

  browser_ = browser;
  // By the time the browser is added a tab (or multiple) are about to be added.
  // Post the callback to the message loop so it gets executed after the tabs
  // are created.
  base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE,
      base::BindOnce(&BrowserAddedForProfileObserver::NotifyBrowserCreatedAnDie,
                     base::Unretained(this)));
}

void BrowserAddedForProfileObserver::OnBrowserRemoved(Browser* browser) {
  // The browser was closed before the callback could run.
  if (browser == browser_) {
    browser_list_observation_.Reset();
    browser_ = nullptr;
  }
}

void BrowserAddedForProfileObserver::NotifyBrowserCreatedAnDie() {
  // If the browser exists, the profile has to exist too.
  CHECK(profile_ || !browser_);
  std::move(callback_).Run(browser_);
  delete this;
}

}  // namespace profiles