blob: 57b325179f3d81c8497463a7f32ffb78c22afaa5 [file] [log] [blame]
ntfschra3234332016-12-22 01:15:421// Copyright 2016 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 COMPONENTS_SAFE_BROWSING_BASE_UI_MANAGER_H_
6#define COMPONENTS_SAFE_BROWSING_BASE_UI_MANAGER_H_
7
8#include <string>
9#include <vector>
10
11#include "base/bind_helpers.h"
ntfschra3234332016-12-22 01:15:4212#include "base/macros.h"
13#include "base/memory/ref_counted.h"
ntfschra3234332016-12-22 01:15:4214#include "base/time/time.h"
15#include "components/security_interstitials/content/unsafe_resource.h"
16
17class GURL;
18
19namespace content {
20class NavigationEntry;
21class WebContents;
22} // namespace content
23
jialiul3d6032e2017-01-12 00:41:3124namespace history {
25class HistoryService;
26} // namespace history
27
ntfschra3234332016-12-22 01:15:4228namespace safe_browsing {
29
30// Construction needs to happen on the main thread.
jialiul3d6032e2017-01-12 00:41:3131class BaseUIManager
32 : public base::RefCountedThreadSafe<BaseUIManager> {
ntfschra3234332016-12-22 01:15:4233 public:
34 typedef security_interstitials::UnsafeResource UnsafeResource;
35
jialiul3d6032e2017-01-12 00:41:3136 BaseUIManager();
ntfschra3234332016-12-22 01:15:4237
38 // Called to stop or shutdown operations on the io_thread. This may be called
39 // multiple times during the life of the UIManager. Should be called
40 // on IO thread. If shutdown is true, the manager is disabled permanently.
41 virtual void StopOnIOThread(bool shutdown);
42
43 // Called on the UI thread to display an interstitial page.
44 // |url| is the url of the resource that matches a safe browsing list.
45 // If the request contained a chain of redirects, |url| is the last url
46 // in the chain, and |original_url| is the first one (the root of the
47 // chain). Otherwise, |original_url| = |url|.
48 virtual void DisplayBlockingPage(const UnsafeResource& resource);
49
50 // Log the user perceived delay caused by SafeBrowsing. This delay is the time
51 // delta starting from when we would have started reading data from the
52 // network, and ending when the SafeBrowsing check completes indicating that
53 // the current page is 'safe'.
54 virtual void LogPauseDelay(base::TimeDelta time);
55
56 // Called on the IO thread by the ThreatDetails with the serialized
57 // protocol buffer, so the service can send it over.
58 virtual void SendSerializedThreatDetails(const std::string& serialized);
59
60 // Report hits to the unsafe contents (malware, phishing, unsafe download URL)
61 // to the server. Can only be called on UI thread. If |post_data| is
62 // non-empty, the request will be sent as a POST instead of a GET.
63 // Will report only for UMA || is_extended_reporting.
64 virtual void MaybeReportSafeBrowsingHit(
65 const safe_browsing::HitReport& hit_report);
66
67 // A convenience wrapper method for IsUrlWhitelistedOrPendingForWebContents.
68 virtual bool IsWhitelisted(const UnsafeResource& resource);
69
70 // Checks if we already displayed or are displaying an interstitial
71 // for the top-level site |url| in a given WebContents. If
72 // |whitelist_only|, it returns true only if the user chose to ignore
73 // the interstitial. Otherwise, it returns true if an interstitial for
74 // |url| is already displaying *or* if the user has seen an
75 // interstitial for |url| before in this WebContents and proceeded
76 // through it. Called on the UI thread.
77 //
78 // If the resource was found in the whitelist or pending for the
79 // whitelist, |threat_type| will be set to the SBThreatType for which
80 // the URL was first whitelisted.
81 virtual bool IsUrlWhitelistedOrPendingForWebContents(
82 const GURL& url,
83 bool is_subresource,
84 content::NavigationEntry* entry,
85 content::WebContents* web_contents,
86 bool whitelist_only,
87 SBThreatType* threat_type);
88
89 // The blocking page for |web_contents| on the UI thread has
90 // completed, with |proceed| set to true if the user has chosen to
91 // proceed through the blocking page and false
92 // otherwise. |web_contents| is the WebContents that was displaying
93 // the blocking page. |main_frame_url| is the top-level URL on which
94 // the blocking page was displayed. If |proceed| is true,
95 // |main_frame_url| is whitelisted so that the user will not see
96 // another warning for that URL in this WebContents.
97 virtual void OnBlockingPageDone(const std::vector<UnsafeResource>& resources,
98 bool proceed,
99 content::WebContents* web_contents,
100 const GURL& main_frame_url);
101
jialiul3d6032e2017-01-12 00:41:31102 virtual const std::string app_locale() const;
103
104 virtual history::HistoryService* history_service(
105 content::WebContents* web_contents);
106
107 // The default safe page when there is no entry in the history to go back to.
108 // e.g. about::blank page, or chrome's new tab page.
109 virtual const GURL default_safe_page() const;
ntfschra3234332016-12-22 01:15:42110
111 protected:
jialiul3d6032e2017-01-12 00:41:31112 virtual ~BaseUIManager();
ntfschra3234332016-12-22 01:15:42113
114 // Updates the whitelist URL set for |web_contents|. Called on the UI thread.
115 void AddToWhitelistUrlSet(const GURL& whitelist_url,
116 content::WebContents* web_contents,
117 bool is_pending,
118 SBThreatType threat_type);
119
120 // Call protocol manager on IO thread to report hits of unsafe contents.
121 virtual void ReportSafeBrowsingHitOnIOThread(
122 const safe_browsing::HitReport& hit_report);
123
124 // Removes |whitelist_url| from the pending whitelist for
125 // |web_contents|. Called on the UI thread.
126 void RemoveFromPendingWhitelistUrlSet(const GURL& whitelist_url,
127 content::WebContents* web_contents);
128
129 // Ensures that |web_contents| has its whitelist set in its userdata
130 static void EnsureWhitelistCreated(content::WebContents* web_contents);
131
jialiul3d6032e2017-01-12 00:41:31132 // Returns the URL that should be used in a WhitelistUrlSet for the given
133 // |resource|.
134 static GURL GetMainFrameWhitelistUrlForResource(
135 const security_interstitials::UnsafeResource& resource);
ntfschra3234332016-12-22 01:15:42136
137 private:
jialiul3d6032e2017-01-12 00:41:31138 friend class base::RefCountedThreadSafe<BaseUIManager>;
ntfschra3234332016-12-22 01:15:42139
jialiul3d6032e2017-01-12 00:41:31140 DISALLOW_COPY_AND_ASSIGN(BaseUIManager);
ntfschra3234332016-12-22 01:15:42141};
142
143} // namespace safe_browsing
144
145#endif // COMPONENTS_SAFE_BROWSING_BASE_UI_MANAGER_H_