blob: 638e0cd4cd12cb5f5109accae14f2fe295054862 [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.
ntfschr54471efa2017-01-12 22:16:5841 // This currently is a no-op in the base class.
ntfschra3234332016-12-22 01:15:4242 virtual void StopOnIOThread(bool shutdown);
43
44 // Called on the UI thread to display an interstitial page.
45 // |url| is the url of the resource that matches a safe browsing list.
46 // If the request contained a chain of redirects, |url| is the last url
47 // in the chain, and |original_url| is the first one (the root of the
48 // chain). Otherwise, |original_url| = |url|.
49 virtual void DisplayBlockingPage(const UnsafeResource& resource);
50
51 // Log the user perceived delay caused by SafeBrowsing. This delay is the time
52 // delta starting from when we would have started reading data from the
53 // network, and ending when the SafeBrowsing check completes indicating that
54 // the current page is 'safe'.
55 virtual void LogPauseDelay(base::TimeDelta time);
56
ntfschr54471efa2017-01-12 22:16:5857 // This is a no-op in the base class, but should be overridden to send threat
58 // details. Called on the IO thread by the ThreatDetails with the serialized
59 // protocol buffer.
ntfschra3234332016-12-22 01:15:4260 virtual void SendSerializedThreatDetails(const std::string& serialized);
61
ntfschr54471efa2017-01-12 22:16:5862 // This is a no-op in the base class, but should be overridden to report hits
63 // to the unsafe contents (malware, phishing, unsafe download URL)
64 // to the server. Can only be called on UI thread.
ntfschra3234332016-12-22 01:15:4265 virtual void MaybeReportSafeBrowsingHit(
66 const safe_browsing::HitReport& hit_report);
67
68 // A convenience wrapper method for IsUrlWhitelistedOrPendingForWebContents.
69 virtual bool IsWhitelisted(const UnsafeResource& resource);
70
71 // Checks if we already displayed or are displaying an interstitial
72 // for the top-level site |url| in a given WebContents. If
73 // |whitelist_only|, it returns true only if the user chose to ignore
74 // the interstitial. Otherwise, it returns true if an interstitial for
75 // |url| is already displaying *or* if the user has seen an
76 // interstitial for |url| before in this WebContents and proceeded
77 // through it. Called on the UI thread.
78 //
79 // If the resource was found in the whitelist or pending for the
80 // whitelist, |threat_type| will be set to the SBThreatType for which
81 // the URL was first whitelisted.
82 virtual bool IsUrlWhitelistedOrPendingForWebContents(
83 const GURL& url,
84 bool is_subresource,
85 content::NavigationEntry* entry,
86 content::WebContents* web_contents,
87 bool whitelist_only,
88 SBThreatType* threat_type);
89
90 // The blocking page for |web_contents| on the UI thread has
91 // completed, with |proceed| set to true if the user has chosen to
92 // proceed through the blocking page and false
93 // otherwise. |web_contents| is the WebContents that was displaying
94 // the blocking page. |main_frame_url| is the top-level URL on which
95 // the blocking page was displayed. If |proceed| is true,
96 // |main_frame_url| is whitelisted so that the user will not see
97 // another warning for that URL in this WebContents.
98 virtual void OnBlockingPageDone(const std::vector<UnsafeResource>& resources,
99 bool proceed,
100 content::WebContents* web_contents,
101 const GURL& main_frame_url);
102
jialiul3d6032e2017-01-12 00:41:31103 virtual const std::string app_locale() const;
104
105 virtual history::HistoryService* history_service(
106 content::WebContents* web_contents);
107
108 // The default safe page when there is no entry in the history to go back to.
109 // e.g. about::blank page, or chrome's new tab page.
110 virtual const GURL default_safe_page() const;
ntfschra3234332016-12-22 01:15:42111
112 protected:
jialiul3d6032e2017-01-12 00:41:31113 virtual ~BaseUIManager();
ntfschra3234332016-12-22 01:15:42114
115 // Updates the whitelist URL set for |web_contents|. Called on the UI thread.
116 void AddToWhitelistUrlSet(const GURL& whitelist_url,
117 content::WebContents* web_contents,
118 bool is_pending,
119 SBThreatType threat_type);
120
ntfschr54471efa2017-01-12 22:16:58121 // This is a no-op that should be overridden to call protocol manager on IO
122 // thread to report hits of unsafe contents.
ntfschra3234332016-12-22 01:15:42123 virtual void ReportSafeBrowsingHitOnIOThread(
124 const safe_browsing::HitReport& hit_report);
125
126 // Removes |whitelist_url| from the pending whitelist for
127 // |web_contents|. Called on the UI thread.
128 void RemoveFromPendingWhitelistUrlSet(const GURL& whitelist_url,
129 content::WebContents* web_contents);
130
131 // Ensures that |web_contents| has its whitelist set in its userdata
132 static void EnsureWhitelistCreated(content::WebContents* web_contents);
133
jialiul3d6032e2017-01-12 00:41:31134 // Returns the URL that should be used in a WhitelistUrlSet for the given
135 // |resource|.
136 static GURL GetMainFrameWhitelistUrlForResource(
137 const security_interstitials::UnsafeResource& resource);
ntfschra3234332016-12-22 01:15:42138
139 private:
jialiul3d6032e2017-01-12 00:41:31140 friend class base::RefCountedThreadSafe<BaseUIManager>;
ntfschra3234332016-12-22 01:15:42141
jialiul3d6032e2017-01-12 00:41:31142 DISALLOW_COPY_AND_ASSIGN(BaseUIManager);
ntfschra3234332016-12-22 01:15:42143};
144
145} // namespace safe_browsing
146
147#endif // COMPONENTS_SAFE_BROWSING_BASE_UI_MANAGER_H_