1 // Copyright 2017 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 COMPONENTS_BROWSING_DATA_CONTENT_SHARED_WORKER_HELPER_H_
6 #define COMPONENTS_BROWSING_DATA_CONTENT_SHARED_WORKER_HELPER_H_
7 
8 #include <stddef.h>
9 
10 #include <list>
11 #include <set>
12 #include <vector>
13 
14 #include "base/callback.h"
15 #include "base/macros.h"
16 #include "base/memory/ref_counted.h"
17 #include "url/gurl.h"
18 #include "url/origin.h"
19 
20 namespace content {
21 class StoragePartition;
22 class ResourceContext;
23 }  // namespace content
24 
25 namespace browsing_data {
26 
27 // Shared Workers don't persist browsing data outside of the regular storage
28 // mechanisms of their origin, however, we need the helper anyways to be able
29 // to show them as cookie-like data.
30 class SharedWorkerHelper
31     : public base::RefCountedThreadSafe<SharedWorkerHelper> {
32  public:
33   // Contains information about a Shared Worker.
34   struct SharedWorkerInfo {
35     SharedWorkerInfo(const GURL& worker,
36                      const std::string& name,
37                      const url::Origin& constructor_origin);
38     SharedWorkerInfo(const SharedWorkerInfo& other);
39     ~SharedWorkerInfo();
40 
41     bool operator<(const SharedWorkerInfo& other) const;
42 
43     GURL worker;
44     std::string name;
45     url::Origin constructor_origin;
46   };
47 
48   using FetchCallback =
49       base::OnceCallback<void(const std::list<SharedWorkerInfo>&)>;
50 
51   SharedWorkerHelper(content::StoragePartition* storage_partition,
52                      content::ResourceContext* resource_context);
53 
54   // Starts the fetching process returning the list of shared workers, which
55   // will notify its completion via |callback|. This must be called only in the
56   // UI thread.
57   virtual void StartFetching(FetchCallback callback);
58 
59   // Requests the given Shared Worker to be deleted.
60   virtual void DeleteSharedWorker(const GURL& worker,
61                                   const std::string& name,
62                                   const url::Origin& constructor_origin);
63 
64  protected:
65   virtual ~SharedWorkerHelper();
66 
67  private:
68   friend class base::RefCountedThreadSafe<SharedWorkerHelper>;
69 
70   content::StoragePartition* storage_partition_;
71   content::ResourceContext* resource_context_;
72 
73   DISALLOW_COPY_AND_ASSIGN(SharedWorkerHelper);
74 };
75 
76 // This class is an implementation of SharedWorkerHelper that does
77 // not fetch its information from the Shared Worker context, but is passed the
78 // info as a parameter.
79 class CannedSharedWorkerHelper : public SharedWorkerHelper {
80  public:
81   CannedSharedWorkerHelper(content::StoragePartition* storage_partition,
82                            content::ResourceContext* resource_context);
83 
84   // Adds Shared Worker to the set of canned Shared Workers that is returned by
85   // this helper.
86   void AddSharedWorker(const GURL& worker,
87                        const std::string& name,
88                        const url::Origin& constructor_origin);
89 
90   // Clears the list of canned Shared Workers.
91   void Reset();
92 
93   // True if no Shared Workers are currently in the set.
94   bool empty() const;
95 
96   // Returns the number of Shared Workers in the set.
97   size_t GetSharedWorkerCount() const;
98 
99   // Returns the current list of Shared Workers.
100   const std::set<CannedSharedWorkerHelper::SharedWorkerInfo>&
101   GetSharedWorkerInfo() const;
102 
103   // SharedWorkerHelper methods.
104   void StartFetching(FetchCallback callback) override;
105   void DeleteSharedWorker(const GURL& worker,
106                           const std::string& name,
107                           const url::Origin& constructor_origin) override;
108 
109  private:
110   ~CannedSharedWorkerHelper() override;
111 
112   std::set<SharedWorkerInfo> pending_shared_worker_info_;
113 
114   DISALLOW_COPY_AND_ASSIGN(CannedSharedWorkerHelper);
115 };
116 
117 }  // namespace browsing_data
118 
119 #endif  // COMPONENTS_BROWSING_DATA_CONTENT_SHARED_WORKER_HELPER_H_
120