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 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
|
// Copyright 2018 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/web_applications/web_app_database.h"
#include <set>
#include <string>
#include <utility>
#include <vector>
#include "base/check.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_functions.h"
#include "base/trace_event/trace_event.h"
#include "build/build_config.h"
#include "chrome/browser/web_applications/proto/web_app.pb.h"
#include "chrome/browser/web_applications/proto/web_app_launch_handler.pb.h"
#include "chrome/browser/web_applications/web_app.h"
#include "chrome/browser/web_applications/web_app_database_factory.h"
#include "chrome/browser/web_applications/web_app_database_serialization.h"
#include "chrome/browser/web_applications/web_app_helpers.h"
#include "chrome/browser/web_applications/web_app_proto_utils.h"
#include "chrome/browser/web_applications/web_app_registry_update.h"
#include "chrome/browser/web_applications/web_app_utils.h"
#include "chrome/common/chrome_features.h"
#include "components/sync/base/data_type.h"
#include "components/sync/model/data_type_store.h"
#include "components/sync/model/metadata_batch.h"
#include "components/sync/model/metadata_change_list.h"
#include "components/sync/model/model_error.h"
#include "components/sync/protocol/web_app_specifics.pb.h"
#include "components/webapps/browser/installable/installable_metrics.h"
#include "components/webapps/common/web_app_id.h"
#include "url/gurl.h"
namespace web_app {
WebAppDatabase::WebAppDatabase(AbstractWebAppDatabaseFactory* database_factory,
ReportErrorCallback error_callback)
: database_factory_(database_factory),
error_callback_(std::move(error_callback)) {
DCHECK(database_factory_);
}
WebAppDatabase::~WebAppDatabase() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}
void WebAppDatabase::OpenDatabase(RegistryOpenedCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(!store_);
syncer::OnceDataTypeStoreFactory store_factory =
database_factory_->GetStoreFactory();
std::move(store_factory)
.Run(syncer::WEB_APPS,
base::BindOnce(&WebAppDatabase::OnDatabaseOpened,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
void WebAppDatabase::Write(
const RegistryUpdateData& update_data,
std::unique_ptr<syncer::MetadataChangeList> metadata_change_list,
CompletionCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
CHECK(opened_);
std::unique_ptr<syncer::DataTypeStore::WriteBatch> write_batch =
store_->CreateWriteBatch();
// |update_data| can be empty here but we should write |metadata_change_list|
// anyway.
write_batch->TakeMetadataChangesFrom(std::move(metadata_change_list));
for (const std::unique_ptr<WebApp>& web_app : update_data.apps_to_create) {
auto proto = WebAppToProto(*web_app);
write_batch->WriteData(web_app->app_id(), proto->SerializeAsString());
}
for (const std::unique_ptr<WebApp>& web_app : update_data.apps_to_update) {
auto proto = WebAppToProto(*web_app);
write_batch->WriteData(web_app->app_id(), proto->SerializeAsString());
}
for (const webapps::AppId& app_id : update_data.apps_to_delete) {
write_batch->DeleteData(app_id);
}
store_->CommitWriteBatch(
std::move(write_batch),
base::BindOnce(&WebAppDatabase::OnDataWritten,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
// static
int WebAppDatabase::GetCurrentDatabaseVersion() {
return 3;
}
WebAppDatabase::ProtobufState::ProtobufState() = default;
WebAppDatabase::ProtobufState::~ProtobufState() = default;
WebAppDatabase::ProtobufState::ProtobufState(ProtobufState&&) = default;
WebAppDatabase::ProtobufState& WebAppDatabase::ProtobufState::operator=(
ProtobufState&&) = default;
WebAppDatabase::ProtobufState WebAppDatabase::ParseProtobufs(
const syncer::DataTypeStore::RecordList& data_records) const {
ProtobufState state;
for (const syncer::DataTypeStore::Record& record : data_records) {
if (record.id == kDatabaseMetadataKey) {
bool success = state.metadata.ParseFromString(record.value);
if (!success) {
DLOG(ERROR)
<< "WebApps LevelDB parse error: can't parse metadata proto.";
// TODO: Consider logging a histogram
}
continue;
}
proto::WebApp app_proto;
bool success = app_proto.ParseFromString(record.value);
if (!success) {
DLOG(ERROR) << "WebApps LevelDB parse error: can't parse app proto.";
// TODO: Consider logging a histogram
}
state.apps.emplace(record.id, std::move(app_proto));
}
return state;
}
void WebAppDatabase::MigrateDatabase(ProtobufState& state) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// Migration should happen when we have gotten a `store_`, but haven't
// finished opening the database yet.
CHECK(store_);
CHECK(!opened_);
bool did_change_metadata = false;
std::set<webapps::AppId> changed_apps;
// Upgrade from version 0 to version 1. This migrates the kSync source to
// a combination of kSync and kUserInstalled.
if (state.metadata.version() < 1 && GetCurrentDatabaseVersion() >= 1) {
MigrateInstallSourceAddUserInstalled(state, changed_apps);
base::UmaHistogramSparse("WebApp.Database.VersionUpgradedTo", 1);
state.metadata.set_version(1);
did_change_metadata = true;
}
// Upgrade from version 1 to version 2.
if (state.metadata.version() < 2 && GetCurrentDatabaseVersion() >= 2) {
MigrateShortcutAppsToDiyApps(state, changed_apps);
MigrateDefaultDisplayModeToPlatformDisplayMode(state, changed_apps);
MigratePartiallyInstalledAppsToCorrectState(state, changed_apps);
base::UmaHistogramSparse("WebApp.Database.VersionUpgradedTo", 2);
state.metadata.set_version(2);
did_change_metadata = true;
}
// Upgrade from version 2 to version 3.
if (state.metadata.version() < 3 && GetCurrentDatabaseVersion() >= 3) {
MigrateDeprecatedLaunchHandlerToClientMode(state, changed_apps);
MigrateScopeToRemoveRefAndQuery(state, changed_apps);
MigrateToRelativeManifestIdNoFragment(state, changed_apps);
base::UmaHistogramSparse("WebApp.Database.VersionUpgradedTo", 3);
state.metadata.set_version(3);
did_change_metadata = true;
}
CHECK_EQ(state.metadata.version(), GetCurrentDatabaseVersion());
if (did_change_metadata || !changed_apps.empty()) {
std::unique_ptr<syncer::DataTypeStore::WriteBatch> write_batch =
store_->CreateWriteBatch();
if (did_change_metadata) {
write_batch->WriteData(std::string(kDatabaseMetadataKey),
state.metadata.SerializeAsString());
}
for (const auto& app_id : changed_apps) {
CHECK(state.apps.contains(app_id));
write_batch->WriteData(app_id, state.apps[app_id].SerializeAsString());
}
store_->CommitWriteBatch(
std::move(write_batch),
base::BindOnce(&WebAppDatabase::OnDataWritten,
weak_ptr_factory_.GetWeakPtr(), base::DoNothing()));
}
}
void WebAppDatabase::MigrateInstallSourceAddUserInstalled(
ProtobufState& state,
std::set<webapps::AppId>& changed_apps) {
// Migrating from version 0 to version 1.
CHECK_LT(state.metadata.version(), 1);
const bool is_syncing_apps = database_factory_->IsSyncingApps();
int apps_migrated_count = 0;
for (auto& [app_id, app_proto] : state.apps) {
if (!app_proto.sources().sync()) {
continue;
}
bool changed = false;
if (!app_proto.sources().user_installed()) {
app_proto.mutable_sources()->set_user_installed(true);
changed = true;
}
if (!is_syncing_apps) {
app_proto.mutable_sources()->set_sync(false);
changed = true;
}
if (changed) {
changed_apps.insert(app_id);
apps_migrated_count++;
}
}
base::UmaHistogramCounts1000(
"WebApp.Migrations.InstallSourceAddUserInstalled", apps_migrated_count);
}
void WebAppDatabase::MigrateShortcutAppsToDiyApps(
WebAppDatabase::ProtobufState& state,
std::set<webapps::AppId>& changed_apps) {
// Migrating from version 1 to version 2.
CHECK_LT(state.metadata.version(), 2);
int shortcut_to_diy_apps = 0;
for (auto& [app_id, app_proto] : state.apps) {
bool is_shortcut =
!app_proto.has_scope() || app_proto.scope().empty() ||
(app_proto.has_latest_install_source() &&
app_proto.latest_install_source() ==
static_cast<uint32_t>(
webapps::WebappInstallSource::MENU_CREATE_SHORTCUT));
if (!is_shortcut) {
continue;
}
changed_apps.insert(app_id);
app_proto.set_is_diy_app(true);
app_proto.set_was_shortcut_app(true);
shortcut_to_diy_apps++;
if (app_proto.has_scope() && !app_proto.scope().empty() &&
GURL(app_proto.scope()).is_valid()) {
continue;
}
// Populate the scope if it was empty or invalid.
if (!app_proto.has_sync_data() || !app_proto.sync_data().has_start_url()) {
DLOG(ERROR) << "Missing sync data or start_url for shortcut app "
<< app_id;
continue;
}
GURL start_url(app_proto.sync_data().start_url());
if (!start_url.is_valid()) {
// Cannot recover scope, mark for potential cleanup later if needed.
DLOG(ERROR) << "Invalid start_url for shortcut app " << app_id << ":"
<< start_url.possibly_invalid_spec();
continue;
}
app_proto.set_scope(start_url.GetWithoutFilename().spec());
}
base::UmaHistogramCounts1000("WebApp.Migrations.ShortcutAppsToDiy2",
shortcut_to_diy_apps);
}
void WebAppDatabase::MigrateDefaultDisplayModeToPlatformDisplayMode(
WebAppDatabase::ProtobufState& state,
std::set<webapps::AppId>& changed_apps) {
// Migrating from version 1 to version 2.
CHECK_LT(state.metadata.version(), 2);
int apps_migrated_count = 0;
for (auto& [app_id, app_proto] : state.apps) {
if (!app_proto.has_sync_data()) {
// Cannot migrate without sync data.
continue;
}
sync_pb::WebAppSpecifics* sync_data = app_proto.mutable_sync_data();
if (!HasCurrentPlatformUserDisplayMode(*sync_data)) {
sync_pb::WebAppSpecifics_UserDisplayMode udm =
ResolvePlatformSpecificUserDisplayMode(*sync_data);
SetPlatformSpecificUserDisplayMode(udm, sync_data);
changed_apps.insert(app_id);
apps_migrated_count++;
}
}
base::UmaHistogramCounts1000("WebApp.Migrations.DefaultDisplayModeToPlatform",
apps_migrated_count);
}
// Corrects the install_state for apps that claim OS integration but lack the
// necessary OS integration state data.
void WebAppDatabase::MigratePartiallyInstalledAppsToCorrectState(
WebAppDatabase::ProtobufState& state,
std::set<webapps::AppId>& changed_apps) {
// Migrating from version 1 to version 2.
CHECK_LT(state.metadata.version(), 2);
int install_state_fixed_count = 0;
for (auto& [app_id, app_proto] : state.apps) {
if (app_proto.install_state() !=
proto::InstallState::INSTALLED_WITH_OS_INTEGRATION) {
continue;
}
// Check if any OS integration state exists. A simple check for shortcut
// presence is sufficient as a proxy for any OS integration.
if (app_proto.has_current_os_integration_states() &&
app_proto.current_os_integration_states().has_shortcut()) {
continue;
}
app_proto.set_install_state(
proto::InstallState::INSTALLED_WITHOUT_OS_INTEGRATION);
changed_apps.insert(app_id);
install_state_fixed_count++;
}
base::UmaHistogramCounts1000(
"WebApp.Migrations.PartiallyInstalledAppsToCorrectState",
install_state_fixed_count);
}
void WebAppDatabase::MigrateDeprecatedLaunchHandlerToClientMode(
ProtobufState& state,
std::set<webapps::AppId>& changed_apps) {
// Migrating from version 2 to version 3.
CHECK_LT(state.metadata.version(), 3);
int apps_migrated_count = 0;
for (auto& [app_id, app_proto] : state.apps) {
if (!app_proto.has_launch_handler()) {
continue;
}
bool changed = false;
proto::LaunchHandler* launch_handler = app_proto.mutable_launch_handler();
// If client_mode is unspecified, try migrating from deprecated fields.
if (launch_handler->client_mode() ==
proto::LaunchHandler::CLIENT_MODE_UNSPECIFIED) {
proto::LaunchHandler::ClientMode migrated_client_mode =
proto::LaunchHandler::CLIENT_MODE_UNSPECIFIED;
switch (launch_handler->route_to()) {
case proto::LaunchHandler_DeprecatedRouteTo_UNSPECIFIED_ROUTE:
break;
case proto::LaunchHandler_DeprecatedRouteTo_AUTO_ROUTE:
migrated_client_mode = proto::LaunchHandler::CLIENT_MODE_AUTO;
break;
case proto::LaunchHandler_DeprecatedRouteTo_NEW_CLIENT:
migrated_client_mode = proto::LaunchHandler::CLIENT_MODE_NAVIGATE_NEW;
break;
case proto::LaunchHandler_DeprecatedRouteTo_EXISTING_CLIENT:
if (launch_handler->navigate_existing_client() ==
proto::LaunchHandler_DeprecatedNavigateExistingClient_NEVER) {
migrated_client_mode =
proto::LaunchHandler::CLIENT_MODE_FOCUS_EXISTING;
} else {
migrated_client_mode =
proto::LaunchHandler::CLIENT_MODE_NAVIGATE_EXISTING;
}
break;
case proto::LaunchHandler_DeprecatedRouteTo_EXISTING_CLIENT_NAVIGATE:
migrated_client_mode =
proto::LaunchHandler::CLIENT_MODE_NAVIGATE_EXISTING;
break;
case proto::LaunchHandler_DeprecatedRouteTo_EXISTING_CLIENT_RETAIN:
migrated_client_mode =
proto::LaunchHandler::CLIENT_MODE_FOCUS_EXISTING;
break;
}
launch_handler->set_client_mode(migrated_client_mode);
changed = true;
} else if (launch_handler->client_mode() ==
proto::LaunchHandler::CLIENT_MODE_AUTO) {
// If client_mode is set to auto, and client_mode_valid_and_specified is
// explicitly false, treat client_mode as unspecified.
if (launch_handler->has_client_mode_valid_and_specified() &&
!launch_handler->client_mode_valid_and_specified()) {
launch_handler->set_client_mode(
proto::LaunchHandler::CLIENT_MODE_UNSPECIFIED);
changed = true;
}
}
// Clear deprecated fields if they exist.
if (launch_handler->has_route_to()) {
launch_handler->clear_route_to();
changed = true;
}
if (launch_handler->has_navigate_existing_client()) {
launch_handler->clear_navigate_existing_client();
changed = true;
}
if (launch_handler->has_client_mode_valid_and_specified()) {
launch_handler->clear_client_mode_valid_and_specified();
changed = true;
}
if (changed) {
changed_apps.insert(app_id);
apps_migrated_count++;
}
}
base::UmaHistogramCounts1000(
"WebApp.Migrations.DeprecatedLaunchHandlerToClientMode",
apps_migrated_count);
}
void WebAppDatabase::MigrateScopeToRemoveRefAndQuery(
ProtobufState& state,
std::set<webapps::AppId>& changed_apps) {
// Migrating from version 2 to version 3.
CHECK_LT(state.metadata.version(), 3);
int apps_migrated_count = 0;
for (auto& [app_id, app_proto] : state.apps) {
if (!app_proto.has_scope()) {
continue;
}
GURL scope(app_proto.scope());
if (!scope.is_valid()) {
continue;
}
if (scope.has_query() || scope.has_ref()) {
GURL::Replacements replacements;
replacements.ClearQuery();
replacements.ClearRef();
GURL clean_scope = scope.ReplaceComponents(replacements);
app_proto.set_scope(clean_scope.spec());
changed_apps.insert(app_id);
apps_migrated_count++;
}
}
base::UmaHistogramCounts1000("WebApp.Migrations.ScopeRefQueryRemoved",
apps_migrated_count);
}
void WebAppDatabase::MigrateToRelativeManifestIdNoFragment(
ProtobufState& state,
std::set<webapps::AppId>& changed_apps) {
// Migrating from version 2 to version 3.
CHECK_LT(state.metadata.version(), 3);
int apps_migrated_count = 0;
int fragment_removed_count = 0;
for (auto& [app_id, app_proto] : state.apps) {
if (!app_proto.has_sync_data()) {
continue;
}
sync_pb::WebAppSpecifics* sync_data = app_proto.mutable_sync_data();
if (!sync_data->has_start_url()) {
continue;
}
GURL start_url(sync_data->start_url());
if (!start_url.is_valid()) {
continue;
}
// Calculate the expected manifest_id and relative path without fragment.
webapps::ManifestId expected_manifest_id;
if (sync_data->has_relative_manifest_id()) {
expected_manifest_id =
GenerateManifestId(sync_data->relative_manifest_id(), start_url);
} else {
expected_manifest_id = GenerateManifestIdFromStartUrlOnly(start_url);
}
if (!expected_manifest_id.is_valid()) {
continue;
}
std::string expected_relative_path =
RelativeManifestIdPath(expected_manifest_id);
bool changed = false;
if (!sync_data->has_relative_manifest_id()) {
// Populate if missing.
sync_data->set_relative_manifest_id(expected_relative_path);
changed = true;
} else if (sync_data->relative_manifest_id() != expected_relative_path) {
// Correct if different (e.g., had a fragment).
sync_data->set_relative_manifest_id(expected_relative_path);
changed = true;
fragment_removed_count++;
}
if (changed) {
changed_apps.insert(app_id);
apps_migrated_count++;
}
}
base::UmaHistogramCounts1000(
"WebApp.Migrations.RelativeManifestIdFragmentRemoved",
fragment_removed_count);
base::UmaHistogramCounts1000(
"WebApp.Migrations.RelativeManifestIdPopulatedOrFixed",
apps_migrated_count);
}
void WebAppDatabase::OnDatabaseOpened(
RegistryOpenedCallback callback,
const std::optional<syncer::ModelError>& error,
std::unique_ptr<syncer::DataTypeStore> store) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (error) {
error_callback_.Run(*error);
DLOG(ERROR) << "WebApps LevelDB opening error: " << error->ToString();
return;
}
store_ = std::move(store);
store_->ReadAllDataAndMetadata(
base::BindOnce(&WebAppDatabase::OnAllDataAndMetadataRead,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
void WebAppDatabase::OnAllDataAndMetadataRead(
RegistryOpenedCallback callback,
const std::optional<syncer::ModelError>& error,
std::unique_ptr<syncer::DataTypeStore::RecordList> data_records,
std::unique_ptr<syncer::MetadataBatch> metadata_batch) {
TRACE_EVENT0("ui", "WebAppDatabase::OnAllMetadataRead");
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (error) {
error_callback_.Run(*error);
DLOG(ERROR) << "WebApps LevelDB read error: " << error->ToString();
return;
}
ProtobufState state = ParseProtobufs(*data_records);
MigrateDatabase(state);
Registry registry;
for (const auto& [app_id, app_proto] : state.apps) {
std::unique_ptr<WebApp> web_app = ParseWebAppProto(app_proto);
base::UmaHistogramBoolean("WebApp.Database.ValidProto", web_app != nullptr);
if (!web_app) {
continue;
}
// Record whether the derived app_id matches the database key.
bool mismatch = (web_app->app_id() != app_id);
base::UmaHistogramBoolean("WebApp.Database.AppIdMatch", !mismatch);
if (mismatch) {
DLOG(ERROR) << "WebApps LevelDB error: app_id doesn't match storage key "
<< app_id << " vs " << web_app->app_id() << ", from "
<< web_app->manifest_id();
continue;
}
registry.emplace(app_id, std::move(web_app));
}
opened_ = true;
// This should be a tail call: a callback code may indirectly call |this|
// methods, like WebAppDatabase::Write()
std::move(callback).Run(std::move(registry), std::move(metadata_batch));
}
void WebAppDatabase::OnDataWritten(
CompletionCallback callback,
const std::optional<syncer::ModelError>& error) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (error) {
error_callback_.Run(*error);
DLOG(ERROR) << "WebApps LevelDB write error: " << error->ToString();
}
std::move(callback).Run(!error);
}
} // namespace web_app
|