summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/modules/background_fetch/background_fetch_bridge.cc
blob: 022104ceead2f0041afcfde559729a9e468308d8 (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
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 2017 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 "third_party/blink/renderer/modules/background_fetch/background_fetch_bridge.h"

#include <utility>
#include "services/service_manager/public/cpp/interface_provider.h"
#include "third_party/blink/public/platform/modules/service_worker/web_service_worker_registration.h"
#include "third_party/blink/public/platform/modules/service_worker/web_service_worker_request.h"
#include "third_party/blink/renderer/modules/background_fetch/background_fetch_options.h"
#include "third_party/blink/renderer/modules/background_fetch/background_fetch_registration.h"
#include "third_party/blink/renderer/modules/background_fetch/background_fetch_type_converters.h"
#include "third_party/blink/renderer/modules/manifest/image_resource.h"

namespace blink {

// static
BackgroundFetchBridge* BackgroundFetchBridge::From(
    ServiceWorkerRegistration* service_worker_registration) {
  DCHECK(service_worker_registration);

  BackgroundFetchBridge* bridge =
      Supplement<ServiceWorkerRegistration>::From<BackgroundFetchBridge>(
          service_worker_registration);

  if (!bridge) {
    bridge = new BackgroundFetchBridge(*service_worker_registration);
    ProvideTo(*service_worker_registration, bridge);
  }

  return bridge;
}

// static
const char BackgroundFetchBridge::kSupplementName[] = "BackgroundFetchBridge";

BackgroundFetchBridge::BackgroundFetchBridge(
    ServiceWorkerRegistration& registration)
    : Supplement<ServiceWorkerRegistration>(registration) {}

BackgroundFetchBridge::~BackgroundFetchBridge() = default;

void BackgroundFetchBridge::GetIconDisplaySize(
    GetIconDisplaySizeCallback callback) {
  GetService()->GetIconDisplaySize(std::move(callback));
}

void BackgroundFetchBridge::MatchRequests(
    const String& developer_id,
    const String& unique_id,
    base::Optional<WebServiceWorkerRequest> request_to_match,
    mojom::blink::QueryParamsPtr cache_query_params,
    bool match_all,
    mojom::blink::BackgroundFetchService::MatchRequestsCallback callback) {
  GetService()->MatchRequests(
      GetSupplementable()->WebRegistration()->RegistrationId(), developer_id,
      unique_id, std::move(request_to_match), std::move(cache_query_params),
      match_all, std::move(callback));
}

void BackgroundFetchBridge::Fetch(
    const String& developer_id,
    Vector<WebServiceWorkerRequest> requests,
    mojom::blink::BackgroundFetchOptionsPtr options,
    const SkBitmap& icon,
    mojom::blink::BackgroundFetchUkmDataPtr ukm_data,
    RegistrationCallback callback) {
  GetService()->Fetch(
      GetSupplementable()->WebRegistration()->RegistrationId(), developer_id,
      std::move(requests), std::move(options), icon, std::move(ukm_data),
      WTF::Bind(&BackgroundFetchBridge::DidGetRegistration,
                WrapPersistent(this), WTF::Passed(std::move(callback))));
}

void BackgroundFetchBridge::Abort(const String& developer_id,
                                  const String& unique_id,
                                  AbortCallback callback) {
  GetService()->Abort(GetSupplementable()->WebRegistration()->RegistrationId(),
                      developer_id, unique_id, std::move(callback));
}

void BackgroundFetchBridge::UpdateUI(const String& developer_id,
                                     const String& unique_id,
                                     const String& title,
                                     const SkBitmap& icon,
                                     UpdateUICallback callback) {
  if (title.IsNull() && icon.isNull()) {
    std::move(callback).Run(
        mojom::blink::BackgroundFetchError::INVALID_ARGUMENT);
    return;
  }

  GetService()->UpdateUI(
      GetSupplementable()->WebRegistration()->RegistrationId(), developer_id,
      unique_id, title, icon, std::move(callback));
}

void BackgroundFetchBridge::GetRegistration(const String& developer_id,
                                            RegistrationCallback callback) {
  GetService()->GetRegistration(
      GetSupplementable()->WebRegistration()->RegistrationId(), developer_id,
      WTF::Bind(&BackgroundFetchBridge::DidGetRegistration,
                WrapPersistent(this), WTF::Passed(std::move(callback))));
}

void BackgroundFetchBridge::DidGetRegistration(
    RegistrationCallback callback,
    mojom::blink::BackgroundFetchError error,
    mojom::blink::BackgroundFetchRegistrationPtr registration_ptr) {
  BackgroundFetchRegistration* registration =
      registration_ptr.To<BackgroundFetchRegistration*>();

  if (registration) {
    DCHECK_EQ(error, mojom::blink::BackgroundFetchError::NONE);
    DCHECK_EQ(registration->result(), "");
    registration->Initialize(GetSupplementable());
  }

  std::move(callback).Run(error, registration);
}

void BackgroundFetchBridge::GetDeveloperIds(GetDeveloperIdsCallback callback) {
  GetService()->GetDeveloperIds(
      GetSupplementable()->WebRegistration()->RegistrationId(),
      std::move(callback));
}

void BackgroundFetchBridge::AddRegistrationObserver(
    const String& unique_id,
    mojom::blink::BackgroundFetchRegistrationObserverPtr observer) {
  GetService()->AddRegistrationObserver(unique_id, std::move(observer));
}

mojom::blink::BackgroundFetchService* BackgroundFetchBridge::GetService() {
  if (!background_fetch_service_) {
    auto request = mojo::MakeRequest(&background_fetch_service_);
    if (auto* interface_provider = GetSupplementable()
                                       ->GetExecutionContext()
                                       ->GetInterfaceProvider()) {
      interface_provider->GetInterface(std::move(request));
    }
  }
  return background_fetch_service_.get();
}

}  // namespace blink