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"
21 #include "content/public/browser/web_contents_observer.h"
24 class SafeBrowsingService
;
34 // Construction needs to happen on the main thread.
35 class SafeBrowsingUIManager
36 : public base::RefCountedThreadSafe
<SafeBrowsingUIManager
> {
38 // Passed a boolean indicating whether or not it is OK to proceed with
40 typedef base::Callback
<void(bool /*proceed*/)> UrlCheckCallback
;
42 // What service classified this threat as unsafe.
45 FROM_DATA_SAVER
, // From the Data Reduction service.
46 FROM_DEVICE
, // From {Local,Remote}SafeBrowingDatabaseManager.
49 // Structure used to pass parameters between the IO and UI thread when
50 // interacting with the blocking page.
51 struct UnsafeResource
{
57 std::vector
<GURL
> redirect_urls
;
60 SBThreatType threat_type
;
61 std::string threat_metadata
;
62 UrlCheckCallback callback
; // This is called back on the IO thread.
63 int render_process_host_id
;
65 ThreatSource threat_source
;
68 // Observer class can be used to get notified when a SafeBrowsing hit
72 // The |resource| was classified as unsafe by SafeBrowsing.
73 // This method will be called every time an unsafe resource is
74 // loaded, even if it has already been whitelisted by the user.
75 // The |resource| must not be accessed after OnSafeBrowsingHit returns.
76 // This method will be called on the UI thread.
77 virtual void OnSafeBrowsingMatch(const UnsafeResource
& resource
) = 0;
79 // The |resource| was classified as unsafe by SafeBrowsing, and is
81 // The |resource| must not be accessed after OnSafeBrowsingHit returns.
82 // This method will be called on the UI thread.
83 virtual void OnSafeBrowsingHit(const UnsafeResource
& resource
) = 0;
87 virtual ~Observer() {}
90 DISALLOW_COPY_AND_ASSIGN(Observer
);
93 explicit SafeBrowsingUIManager(
94 const scoped_refptr
<SafeBrowsingService
>& service
);
96 // Called to stop or shutdown operations on the io_thread. This may be called
97 // multiple times during the life of the UIManager. Should be called
98 // on IO thread. If shutdown is true, the manager is disabled permanently.
99 void StopOnIOThread(bool shutdown
);
101 // Called on UI thread to decide if safe browsing related stats
102 // could be reported.
103 virtual bool CanReportStats() const;
105 // Called on the UI thread to display an interstitial page.
106 // |url| is the url of the resource that matches a safe browsing list.
107 // If the request contained a chain of redirects, |url| is the last url
108 // in the chain, and |original_url| is the first one (the root of the
109 // chain). Otherwise, |original_url| = |url|.
110 virtual void DisplayBlockingPage(const UnsafeResource
& resource
);
112 // Returns true if we already displayed an interstitial for that top-level
113 // site in a given WebContents. Called on the UI thread.
114 bool IsWhitelisted(const UnsafeResource
& resource
);
116 // The blocking page on the UI thread has completed.
117 void OnBlockingPageDone(const std::vector
<UnsafeResource
>& resources
,
120 // Log the user perceived delay caused by SafeBrowsing. This delay is the time
121 // delta starting from when we would have started reading data from the
122 // network, and ending when the SafeBrowsing check completes indicating that
123 // the current page is 'safe'.
124 void LogPauseDelay(base::TimeDelta time
);
126 // Called on the IO thread by the MalwareDetails with the serialized
127 // protocol buffer, so the service can send it over.
128 virtual void SendSerializedMalwareDetails(const std::string
& serialized
);
130 // Report hits to the unsafe contents (malware, phishing, unsafe download URL)
131 // to the server. Can only be called on UI thread. If |post_data| is
132 // non-empty, the request will be sent as a POST instead of a GET.
133 virtual void ReportSafeBrowsingHit(const GURL
& malicious_url
,
134 const GURL
& page_url
,
135 const GURL
& referrer_url
,
137 SBThreatType threat_type
,
138 const std::string
& post_data
,
139 bool is_extended_reporting
);
141 // Report an invalid TLS/SSL certificate chain to the server. Can only
142 // be called on UI thread.
143 void ReportInvalidCertificateChain(const std::string
& serialized_report
,
144 const base::Closure
& callback
);
146 // Add and remove observers. These methods must be invoked on the UI thread.
147 void AddObserver(Observer
* observer
);
148 void RemoveObserver(Observer
* remove
);
151 virtual ~SafeBrowsingUIManager();
154 friend class base::RefCountedThreadSafe
<SafeBrowsingUIManager
>;
155 friend class SafeBrowsingUIManagerTest
;
157 // Call protocol manager on IO thread to report hits of unsafe contents.
158 void ReportSafeBrowsingHitOnIOThread(const GURL
& malicious_url
,
159 const GURL
& page_url
,
160 const GURL
& referrer_url
,
162 SBThreatType threat_type
,
163 const std::string
& post_data
,
164 bool is_extended_reporting
);
166 // Sends an invalid certificate chain report over the network.
167 void ReportInvalidCertificateChainOnIOThread(
168 const std::string
& serialized_report
);
170 // Updates the whitelist state. Called on the UI thread.
171 void AddToWhitelist(const UnsafeResource
& resource
);
173 // Safebrowsing service.
174 scoped_refptr
<SafeBrowsingService
> sb_service_
;
176 base::ObserverList
<Observer
> observer_list_
;
178 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingUIManager
);
181 #endif // CHROME_BROWSER_SAFE_BROWSING_UI_MANAGER_H_