File: profile_view_utils.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 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 (147 lines) | stat: -rw-r--r-- 5,704 bytes parent folder | download | duplicates (6)
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
// Copyright 2023 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/ui/profiles/profile_view_utils.h"

#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/prefs/incognito_mode_prefs.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_manager.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/sync/sync_ui_util.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_navigator.h"
#include "chrome/browser/ui/browser_navigator_params.h"
#include "chrome/browser/ui/layout_constants.h"
#include "chrome/browser/ui/toolbar/app_menu_model.h"
#include "chrome/common/url_constants.h"
#include "components/policy/core/common/policy_pref_names.h"
#include "components/signin/public/base/consent_level.h"
#include "components/signin/public/identity_manager/account_info.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/user_prefs/user_prefs.h"
#include "net/base/url_util.h"
#include "ui/base/accelerators/menu_label_accelerator_util.h"
#include "ui/base/window_open_disposition.h"
#include "ui/gfx/text_elider.h"
#include "url/gurl.h"

void NavigateToGoogleAccountPage(Profile* profile, const std::string& email) {
  // Create a URL so that the account chooser is shown if the account with
  // |email| is not signed into the web. Include a UTM parameter to signal the
  // source of the navigation.
  GURL google_account = net::AppendQueryParameter(
      GURL(chrome::kGoogleAccountURL), "utm_source", "chrome-profile-chooser");

  GURL url(chrome::kGoogleAccountChooserURL);
  url = net::AppendQueryParameter(url, "Email", email);
  url = net::AppendQueryParameter(url, "continue", google_account.spec());

  NavigateParams params(profile, url, ui::PAGE_TRANSITION_LINK);
  params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
  Navigate(&params);
}

bool IsSyncPaused(Profile* profile) {
  return GetAvatarSyncErrorType(profile) == AvatarSyncErrorType::kSyncPaused;
}

bool HasUnconstentedProfile(Profile* profile) {
  signin::IdentityManager* identity_manager =
      IdentityManagerFactory::GetForProfile(profile);
  return identity_manager ? !profile->IsGuestSession() &&
                                identity_manager->HasPrimaryAccount(
                                    signin::ConsentLevel::kSignin)
                          : false;
}

int CountBrowsersFor(Profile* profile) {
  int browser_count = chrome::GetBrowserCount(profile);
  if (!profile->IsOffTheRecord() && profile->HasPrimaryOTRProfile()) {
    browser_count += chrome::GetBrowserCount(
        profile->GetPrimaryOTRProfile(/*create_if_needed=*/true));
  }
  return browser_count;
}

AccountInfo GetAccountInfoFromProfile(const Profile* profile) {
  signin::IdentityManager* identity_manager =
      IdentityManagerFactory::GetForProfileIfExists(profile);
  // IdentityManager may be null if one is not mapped to the profile through the
  // KeyedServiceFactory. We do not create one if it doesn't already exist and
  // simply return an empty AccountInfo object.
  if (!identity_manager) {
    return AccountInfo();
  }
  CoreAccountInfo account =
      identity_manager->GetPrimaryAccountInfo(signin::ConsentLevel::kSignin);
  return identity_manager->FindExtendedAccountInfo(account);
}

ProfileAttributesEntry* GetProfileAttributesFromProfile(
    const Profile* profile) {
  return g_browser_process->profile_manager()
      ->GetProfileAttributesStorage()
      .GetProfileAttributesWithPath(profile->GetPath());
}

std::u16string GetProfileMenuDisplayName(
    ProfileAttributesEntry* profile_attributes) {
  std::u16string profile_name = profile_attributes->GetName();
  if (profile_name.empty()) {
    profile_name = profile_attributes->GetLocalProfileName();
  }
  profile_name = ui::EscapeMenuLabelAmpersands(gfx::TruncateString(
      profile_name, GetLayoutConstant(APP_MENU_MAXIMUM_CHARACTER_LENGTH),
      gfx::CHARACTER_BREAK));

  return profile_name;
}

std::vector<ProfileAttributesEntry*> GetAllOtherProfileEntriesForProfileSubMenu(
    const Profile* current_profile) {
  auto profile_entries =
      g_browser_process->profile_manager()
          ->GetProfileAttributesStorage()
          .GetAllProfilesAttributesSortedByLocalProfileNameWithCheck();

  std::vector<ProfileAttributesEntry*> result;

  for (ProfileAttributesEntry* profile_entry : profile_entries) {
    // The current profile and omitted profiles are excluded.
    if (profile_entry->GetPath() == current_profile->GetPath() ||
        profile_entry->IsOmitted()) {
      continue;
    }

    result.push_back(profile_entry);
  }

  return result;
}

bool IsOtherProfileCommand(int command_id) {
  return command_id >= AppMenuModel::kMinOtherProfileCommandId &&
         ((command_id - IDC_FIRST_UNBOUNDED_MENU) %
              AppMenuModel::kNumUnboundedMenuTypes ==
          (AppMenuModel::kMinOtherProfileCommandId - IDC_FIRST_UNBOUNDED_MENU));
}

bool IsOpenLinkOTREnabled(Profile* source_profie, const GURL& url) {
  if (source_profie->IsOffTheRecord() || !url.is_valid()) {
    return false;
  }

  if (!IsURLAllowedInIncognito(url)) {
    return false;
  }

  policy::IncognitoModeAvailability incognito_avail =
      IncognitoModePrefs::GetAvailability(
          user_prefs::UserPrefs::Get(source_profie));
  return incognito_avail != policy::IncognitoModeAvailability::kDisabled;
}