summaryrefslogtreecommitdiff
path: root/chromium/content/browser/background_fetch/storage/update_registration_ui_task.cc
blob: 38ad299080f36c0cd0b9ddfb8fdefdefaf2ada18 (plain)
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
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/background_fetch/storage/update_registration_ui_task.h"

#include "content/browser/background_fetch/background_fetch_data_manager.h"
#include "content/browser/background_fetch/background_fetch_data_manager_observer.h"
#include "content/browser/background_fetch/storage/database_helpers.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h"
#include "third_party/blink/public/platform/modules/background_fetch/background_fetch.mojom.h"

namespace content {

namespace background_fetch {

UpdateRegistrationUITask::UpdateRegistrationUITask(
    DatabaseTaskHost* host,
    const BackgroundFetchRegistrationId& registration_id,
    const std::string& updated_title,
    UpdateRegistrationUICallback callback)
    : DatabaseTask(host),
      registration_id_(registration_id),
      updated_title_(updated_title),
      callback_(std::move(callback)),
      weak_factory_(this) {}

UpdateRegistrationUITask::~UpdateRegistrationUITask() = default;

void UpdateRegistrationUITask::Start() {
  service_worker_context()->StoreRegistrationUserData(
      registration_id_.service_worker_registration_id(),
      registration_id_.origin().GetURL(),
      {{TitleKey(registration_id_.unique_id()), updated_title_}},
      base::BindOnce(&UpdateRegistrationUITask::DidUpdateTitle,
                     weak_factory_.GetWeakPtr()));
}

void UpdateRegistrationUITask::DidUpdateTitle(
    blink::ServiceWorkerStatusCode status) {
  switch (ToDatabaseStatus(status)) {
    case DatabaseStatus::kOk:
      break;
    case DatabaseStatus::kFailed:
    case DatabaseStatus::kNotFound:
      FinishWithError(blink::mojom::BackgroundFetchError::STORAGE_ERROR);
      return;
  }

  for (auto& observer : data_manager()->observers())
    observer.OnUpdatedUI(registration_id_, updated_title_);

  FinishWithError(blink::mojom::BackgroundFetchError::NONE);
}

void UpdateRegistrationUITask::FinishWithError(
    blink::mojom::BackgroundFetchError error) {
  std::move(callback_).Run(error);
  Finished();  // Destroys |this|.
}

}  // namespace background_fetch

}  // namespace content