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

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: make all bots happy 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 is in charge of
25 // cleaning up stale navigation events, and identifing landing page/landing
26 // referrer for 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 // Helper function to check if user gesture is older than
36 // kUserGestureTTLInSecond;
Charlie Reis 2016/10/25 04:52:27 nit: Period, not semicolon.
Jialiu Lin 2016/10/27 02:15:21 Done.
37 static bool IsUserGestureExpired(const base::Time& timestamp);
38 // Helper function to strip empty ref fragment from a URL.
Charlie Reis 2016/10/25 04:52:27 Let's add a justification for why this is useful.
Jialiu Lin 2016/10/27 02:15:20 Done.
39 static GURL ClearEmptyRef(const GURL& url);
40
41 SafeBrowsingNavigationObserverManager();
42
43 // Add |nav_event| to |navigation_map_| based on |nav_event_key|. Object
44 // pointed by |nav_event| will be no longer accessible after this function.
Charlie Reis 2016/10/25 04:52:27 nit: pointed to by
Jialiu Lin 2016/10/27 02:15:21 Done.
45 void RecordNavigationEvent(const GURL& nav_event_key,
46 NavigationEvent* nav_event);
47 void RecordUserGestureForWebContents(content::WebContents* web_contents,
48 const base::Time& timestamp);
49 void OnUserGestureConsumed(content::WebContents* web_contents,
50 const base::Time& timestamp);
51 void RecordHostToIpMapping(const std::string& host, const std::string& ip);
52 // Clean-ups need to be done when a WebContents gets destroyed.
53 void OnWebContentDestroyed(content::WebContents* web_contents);
54
55 // TODO(jialiul): more functions are coming for managing navigation_map_.
56
57 private:
58 friend class base::RefCountedThreadSafe<
59 SafeBrowsingNavigationObserverManager>;
60 friend class TestNavigationObserverManager;
61 friend class SBNavigationObserverBrowserTest;
62 friend class SBNavigationObserverTest;
63
64 struct GurlHash {
65 std::size_t operator()(const GURL& url) const {
66 return std::hash<std::string>()(url.spec());
67 }
68 };
69
70 typedef std::unordered_map<GURL, std::vector<NavigationEvent>, GurlHash>
71 NavigationMap;
72 typedef std::unordered_map<content::WebContents*, base::Time> UserGestureMap;
73 typedef std::unordered_map<std::string, std::vector<ResolvedIPAddress>>
74 HostToIpMap;
75
76 ~SafeBrowsingNavigationObserverManager() override;
77
78 // content::NotificationObserver:
79 void Observe(int type,
80 const content::NotificationSource& source,
81 const content::NotificationDetails& details) override;
82
83 void RecordRetargeting(const content::NotificationDetails& details);
84
85 NavigationMap* navigation_map() { return &navigation_map_; }
86
87 HostToIpMap* host_to_ip_map() { return &host_to_ip_map_; }
88
89 // navigation_map_ keeps track of all the observed navigations. This map is
90 // keyed on the resolved request url. In other words, in case of server
91 // redirects, its key is the last server redirect url, otherwise, it is the
92 // original target url. Since the same url can be requested multiple times
93 // across different tabs and frames, the value of this map is a vector of
94 // NavigationEvent ordered by navigation finish time.
95 // TODO(jialiul): Entries in navigation_map_ will be removed if they are older
96 // than 2 minutes since their corresponding navigations finish.
97 NavigationMap navigation_map_;
98
99 // user_gesture_map_ keeps track of the timestamp of last user gesture in
100 // in each WebContents. We assume for majority of cases, a navigation
101 // shortly after a user gesture indicate this navigation is user initiated.
102 UserGestureMap user_gesture_map_;
103
104 // Host to timestamped IP addresses map that covers all the main frame and
105 // sub frame URLs' hosts. Since it is possible for a host to resolve to more
Charlie Reis 2016/10/25 04:52:27 nit: subframe
Jialiu Lin 2016/10/27 02:15:21 Done.
106 // than one IP in even a short period of time, we map a single host to a
107 // vector of ResolvedIPAddresss. This map is used to fill in ip_address field
108 // in URLChainEntry in ClientDownloadRequest.
109 HostToIpMap host_to_ip_map_;
110
111 content::NotificationRegistrar registrar_;
112
113 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingNavigationObserverManager);
114 };
115 } // namespace safe_browsing
116
117 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_NAVIGATION_OBSERVER_MANAGE R_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698