15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CHROME_BROWSER_MANAGED_MODE_MANAGED_MODE_URL_FILTER_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHROME_BROWSER_MANAGED_MODE_MANAGED_MODE_URL_FILTER_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/callback_forward.h"
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/memory/ref_counted.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_vector.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/observer_list.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/threading/non_thread_safe.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/values.h"
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/managed_mode/managed_mode_site_list.h"
162385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch#include "chrome/browser/managed_mode/managed_users.h"
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/policy/url_blacklist_manager.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace policy {
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class URLBlacklist;
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace policy
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class GURL;
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// This class manages the filtering behavior for a given URL, i.e. it tells
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// callers if a given URL should be allowed, blocked or warned about. It uses
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// information from multiple sources:
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//   * A default setting (allow, block or warn).
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//   * The set of installed and enabled content packs, which contain whitelists
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//     of URL patterns that should be allowed.
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//   * User-specified manual overrides (allow or block) for either sites
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//     (hostnames) or exact URLs, which take precedence over the previous
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//     sources.
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// References to it can be passed around on different threads (the refcounting
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// is thread-safe), but the object itself should always be accessed on the same
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// thread (member access isn't thread-safe).
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class ManagedModeURLFilter
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    : public base::RefCountedThreadSafe<ManagedModeURLFilter>,
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      public base::NonThreadSafe {
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  enum FilteringBehavior {
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ALLOW,
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    WARN,
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    BLOCK,
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    HISTOGRAM_BOUNDING_VALUE
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  class Observer {
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   public:
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    virtual void OnSiteListUpdated() = 0;
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  struct Contents;
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ManagedModeURLFilter();
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static FilteringBehavior BehaviorFromInt(int behavior_value);
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Normalizes a URL for matching purposes.
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static GURL Normalize(const GURL& url);
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
62868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Returns true if the URL has a standard scheme. Only URLs with standard
63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // schemes are filtered.
64868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // This method is public for testing.
65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  static bool HasStandardScheme(const GURL& url);
66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void GetSites(const GURL& url,
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                std::vector<ManagedModeSiteList::Site*>* sites) const;
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns the filtering behavior for a given URL, based on the default
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // behavior and whether it is on a site list.
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FilteringBehavior GetFilteringBehaviorForURL(const GURL& url) const;
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Sets the filtering behavior for pages not on a list (default is ALLOW).
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetDefaultFilteringBehavior(FilteringBehavior behavior);
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Asynchronously loads the specified site lists from disk and updates the
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // filter to recognize each site on them.
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Calls |continuation| when the filter has been updated.
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void LoadWhitelists(ScopedVector<ManagedModeSiteList> site_lists);
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Set the list of matched patterns to the passed in list.
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // This method is only used for testing.
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetFromPatterns(const std::vector<std::string>& patterns);
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Sets the set of manually allowed or blocked hosts.
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetManualHosts(const std::map<std::string, bool>* host_map);
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Sets the set of manually allowed or blocked URLs.
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetManualURLs(const std::map<GURL, bool>* url_map);
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void AddObserver(Observer* observer);
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void RemoveObserver(Observer* observer);
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  friend class base::RefCountedThreadSafe<ManagedModeURLFilter>;
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ~ManagedModeURLFilter();
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetContents(scoped_ptr<Contents> url_matcher);
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ObserverList<Observer> observers_;
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FilteringBehavior default_behavior_;
1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<Contents> contents_;
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Maps from a URL to whether it is manually allowed (true) or blocked
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // (false).
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::map<GURL, bool> url_map_;
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Maps from a hostname to whether it is manually allowed (true) or blocked
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // (false).
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::map<std::string, bool> host_map_;
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(ManagedModeURLFilter);
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CHROME_BROWSER_MANAGED_MODE_MANAGED_MODE_URL_FILTER_H_
118