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

content / public / browser / push_messaging_service.cc [blame]

// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/public/browser/push_messaging_service.h"

#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "content/browser/push_messaging/push_messaging_manager.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/storage_partition.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"

namespace content {

namespace {

void CallStringCallback(
    PushMessagingService::RegistrationUserDataCallback callback,
    const std::vector<std::string>& data,
    blink::ServiceWorkerStatusCode service_worker_status) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  bool success = service_worker_status == blink::ServiceWorkerStatusCode::kOk;
  std::move(callback).Run(success ? data : std::vector<std::string>());
}

void CallClosure(base::OnceClosure callback,
                 blink::ServiceWorkerStatusCode status) {
  std::move(callback).Run();
}

scoped_refptr<ServiceWorkerContextWrapper> GetServiceWorkerContext(
    BrowserContext* browser_context, const GURL& origin) {
  StoragePartition* partition =
      browser_context->GetStoragePartitionForUrl(origin);
  return base::WrapRefCounted(static_cast<ServiceWorkerContextWrapper*>(
      partition->GetServiceWorkerContext()));
}

void GetSWDataCallback(PushMessagingService::SWDataCallback callback,
                       const std::vector<std::string>& result) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  std::string sender_id;
  std::string subscription_id;
  if (!result.empty()) {
    DCHECK_EQ(2u, result.size());
    sender_id = result[0];
    subscription_id = result[1];
  }
  std::move(callback).Run(sender_id, subscription_id);
}

void GetSenderIdCallback(PushMessagingService::SenderIdCallback callback,
                         const std::vector<std::string>& result) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  std::string sender_id;
  if (!result.empty()) {
    DCHECK_EQ(1u, result.size());
    sender_id = result[0];
  }
  std::move(callback).Run(sender_id);
}

}  // anonymous namespace

// static
void PushMessagingService::GetSenderId(BrowserContext* browser_context,
                                       const GURL& origin,
                                       int64_t service_worker_registration_id,
                                       SenderIdCallback callback) {
  PushMessagingService::RegistrationUserDataCallback service_callback =
      base::BindOnce(&GetSenderIdCallback, std::move(callback));
  ServiceWorkerContextWrapper::GetUserDataCallback wrapper_callback =
      base::BindOnce(&CallStringCallback, std::move(service_callback));

  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  GetServiceWorkerContext(browser_context, origin)
      ->GetRegistrationUserData(
          service_worker_registration_id,
          std::vector<std::string>{kPushSenderIdServiceWorkerKey},
          std::move(wrapper_callback));
}

// static
void PushMessagingService::GetSWData(BrowserContext* browser_context,
                                     const GURL& origin,
                                     int64_t service_worker_registration_id,
                                     SWDataCallback callback) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  PushMessagingService::RegistrationUserDataCallback service_callback =
      base::BindOnce(&GetSWDataCallback, std::move(callback));
  ServiceWorkerContextWrapper::GetUserDataCallback wrapper_callback =
      base::BindOnce(&CallStringCallback, std::move(service_callback));

  GetServiceWorkerContext(browser_context, origin)
      ->GetRegistrationUserData(
          service_worker_registration_id,
          std::vector<std::string>{kPushSenderIdServiceWorkerKey,
                                   kPushRegistrationIdServiceWorkerKey},
          std::move(wrapper_callback));
}

// static
void PushMessagingService::ClearPushSubscriptionId(
    BrowserContext* browser_context,
    const GURL& origin,
    int64_t service_worker_registration_id,
    base::OnceClosure callback) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  GetServiceWorkerContext(browser_context, origin)
      ->ClearRegistrationUserData(
          service_worker_registration_id, {kPushRegistrationIdServiceWorkerKey},
          base::BindOnce(&CallClosure, std::move(callback)));
}

// static
void PushMessagingService::UpdatePushSubscriptionId(
    BrowserContext* browser_context,
    const GURL& origin,
    int64_t service_worker_registration_id,
    const std::string& subscription_id,
    base::OnceClosure callback) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  GetServiceWorkerContext(browser_context, origin)
      ->StoreRegistrationUserData(
          service_worker_registration_id,
          blink::StorageKey::CreateFirstParty(url::Origin::Create(origin)),
          {{kPushRegistrationIdServiceWorkerKey, subscription_id}},
          base::BindOnce(&CallClosure, std::move(callback)));
}

// static
void PushMessagingService::StorePushSubscriptionForTesting(
    BrowserContext* browser_context,
    const GURL& origin,
    int64_t service_worker_registration_id,
    const std::string& subscription_id,
    const std::string& sender_id,
    base::OnceClosure callback) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  GetServiceWorkerContext(browser_context, origin)
      ->StoreRegistrationUserData(
          service_worker_registration_id,
          blink::StorageKey::CreateFirstParty(url::Origin::Create(origin)),
          {{kPushRegistrationIdServiceWorkerKey, subscription_id},
           {kPushSenderIdServiceWorkerKey, sender_id}},
          base::BindOnce(&CallClosure, std::move(callback)));
}

}  // namespace content