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

content / browser / cookie_store / cookie_change_subscription.h [blame]

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

#ifndef CONTENT_BROWSER_COOKIE_STORE_COOKIE_CHANGE_SUBSCRIPTION_H_
#define CONTENT_BROWSER_COOKIE_STORE_COOKIE_CHANGE_SUBSCRIPTION_H_

#include <memory>
#include <string>

#include "base/containers/linked_list.h"
#include "third_party/blink/public/mojom/cookie_store/cookie_store.mojom.h"
#include "url/gurl.h"

namespace net {
class CanonicalCookie;
enum class CookieAccessSemantics;
}  // namespace net

namespace content {

namespace proto {

class CookieChangeSubscriptionProto;

}  // namespace proto

// Represents a single subscription to the list of cookies sent to a URL.
//
// The included linked list node and service worker registration ID are used by
// CookieStoreManager. They are ignored when comparing instances.
class CookieChangeSubscription
    : public base::LinkNode<CookieChangeSubscription> {
 public:
  // Used to read a service worker's subscriptions from the persistent store.
  //
  // Returns an empty vector if deserialization failed.
  static std::vector<std::unique_ptr<CookieChangeSubscription>>
  DeserializeVector(const std::string& proto_string,
                    int64_t service_worker_registration_id);

  // Used to write a service worker's subscriptions to the service worker store.
  //
  // The subscriptions vector should not be empty. If a registration does not
  // have subscriptions, it should not be serialized.
  //
  // Returns the empty string in case of a serialization error.
  static std::string SerializeVector(
      const std::vector<std::unique_ptr<CookieChangeSubscription>>&
          subscriptions);

  // Converts a service worker's subscriptions to a Mojo API call result.
  static std::vector<blink::mojom::CookieChangeSubscriptionPtr> ToMojoVector(
      const std::vector<std::unique_ptr<CookieChangeSubscription>>&
          subscription);

  // Public for testing.
  //
  // Production code should use the vector-based factory methods above.
  //
  // Returns null in case of deserialization error.
  static std::unique_ptr<CookieChangeSubscription> Create(
      proto::CookieChangeSubscriptionProto proto,
      int64_t service_worker_registration_id);

  // Converts a subscription from a Mojo API call.
  CookieChangeSubscription(
      blink::mojom::CookieChangeSubscriptionPtr mojo_subscription,
      int64_t service_worker_registration_id);

  // Public for testing.
  //
  // Production code should use the vector-based factory methods above.
  CookieChangeSubscription(GURL url,
                           std::string name,
                           ::network::mojom::CookieMatchType match_type,
                           int64_t service_worker_registration_id);

  CookieChangeSubscription(const CookieChangeSubscription&) = delete;
  CookieChangeSubscription& operator=(const CookieChangeSubscription&) = delete;

  ~CookieChangeSubscription();

  // The URL whose cookie list is watched for changes.
  const GURL& url() const { return url_; }

  // Operator for name-based matching.
  //
  // This is used to implement both equality and prefix-based name matching.
  // Supporting the latter helps avoid wasting battery by waking up service
  // workers unnecessarily.
  ::network::mojom::CookieMatchType match_type() const { return match_type_; }

  // Operand for the name-based matching operator above.
  //
  // For EQUAL matching, the cookie name must precisely match name(). For
  // STARTS_WITH matching, the cookie name must be prefixed by name().
  const std::string& name() const { return name_; }

  // The service worker registration that this subscription belongs to.
  int64_t service_worker_registration_id() const {
    return service_worker_registration_id_;
  }

  // Writes the subscription to the given protobuf.
  void Serialize(proto::CookieChangeSubscriptionProto* proto) const;
  // Writes the subscription to the given Mojo object.
  void Serialize(
      blink::mojom::CookieChangeSubscription* mojo_subscription) const;

  // True if the subscription covers a change to the given cookie.
  bool ShouldObserveChangeTo(const net::CanonicalCookie& cookie,
                             net::CookieAccessSemantics access_semantics) const;

 private:
  const GURL url_;
  const std::string name_;
  const ::network::mojom::CookieMatchType match_type_;
  const int64_t service_worker_registration_id_;
};

// Used to deduplicate equivalent subscriptons.
//
// Ignores the service worker registration ID and the linked list node.
bool operator==(const CookieChangeSubscription& lhs,
                const CookieChangeSubscription& rhs);

}  // namespace content

#endif  // CONTENT_BROWSER_COOKIE_STORE_COOKIE_CHANGE_SUBSCRIPTION_H_