File: web_app_registry_update.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 (92 lines) | stat: -rw-r--r-- 2,933 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
// Copyright 2019 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_registry_update.h"

#include "base/containers/contains.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/types/pass_key.h"
#include "chrome/browser/web_applications/web_app_registrar.h"

namespace web_app {

RegistryUpdateData::RegistryUpdateData() = default;

RegistryUpdateData::~RegistryUpdateData() = default;

bool RegistryUpdateData::IsEmpty() const {
  return apps_to_create.empty() && apps_to_delete.empty() &&
         apps_to_update.empty();
}

WebAppRegistryUpdate::WebAppRegistryUpdate(const WebAppRegistrar* registrar,
                                           base::PassKey<WebAppSyncBridge>)
    : registrar_(registrar) {
  DCHECK(registrar_);
  update_data_ = std::make_unique<RegistryUpdateData>();
}

WebAppRegistryUpdate::~WebAppRegistryUpdate() = default;

void WebAppRegistryUpdate::CreateApp(std::unique_ptr<WebApp> web_app) {
  DCHECK(update_data_);
  CHECK(web_app->manifest_id().is_valid());
  DCHECK(!web_app->app_id().empty());
  DCHECK(!registrar_->GetAppById(web_app->app_id()));
  DCHECK(!base::Contains(update_data_->apps_to_create, web_app));

  update_data_->apps_to_create.push_back(std::move(web_app));
}

void WebAppRegistryUpdate::DeleteApp(const webapps::AppId& app_id) {
  DCHECK(update_data_);
  DCHECK(!app_id.empty());
  DCHECK(registrar_->GetAppById(app_id));
  DCHECK(!base::Contains(update_data_->apps_to_delete, app_id));

  update_data_->apps_to_delete.push_back(app_id);
}

WebApp* WebAppRegistryUpdate::UpdateApp(const webapps::AppId& app_id) {
  DCHECK(update_data_);
  const WebApp* original_app = registrar_->GetAppById(app_id);
  if (!original_app)
    return nullptr;

  for (auto& app_to_update : update_data_->apps_to_update) {
    if (app_to_update->app_id() == app_id)
      return app_to_update.get();
  }

  // Make a copy on write.
  auto app_copy = std::make_unique<WebApp>(*original_app);
  WebApp* app_copy_ptr = app_copy.get();
  update_data_->apps_to_update.push_back(std::move(app_copy));

  return app_copy_ptr;
}

std::unique_ptr<RegistryUpdateData> WebAppRegistryUpdate::TakeUpdateData(
    base::PassKey<WebAppSyncBridge> pass_key) {
  return std::move(update_data_);
}

ScopedRegistryUpdate::ScopedRegistryUpdate(
    base::PassKey<WebAppSyncBridge>,
    std::unique_ptr<WebAppRegistryUpdate> update,
    base::OnceCallback<void(std::unique_ptr<WebAppRegistryUpdate>)>
        commit_update)
    : update_(std::move(update)), commit_update_(std::move(commit_update)) {}

ScopedRegistryUpdate::ScopedRegistryUpdate(ScopedRegistryUpdate&&) noexcept =
    default;

ScopedRegistryUpdate::~ScopedRegistryUpdate() {
  if (update_) {
    std::move(commit_update_).Run(std::move(update_));
  }
}

}  // namespace web_app