1 // Copyright (c) 2012 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_MANAGED_MODE_MANAGED_MODE_URL_FILTER_H_ 6 #define CHROME_BROWSER_MANAGED_MODE_MANAGED_MODE_URL_FILTER_H_ 7 8 #include "base/callback_forward.h" 9 #include "base/memory/ref_counted.h" 10 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_vector.h" 12 #include "base/observer_list.h" 13 #include "base/threading/non_thread_safe.h" 14 #include "base/values.h" 15 #include "chrome/browser/managed_mode/managed_mode_site_list.h" 16 #include "chrome/browser/managed_mode/managed_users.h" 17 18 class GURL; 19 20 // This class manages the filtering behavior for a given URL, i.e. it tells 21 // callers if a given URL should be allowed, blocked or warned about. It uses 22 // information from multiple sources: 23 // * A default setting (allow, block or warn). 24 // * The set of installed and enabled content packs, which contain whitelists 25 // of URL patterns that should be allowed. 26 // * User-specified manual overrides (allow or block) for either sites 27 // (hostnames) or exact URLs, which take precedence over the previous 28 // sources. 29 // References to it can be passed around on different threads (the refcounting 30 // is thread-safe), but the object itself should always be accessed on the same 31 // thread (member access isn't thread-safe). 32 class ManagedModeURLFilter 33 : public base::RefCountedThreadSafe<ManagedModeURLFilter>, 34 public base::NonThreadSafe { 35 public: 36 enum FilteringBehavior { 37 ALLOW, 38 WARN, 39 BLOCK, 40 HISTOGRAM_BOUNDING_VALUE 41 }; 42 43 class Observer { 44 public: 45 virtual void OnSiteListUpdated() = 0; 46 }; 47 48 struct Contents; 49 50 ManagedModeURLFilter(); 51 52 static FilteringBehavior BehaviorFromInt(int behavior_value); 53 54 // Normalizes a URL for matching purposes. 55 static GURL Normalize(const GURL& url); 56 57 // Returns true if the URL has a standard scheme. Only URLs with standard 58 // schemes are filtered. 59 // This method is public for testing. 60 static bool HasFilteredScheme(const GURL& url); 61 62 // Returns true if the |host| matches the pattern. A pattern is a hostname 63 // with one or both of the following modifications: 64 // - If the pattern starts with "*.", it matches the host or any subdomain 65 // (e.g. the pattern "*.google.com" would match google.com, www.google.com, 66 // or accounts.google.com). 67 // - If the pattern ends with ".*", it matches the host on any known TLD 68 // (e.g. the pattern "google.*" would match google.com or google.co.uk). 69 // See the ManagedModeURLFilterTest.HostMatchesPattern unit test for more 70 // examples. 71 // Asterisks in other parts of the pattern are not allowed. 72 // |host| and |pattern| are assumed to be normalized to lower-case. 73 // This method is public for testing. 74 static bool HostMatchesPattern(const std::string& host, 75 const std::string& pattern); 76 77 void GetSites(const GURL& url, 78 std::vector<ManagedModeSiteList::Site*>* sites) const; 79 80 // Returns the filtering behavior for a given URL, based on the default 81 // behavior and whether it is on a site list. 82 FilteringBehavior GetFilteringBehaviorForURL(const GURL& url) const; 83 84 // Sets the filtering behavior for pages not on a list (default is ALLOW). 85 void SetDefaultFilteringBehavior(FilteringBehavior behavior); 86 87 // Asynchronously loads the specified site lists from disk and updates the 88 // filter to recognize each site on them. 89 // Calls |continuation| when the filter has been updated. 90 void LoadWhitelists(ScopedVector<ManagedModeSiteList> site_lists); 91 92 // Set the list of matched patterns to the passed in list. 93 // This method is only used for testing. 94 void SetFromPatterns(const std::vector<std::string>& patterns); 95 96 // Sets the set of manually allowed or blocked hosts. 97 void SetManualHosts(const std::map<std::string, bool>* host_map); 98 99 // Sets the set of manually allowed or blocked URLs. 100 void SetManualURLs(const std::map<GURL, bool>* url_map); 101 102 void AddObserver(Observer* observer); 103 void RemoveObserver(Observer* observer); 104 105 private: 106 friend class base::RefCountedThreadSafe<ManagedModeURLFilter>; 107 ~ManagedModeURLFilter(); 108 109 void SetContents(scoped_ptr<Contents> url_matcher); 110 111 ObserverList<Observer> observers_; 112 113 FilteringBehavior default_behavior_; 114 scoped_ptr<Contents> contents_; 115 116 // Maps from a URL to whether it is manually allowed (true) or blocked 117 // (false). 118 std::map<GURL, bool> url_map_; 119 120 // Maps from a hostname to whether it is manually allowed (true) or blocked 121 // (false). 122 std::map<std::string, bool> host_map_; 123 124 DISALLOW_COPY_AND_ASSIGN(ManagedModeURLFilter); 125 }; 126 127 #endif // CHROME_BROWSER_MANAGED_MODE_MANAGED_MODE_URL_FILTER_H_ 128