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.
5 // The Safe Browsing service is responsible for downloading anti-phishing and
6 // anti-malware tables and checking urls against them.
8 #ifndef CHROME_BROWSER_SAFE_BROWSING_UI_MANAGER_H_
9 #define CHROME_BROWSER_SAFE_BROWSING_UI_MANAGER_H_
14 #include "base/callback.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/observer_list.h"
18 #include "base/time/time.h"
19 #include "chrome/browser/safe_browsing/safe_browsing_util.h"
20 #include "content/public/browser/notification_observer.h"
23 class SafeBrowsingService;
29 // Construction needs to happen on the main thread.
30 class SafeBrowsingUIManager
31 : public base::RefCountedThreadSafe<SafeBrowsingUIManager> {
33 // Passed a boolean indicating whether or not it is OK to proceed with
35 typedef base::Callback<void(bool /*proceed*/)> UrlCheckCallback;
37 // Structure used to pass parameters between the IO and UI thread when
38 // interacting with the blocking page.
39 struct UnsafeResource {
45 std::vector<GURL> redirect_urls;
47 SBThreatType threat_type;
48 UrlCheckCallback callback;
49 int render_process_host_id;
53 // Observer class can be used to get notified when a SafeBrowsing hit
57 // The |resource| must not be accessed after OnSafeBrowsingHit returns.
58 // This method will be called on the UI thread.
59 virtual void OnSafeBrowsingHit(const UnsafeResource& resource) = 0;
63 virtual ~Observer() {}
66 DISALLOW_COPY_AND_ASSIGN(Observer);
69 explicit SafeBrowsingUIManager(
70 const scoped_refptr<SafeBrowsingService>& service);
72 // Called to stop or shutdown operations on the io_thread. This may be called
73 // multiple times during the life of the UIManager. Should be called
74 // on IO thread. If shutdown is true, the manager is disabled permanently.
75 void StopOnIOThread(bool shutdown);
77 // Called on UI thread to decide if safe browsing related stats
79 virtual bool CanReportStats() const;
81 // Called on the IO thread to display an interstitial page.
82 // |url| is the url of the resource that matches a safe browsing list.
83 // If the request contained a chain of redirects, |url| is the last url
84 // in the chain, and |original_url| is the first one (the root of the
85 // chain). Otherwise, |original_url| = |url|.
86 void DisplayBlockingPage(const GURL& url,
87 const GURL& original_url,
88 const std::vector<GURL>& redirect_urls,
90 SBThreatType threat_type,
91 const UrlCheckCallback& callback,
92 int render_process_host_id,
95 // Same as above but gets invoked on the UI thread.
96 virtual void DoDisplayBlockingPage(const UnsafeResource& resource);
98 // Returns true if we already displayed an interstitial for that resource.
99 // Called on the UI thread.
100 bool IsWhitelisted(const UnsafeResource& resource);
102 // The blocking page on the UI thread has completed.
103 void OnBlockingPageDone(const std::vector<UnsafeResource>& resources,
106 // Log the user perceived delay caused by SafeBrowsing. This delay is the time
107 // delta starting from when we would have started reading data from the
108 // network, and ending when the SafeBrowsing check completes indicating that
109 // the current page is 'safe'.
110 void LogPauseDelay(base::TimeDelta time);
112 // Called on the IO thread by the MalwareDetails with the serialized
113 // protocol buffer, so the service can send it over.
114 virtual void SendSerializedMalwareDetails(const std::string& serialized);
116 // Report hits to the unsafe contents (malware, phishing, unsafe download URL)
117 // to the server. Can only be called on UI thread. If |post_data| is
118 // non-empty, the request will be sent as a POST instead of a GET.
119 virtual void ReportSafeBrowsingHit(const GURL& malicious_url,
120 const GURL& page_url,
121 const GURL& referrer_url,
123 SBThreatType threat_type,
124 const std::string& post_data);
126 // Add and remove observers. These methods must be invoked on the UI thread.
127 void AddObserver(Observer* observer);
128 void RemoveObserver(Observer* remove);
131 virtual ~SafeBrowsingUIManager();
134 friend class base::RefCountedThreadSafe<SafeBrowsingUIManager>;
136 // Used for whitelisting a render view when the user ignores our warning.
137 struct WhiteListedEntry;
139 // Call protocol manager on IO thread to report hits of unsafe contents.
140 void ReportSafeBrowsingHitOnIOThread(const GURL& malicious_url,
141 const GURL& page_url,
142 const GURL& referrer_url,
144 SBThreatType threat_type,
145 const std::string& post_data);
147 // Adds the given entry to the whitelist. Called on the UI thread.
148 void UpdateWhitelist(const UnsafeResource& resource);
150 // Safebrowsing service.
151 scoped_refptr<SafeBrowsingService> sb_service_;
153 // Only access this whitelist from the UI thread.
154 std::vector<WhiteListedEntry> white_listed_entries_;
156 ObserverList<Observer> observer_list_;
158 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingUIManager);
161 #endif // CHROME_BROWSER_SAFE_BROWSING_UI_MANAGER_H_