1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CONTENT_PUBLIC_BROWSER_CORS_ORIGIN_PATTERN_SETTER_H_
6 #define CONTENT_PUBLIC_BROWSER_CORS_ORIGIN_PATTERN_SETTER_H_
7 
8 #include <utility>
9 #include <vector>
10 
11 #include "base/barrier_closure.h"
12 #include "base/callback.h"
13 #include "content/common/content_export.h"
14 #include "services/network/public/mojom/cors_origin_pattern.mojom.h"
15 #include "url/origin.h"
16 
17 namespace content {
18 
19 class StoragePartition;
20 
21 // A class used to make an asynchronous Mojo call with cloned patterns for each
22 // StoragePartition iteration. |this| instance will be destructed when all
23 // existing asynchronous Mojo calls made in SetLists() are done, and |closure|
24 // will be invoked on destructing |this|.
25 //
26 // Typically this would be used to implement
27 // BrowserContext::SetCorsOriginAccessListForOrigin, and would use
28 // ForEachStoragePartition with SetLists as the StoragePartitionCallback.
29 class CONTENT_EXPORT CorsOriginPatternSetter
30     : public base::RefCounted<CorsOriginPatternSetter> {
31  public:
32   CorsOriginPatternSetter(
33       const url::Origin& source_origin,
34       std::vector<network::mojom::CorsOriginPatternPtr> allow_patterns,
35       std::vector<network::mojom::CorsOriginPatternPtr> block_patterns,
36       base::OnceClosure closure);
37 
38   void SetLists(StoragePartition* partition);
39 
40   static std::vector<network::mojom::CorsOriginPatternPtr> ClonePatterns(
41       const std::vector<network::mojom::CorsOriginPatternPtr>& patterns);
42 
43  private:
44   friend class base::RefCounted<CorsOriginPatternSetter>;
45 
46   ~CorsOriginPatternSetter();
47 
48   const url::Origin source_origin_;
49   const std::vector<network::mojom::CorsOriginPatternPtr> allow_patterns_;
50   const std::vector<network::mojom::CorsOriginPatternPtr> block_patterns_;
51 
52   base::OnceClosure closure_;
53 
54   DISALLOW_COPY_AND_ASSIGN(CorsOriginPatternSetter);
55 };
56 
57 }  // namespace content
58 
59 #endif  // CONTENT_PUBLIC_BROWSER_CORS_ORIGIN_PATTERN_SETTER_H_
60