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
|
// Copyright 2022 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/webui/intro/intro_handler.h"
#include "base/cancelable_callback.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_functions.h"
#include "base/scoped_observation.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/time.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/policy/chrome_browser_policy_connector.h"
#include "chrome/browser/policy/profile_policy_connector.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_avatar_icon_util.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/ui/managed_ui.h"
#include "chrome/browser/ui/webui/intro/intro_ui.h"
#include "chrome/grit/branded_strings.h"
#include "chrome/grit/generated_resources.h"
#include "components/policy/core/common/cloud/cloud_policy_store.h"
#include "components/policy/core/common/cloud/machine_level_user_cloud_policy_manager.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/signin/public/identity_manager/signin_constants.h"
#include "content/public/browser/web_ui.h"
#include "content/public/browser/web_ui_data_source.h"
#include "google_apis/gaia/core_account_id.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
using signin::constants::kNoHostedDomainFound;
namespace {
policy::CloudPolicyStore* GetCloudPolicyStore() {
auto* machine_level_manager = g_browser_process->browser_policy_connector()
->machine_level_user_cloud_policy_manager();
return machine_level_manager ? machine_level_manager->core()->store()
: nullptr;
}
// PolicyStoreState will make it easier to handle all the states in a single
// callback.
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
enum class PolicyStoreState {
// Store was already loaded when we attached the observer.
kSuccessAlreadyLoaded = 0,
// Store has been loaded before the time delay ends.
kSuccess = 1,
// Store did not load in time.
kTimeout = 2,
// OnStoreError called.
kStoreError = 3,
// Store is null for a managed device.
kStoreNull = 4,
kMaxValue = kStoreNull,
};
void RecordDisclaimerMetrics(PolicyStoreState state,
base::TimeTicks start_time) {
base::UmaHistogramEnumeration("ProfilePicker.FirstRun.PolicyStoreState",
state);
if (state == PolicyStoreState::kSuccess) {
base::UmaHistogramTimes(
"ProfilePicker.FirstRun.OrganizationAvailableTiming",
/*sample*/ base::TimeTicks::Now() - start_time);
}
}
class PolicyStoreObserver : public policy::CloudPolicyStore::Observer {
public:
explicit PolicyStoreObserver(
base::OnceCallback<void(std::string)> handle_policy_store_change)
: handle_policy_store_change_(std::move(handle_policy_store_change)) {
DCHECK(handle_policy_store_change_);
start_time_ = base::TimeTicks::Now();
// Update the disclaimer directly if the policy store is already loaded.
auto* policy_store = GetCloudPolicyStore();
// GetCloudPolicyStore will return nullptr for managed devices with
// non-branded builds because the machine level cloud policy manager will be
// null while the device is still managed. In that case, we show a generic
// disclaimer.
if (!policy_store) {
// The device is not enrolled in Chrome Browser Cloud Management
HandlePolicyStoreStatusChange(PolicyStoreState::kStoreNull);
return;
}
if (policy_store->is_initialized()) {
HandlePolicyStoreStatusChange(PolicyStoreState::kSuccessAlreadyLoaded);
return;
}
policy_store_observation_.Observe(policy_store);
// 2.5 is the chrome logo animation time which is 1.5s plus the maximum
// delay of 1s that we are willing to wait for.
constexpr auto kMaximumEnterpriseDisclaimerDelay = base::Seconds(2.5);
on_organization_fetch_timeout_.Reset(
base::BindOnce(&PolicyStoreObserver::OnOrganizationFetchTimeout,
base::Unretained(this)));
base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE, on_organization_fetch_timeout_.callback(),
kMaximumEnterpriseDisclaimerDelay);
}
~PolicyStoreObserver() override = default;
// policy::CloudPolicyStore::Observer:
void OnStoreLoaded(policy::CloudPolicyStore* store) override {
on_organization_fetch_timeout_.Cancel();
policy_store_observation_.Reset();
HandlePolicyStoreStatusChange(PolicyStoreState::kSuccess);
}
void OnStoreError(policy::CloudPolicyStore* store) override {
on_organization_fetch_timeout_.Cancel();
policy_store_observation_.Reset();
HandlePolicyStoreStatusChange(PolicyStoreState::kStoreError);
}
// Called when the delay specified for the store to load has passed. We show a
// generic disclaimer when this happens.
void OnOrganizationFetchTimeout() {
policy_store_observation_.Reset();
HandlePolicyStoreStatusChange(PolicyStoreState::kTimeout);
}
void HandlePolicyStoreStatusChange(PolicyStoreState state) {
RecordDisclaimerMetrics(state, start_time_);
std::string managed_device_disclaimer;
if (state == PolicyStoreState::kSuccess ||
state == PolicyStoreState::kSuccessAlreadyLoaded) {
std::optional<std::string> manager = GetDeviceManagerIdentity();
managed_device_disclaimer =
manager->empty()
? l10n_util::GetStringUTF8(IDS_FRE_MANAGED_DESCRIPTION)
: l10n_util::GetStringFUTF8(IDS_FRE_MANAGED_BY_DESCRIPTION,
base::UTF8ToUTF16(*manager));
} else {
managed_device_disclaimer =
l10n_util::GetStringUTF8(IDS_FRE_MANAGED_DESCRIPTION);
}
std::move(handle_policy_store_change_).Run(managed_device_disclaimer);
}
private:
base::ScopedObservation<policy::CloudPolicyStore,
policy::CloudPolicyStore::Observer>
policy_store_observation_{this};
base::OnceCallback<void(std::string)> handle_policy_store_change_;
base::CancelableOnceCallback<void()> on_organization_fetch_timeout_;
base::TimeTicks start_time_;
};
} // namespace
IntroHandler::IntroHandler(
base::RepeatingCallback<void(IntroChoice)> intro_callback,
base::OnceCallback<void(DefaultBrowserChoice)> default_browser_callback,
bool is_device_managed,
std::string_view source_name)
: intro_callback_(std::move(intro_callback)),
default_browser_callback_(std::move(default_browser_callback)),
is_device_managed_(is_device_managed),
source_name_(source_name) {
DCHECK(intro_callback_);
DCHECK(default_browser_callback_);
}
IntroHandler::~IntroHandler() = default;
void IntroHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback(
"continueWithoutAccount",
base::BindRepeating(&IntroHandler::HandleContinueWithoutAccount,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"continueWithAccount",
base::BindRepeating(&IntroHandler::HandleContinueWithAccount,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"initializeMainView",
base::BindRepeating(&IntroHandler::HandleInitializeMainView,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"setAsDefaultBrowser",
base::BindRepeating(&IntroHandler::HandleSetAsDefaultBrowser,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"skipDefaultBrowser",
base::BindRepeating(&IntroHandler::HandleSkipDefaultBrowser,
base::Unretained(this)));
}
void IntroHandler::OnJavascriptAllowed() {
if (!is_device_managed_) {
return;
}
policy_store_observer_ = std::make_unique<PolicyStoreObserver>(base::BindOnce(
&IntroHandler::FireManagedDisclaimerUpdate, base::Unretained(this)));
}
void IntroHandler::HandleContinueWithAccount(const base::Value::List& args) {
CHECK(args.empty());
intro_callback_.Run(IntroChoice::kContinueWithAccount);
}
void IntroHandler::HandleContinueWithoutAccount(const base::Value::List& args) {
CHECK(args.empty());
intro_callback_.Run(IntroChoice::kContinueWithoutAccount);
}
void IntroHandler::ResetIntroButtons() {
if (IsJavascriptAllowed()) {
FireWebUIListener("reset-intro-buttons");
}
}
void IntroHandler::HandleInitializeMainView(const base::Value::List& args) {
CHECK(args.empty());
AllowJavascript();
}
void IntroHandler::HandleSetAsDefaultBrowser(const base::Value::List& args) {
CHECK(args.empty());
if (default_browser_callback_) {
std::move(default_browser_callback_)
.Run(DefaultBrowserChoice::kClickSetAsDefault);
}
}
void IntroHandler::HandleSkipDefaultBrowser(const base::Value::List& args) {
CHECK(args.empty());
if (default_browser_callback_) {
std::move(default_browser_callback_).Run(DefaultBrowserChoice::kSkip);
}
}
void IntroHandler::ResetDefaultBrowserButtons() {
if (IsJavascriptAllowed()) {
FireWebUIListener("reset-default-browser-buttons");
}
}
void IntroHandler::SetCanPinToTaskbar(bool can_pin) {
if (can_pin) {
base::Value::Dict update;
update.Set(
"defaultBrowserTitle",
l10n_util::GetStringUTF16(IDS_FRE_DEFAULT_BROWSER_AND_PINNING_TITLE));
update.Set("defaultBrowserSubtitle",
l10n_util::GetStringUTF16(
IDS_FRE_DEFAULT_BROWSER_AND_PINNING_SUBTITLE));
content::WebUIDataSource::Update(Profile::FromWebUI(web_ui()), source_name_,
std::move(update));
}
}
void IntroHandler::FireManagedDisclaimerUpdate(std::string disclaimer) {
DCHECK(is_device_managed_);
if (IsJavascriptAllowed()) {
FireWebUIListener("managed-device-disclaimer-updated",
base::Value(std::move(disclaimer)));
}
}
|