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

content / public / browser / cors_origin_pattern_setter.h [blame]

// Copyright 2019 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_PUBLIC_BROWSER_CORS_ORIGIN_PATTERN_SETTER_H_
#define CONTENT_PUBLIC_BROWSER_CORS_ORIGIN_PATTERN_SETTER_H_

#include <utility>
#include <vector>

#include "base/barrier_closure.h"
#include "base/functional/callback.h"
#include "base/types/pass_key.h"
#include "content/common/content_export.h"
#include "services/network/public/mojom/cors_origin_pattern.mojom.h"
#include "url/origin.h"

namespace content {

class BrowserContext;
class SharedCorsOriginAccessList;
class StoragePartition;

// A class for mutating CORS examptions list associated with a BrowserContext:
// 1. the in-process list in BrowserContext::shared_cors_origin_access_list
// 2. the per-NetworkContext (i.e. per-StoragePartition) lists
class CONTENT_EXPORT CorsOriginPatternSetter
    : public base::RefCounted<CorsOriginPatternSetter> {
 public:
  // Sets |allow_patterns| and |block_patterns| for |source_origin| for the
  // |browser_context|.
  //
  // The new settings will be 1) set in the SharedCorsOriginAccessList from
  // |browser_context|'s GetSharedCorsOriginAccessList as well as 2) pushed to
  // all network::mojom::NetworkContexts associated with all the current
  // StoragePartitions of the |browser_context|.  |closure| will be called once
  // all the stores/pushes have been acked.
  static void Set(
      content::BrowserContext* browser_context,
      const url::Origin& source_origin,
      std::vector<network::mojom::CorsOriginPatternPtr> allow_patterns,
      std::vector<network::mojom::CorsOriginPatternPtr> block_patterns,
      base::OnceClosure closure);

  // The constructor is semi-private (CorsOriginPatternSetter should only be
  // constructed internally, within the implementation of the public Set
  // method).
  using PassKey = base::PassKey<CorsOriginPatternSetter>;
  CorsOriginPatternSetter(
      base::PassKey<CorsOriginPatternSetter> pass_key,
      const url::Origin& source_origin,
      std::vector<network::mojom::CorsOriginPatternPtr> allow_patterns,
      std::vector<network::mojom::CorsOriginPatternPtr> block_patterns,
      base::OnceClosure closure);

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

 private:
  friend class base::RefCounted<CorsOriginPatternSetter>;
  ~CorsOriginPatternSetter();

  void SetForStoragePartition(content::StoragePartition* partition);

  const url::Origin source_origin_;
  const std::vector<network::mojom::CorsOriginPatternPtr> allow_patterns_;
  const std::vector<network::mojom::CorsOriginPatternPtr> block_patterns_;

  base::OnceClosure closure_;
};

}  // namespace content

#endif  // CONTENT_PUBLIC_BROWSER_CORS_ORIGIN_PATTERN_SETTER_H_