Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(716)

Side by Side Diff: chrome/browser/safe_browsing/safe_browsing_navigation_observer_manager.h

Issue 2302913003: Add SafeBrowsingNavigationObserver to listen to navigation events (Closed)
Patch Set: nit Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // 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 CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_NAVIGATION_OBSERVER_MANAGER_H _
6 #define CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_NAVIGATION_OBSERVER_MANAGER_H _
7
8 #include "content/public/browser/notification_observer.h"
9 #include "content/public/browser/notification_registrar.h"
10 #include "content/public/browser/web_contents_observer.h"
11 #include "url/gurl.h"
12
13 namespace content {
14 class NavigationHandle;
15 struct ResourceRedirectDetails;
16 }
17
18 namespace safe_browsing {
19
20 class SafeBrowsingNavigationObserver;
21 struct NavigationEvent;
22 struct ResolvedIPAddress;
23
24 // Manager class for SafeBrowsingNavigationObserver, which in charge of cleaning
Nathan Parker 2016/10/14 23:52:49 nit: which is in charge
Jialiu Lin 2016/10/17 19:17:15 Done.
25 // up stale navigation events, and identifing landing page/landing referrer for
26 // a specific download.
27 // TODO(jialiul): For now, SafeBrowsingNavigationObserverManager also listens to
28 // NOTIFICATION_RETARGETING as a way to detect cross frame/tab navigation.
29 // Remove base class content::NotificationObserver when
30 // WebContentsObserver::DidOpenRequestedURL() covers all retargeting cases.
31 class SafeBrowsingNavigationObserverManager
32 : public content::NotificationObserver,
33 public base::RefCountedThreadSafe<SafeBrowsingNavigationObserverManager> {
34 public:
35 struct GurlHash {
Nathan Parker 2016/10/14 23:52:49 This could be private
Jialiu Lin 2016/10/17 19:17:15 Yes. Moved to private.
36 std::size_t operator()(const GURL& url) const {
37 return std::hash<std::string>()(url.spec());
38 }
39 };
40
41 // Helper function to check if user gesture is older than
42 // kUserGestureTTLInSecond;
43 static bool IsUserGestureExpired(const base::Time& timestamp);
44 // Helper function to strip empty ref fragment from a URL.
45 static GURL ClearEmptyURLRef(const GURL& url);
46
47 SafeBrowsingNavigationObserverManager();
48 void RecordNavigationEvent(const GURL& nav_event_key,
49 NavigationEvent* nav_event);
50 void RecordUserGestureForWebContents(content::WebContents* web_contents,
51 const base::Time& timestamp);
52 void OnUserGestureConsumed(content::WebContents* web_contents,
53 const base::Time& timestamp);
54 void RecordHostToIpMapping(const std::string& host, const std::string& ip);
55 // Clean-ups need to be done when a WebContents gets destroyed.
56 void OnWebContentDestroyed(content::WebContents* web_contents);
57
58 // TODO(jialiul): more functions are coming for managing navigation_map_.
59
60 private:
61 friend class base::RefCountedThreadSafe<
62 SafeBrowsingNavigationObserverManager>;
63 friend class TestNavigationObserverManager;
64 friend class SBNavigationObserverBrowserTest;
65 friend class SBNavigationObserverTest;
66
67 typedef std::unordered_map<GURL, std::vector<NavigationEvent>, GurlHash>
68 NavigationMap;
69 typedef std::unordered_map<content::WebContents*, base::Time> UserGestureMap;
70 typedef std::unordered_map<std::string, std::vector<ResolvedIPAddress>>
71 HostToIpMap;
72
73 ~SafeBrowsingNavigationObserverManager() override;
74
75 // content::NotificationObserver:
76 void Observe(int type,
77 const content::NotificationSource& source,
78 const content::NotificationDetails& details) override;
79
80 void RecordRetargeting(const content::NotificationDetails& details);
81
82 NavigationMap* navigation_map() { return &navigation_map_; }
83
84 HostToIpMap* host_to_ip_map() { return &host_to_ip_map_; }
85
86 // navigation_map_ keeps track of all the observed navigations. This map is
87 // keyed on the resolved request url. In other words, in case of server
88 // redirects, its key is the last server redirect url, otherwise, it is the
89 // original target url. Since the same url can be requested multiple times
90 // across different tabs and frames, the value of this map is a vector of
91 // NavigationEvent ordered by navigation finish time.
92 // TODO(jialiul): Entries in navigation_map_ will be removed if they are older
93 // than 2 minutes since their corresponding navigations finish.
94 NavigationMap navigation_map_;
95
96 // user_gesture_map_ keeps track of the timestamp of last user gesture in
97 // in each WebContents. We assume for majority of cases, a navigation
98 // shortly after a user gesture indicate this navigation is user initiated.
99 UserGestureMap user_gesture_map_;
100
101 // Host to timestamped IP addresses map that covers all the main frame and
102 // sub frame URLs' hosts. Since it is possible for a host to resolve to more
103 // than one IP in even a short period of time, we map a single host to a
104 // vector of ResolvedIPAddresss. This map is used to fill in ip_address field
105 // in URLChainEntry in ClientDownlodRequest.
106 HostToIpMap host_to_ip_map_;
107
108 content::NotificationRegistrar registrar_;
109 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingNavigationObserverManager);
110 };
111 } // namespace safe_browsing
112
113 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_NAVIGATION_OBSERVER_MANAGE R_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698