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

content / browser / shared_storage / shared_storage_event_params.cc [blame]

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

#include <algorithm>

#include "base/debug/crash_logging.h"
#include "content/browser/shared_storage/shared_storage_event_params.h"

namespace content {

const size_t kSharedStorageSerializedDataLengthLimitForEventParams = 1024;

std::string MaybeTruncateSerializedData(
    const blink::CloneableMessage& serialized_data) {
  SCOPED_CRASH_KEY_NUMBER("SharedStorageEventParams", "data_size",
                          serialized_data.owned_encoded_message.size());
  size_t length =
      std::min(serialized_data.owned_encoded_message.size(),
               kSharedStorageSerializedDataLengthLimitForEventParams);
  return std::string(serialized_data.owned_encoded_message.begin(),
                     serialized_data.owned_encoded_message.begin() + length);
}

SharedStorageEventParams::SharedStorageUrlSpecWithMetadata::
    SharedStorageUrlSpecWithMetadata() = default;

SharedStorageEventParams::SharedStorageUrlSpecWithMetadata::
    SharedStorageUrlSpecWithMetadata(
        const GURL& url,
        std::map<std::string, std::string> reporting_metadata)
    : url(url.spec()), reporting_metadata(std::move(reporting_metadata)) {}

SharedStorageEventParams::SharedStorageUrlSpecWithMetadata::
    SharedStorageUrlSpecWithMetadata(const SharedStorageUrlSpecWithMetadata&) =
        default;

SharedStorageEventParams::SharedStorageUrlSpecWithMetadata::
    ~SharedStorageUrlSpecWithMetadata() = default;

SharedStorageEventParams::SharedStorageUrlSpecWithMetadata&
SharedStorageEventParams::SharedStorageUrlSpecWithMetadata::operator=(
    const SharedStorageUrlSpecWithMetadata&) = default;

bool SharedStorageEventParams::SharedStorageUrlSpecWithMetadata::operator==(
    const SharedStorageUrlSpecWithMetadata& other) const {
  return url == other.url && reporting_metadata == other.reporting_metadata;
}

SharedStorageEventParams::SharedStorageEventParams(
    const SharedStorageEventParams&) = default;

SharedStorageEventParams::~SharedStorageEventParams() = default;

SharedStorageEventParams& SharedStorageEventParams::operator=(
    const SharedStorageEventParams&) = default;

SharedStorageEventParams::SharedStorageEventParams() = default;

SharedStorageEventParams::SharedStorageEventParams(
    std::optional<std::string> script_source_url,
    std::optional<std::string> operation_name,
    std::optional<std::string> serialized_data,
    std::optional<std::vector<SharedStorageUrlSpecWithMetadata>>
        urls_with_metadata,
    std::optional<std::string> key,
    std::optional<std::string> value,
    std::optional<bool> ignore_if_present)
    : script_source_url(std::move(script_source_url)),
      operation_name(std::move(operation_name)),
      serialized_data(std::move(serialized_data)),
      urls_with_metadata(std::move(urls_with_metadata)),
      key(std::move(key)),
      value(std::move(value)),
      ignore_if_present(ignore_if_present) {}

// static
SharedStorageEventParams SharedStorageEventParams::CreateForAddModule(
    const GURL& script_source_url) {
  return SharedStorageEventParams(script_source_url.spec(), std::nullopt,
                                  std::nullopt, std::nullopt, std::nullopt,
                                  std::nullopt, std::nullopt);
}

// static
SharedStorageEventParams SharedStorageEventParams::CreateForRun(
    const std::string& operation_name,
    const blink::CloneableMessage& serialized_data) {
  return SharedStorageEventParams(std::nullopt, operation_name,
                                  MaybeTruncateSerializedData(serialized_data),
                                  std::nullopt, std::nullopt, std::nullopt,
                                  std::nullopt);
}

// static
SharedStorageEventParams SharedStorageEventParams::CreateForSelectURL(
    const std::string& operation_name,
    const blink::CloneableMessage& serialized_data,
    std::vector<SharedStorageUrlSpecWithMetadata> urls_with_metadata) {
  return SharedStorageEventParams(std::nullopt, operation_name,
                                  MaybeTruncateSerializedData(serialized_data),
                                  std::move(urls_with_metadata), std::nullopt,
                                  std::nullopt, std::nullopt);
}

// static
SharedStorageEventParams SharedStorageEventParams::CreateForSet(
    const std::string& key,
    const std::string& value,
    bool ignore_if_present) {
  return SharedStorageEventParams(std::nullopt, std::nullopt, std::nullopt,
                                  std::nullopt, key, value, ignore_if_present);
}

// static
SharedStorageEventParams SharedStorageEventParams::CreateForAppend(
    const std::string& key,
    const std::string& value) {
  return SharedStorageEventParams(std::nullopt, std::nullopt, std::nullopt,
                                  std::nullopt, key, value, std::nullopt);
}

// static
SharedStorageEventParams SharedStorageEventParams::CreateForGetOrDelete(
    const std::string& key) {
  return SharedStorageEventParams(std::nullopt, std::nullopt, std::nullopt,
                                  std::nullopt, key, std::nullopt,
                                  std::nullopt);
}

// static
SharedStorageEventParams SharedStorageEventParams::CreateDefault() {
  return SharedStorageEventParams();
}

}  // namespace content