1 // Copyright 2014 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 CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_SITE_LIST_H_
6 #define CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_SITE_LIST_H_
7 
8 #include <stddef.h>
9 #include <stdint.h>
10 
11 #include <array>
12 #include <memory>
13 #include <string>
14 #include <vector>
15 
16 #include "base/callback_forward.h"
17 #include "base/files/file_path.h"
18 #include "base/gtest_prod_util.h"
19 #include "base/hash/sha1.h"
20 #include "base/macros.h"
21 #include "base/memory/ref_counted.h"
22 #include "base/strings/string16.h"
23 #include "url/gurl.h"
24 
25 namespace base {
26 class ListValue;
27 class Value;
28 }
29 
30 // This class represents the content of a supervised user allowlist. It is
31 // loaded from a JSON file inside the extension bundle, which defines the sites
32 // on the list.
33 // All allowlists are combined in the SupervisedUserURLFilter, which can tell
34 // for a given URL if it is part of any allowlist. Effectively,
35 // SupervisedUserURLFilter then acts as a big allowlist which is the union of
36 // all the allowlists.
37 class SupervisedUserSiteList
38     : public base::RefCountedThreadSafe<SupervisedUserSiteList> {
39  public:
40   class HostnameHash {
41    public:
42     explicit HostnameHash(const std::string& hostname);
43     // |bytes| must have a size of at least |base::kSHA1Length|.
44     explicit HostnameHash(const std::vector<uint8_t>& bytes);
45     HostnameHash(const HostnameHash& other);
46 
47     bool operator==(const HostnameHash& rhs) const;
48 
49     // Returns a hash code suitable for putting this into hash maps.
50     size_t hash() const;
51 
52    private:
53     base::SHA1Digest bytes_;
54     // Copy and assign are allowed.
55   };
56 
57   using LoadedCallback =
58       base::Callback<void(const scoped_refptr<SupervisedUserSiteList>&)>;
59 
60   // Asynchronously loads the site list from |file| and calls |callback| with
61   // the newly created object.
62   static void Load(const std::string& id,
63                    const base::string16& title,
64                    const base::FilePath& large_icon_path,
65                    const base::FilePath& file,
66                    const LoadedCallback& callback);
67 
id()68   const std::string& id() const { return id_; }
title()69   const base::string16& title() const { return title_; }
entry_point()70   const GURL& entry_point() const { return entry_point_; }
large_icon_path()71   const base::FilePath& large_icon_path() const { return large_icon_path_; }
patterns()72   const std::vector<std::string>& patterns() const { return patterns_; }
hostname_hashes()73   const std::vector<HostnameHash>& hostname_hashes() const {
74     return hostname_hashes_;
75   }
76 
77  private:
78   friend class base::RefCountedThreadSafe<SupervisedUserSiteList>;
79   friend class SupervisedUserURLFilterTest;
80   FRIEND_TEST_ALL_PREFIXES(SupervisedUserURLFilterTest, AllowlistsPatterns);
81   FRIEND_TEST_ALL_PREFIXES(SupervisedUserURLFilterTest,
82                            AllowlistsHostnameHashes);
83 
84   SupervisedUserSiteList(const std::string& id,
85                          const base::string16& title,
86                          const GURL& entry_point,
87                          const base::FilePath& large_icon_path,
88                          const base::ListValue* patterns,
89                          const base::ListValue* hostname_hashes);
90   // Used for testing.
91   SupervisedUserSiteList(const std::string& id,
92                          const base::string16& title,
93                          const GURL& entry_point,
94                          const base::FilePath& large_icon_path,
95                          const std::vector<std::string>& patterns,
96                          const std::vector<std::string>& hostname_hashes);
97   ~SupervisedUserSiteList();
98 
99   // Static private so it can access the private constructor.
100   static void OnJsonLoaded(
101       const std::string& id,
102       const base::string16& title,
103       const base::FilePath& large_icon_path,
104       const base::FilePath& path,
105       const SupervisedUserSiteList::LoadedCallback& callback,
106       std::unique_ptr<base::Value> value);
107 
108   std::string id_;
109   base::string16 title_;
110   GURL entry_point_;
111   base::FilePath large_icon_path_;
112 
113   // A list of URL patterns that should be allowed.
114   std::vector<std::string> patterns_;
115 
116   // A list of SHA1 hashes of hostnames that should be allowed.
117   std::vector<HostnameHash> hostname_hashes_;
118 
119   DISALLOW_COPY_AND_ASSIGN(SupervisedUserSiteList);
120 };
121 
122 #endif  // CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_SITE_LIST_H_
123