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

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: resolve final nit Created 4 years, 1 month 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.
37 static bool IsUserGestureExpired(const base::Time& timestamp);
38 // Helper function to strip empty ref fragment from a URL. Many pages
39 // end up with a "#" at the end of their URLs due to navigation triggered by
40 // href="#" and javascript onclick function. We don't want to have separate
41 // entries for these cases in the maps.
42 static GURL ClearEmptyRef(const GURL& url);
43
44 SafeBrowsingNavigationObserverManager();
45
46 // Add |nav_event| to |navigation_map_| based on |nav_event_key|. Object
47 // pointed to by |nav_event| will be no longer accessible after this function.
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 struct GurlHash {
68 std::size_t operator()(const GURL& url) const {
69 return std::hash<std::string>()(url.spec());
70 }
71 };
72
73 typedef std::unordered_map<GURL, std::vector<NavigationEvent>, GurlHash>
74 NavigationMap;
75 typedef std::unordered_map<content::WebContents*, base::Time> UserGestureMap;
76 typedef std::unordered_map<std::string, std::vector<ResolvedIPAddress>>
77 HostToIpMap;
78
79 ~SafeBrowsingNavigationObserverManager() override;
80
81 // content::NotificationObserver:
82 void Observe(int type,
83 const content::NotificationSource& source,
84 const content::NotificationDetails& details) override;
85
86 void RecordRetargeting(const content::NotificationDetails& details);
87
88 NavigationMap* navigation_map() { return &navigation_map_; }
89
90 HostToIpMap* host_to_ip_map() { return &host_to_ip_map_; }
91
92 // navigation_map_ keeps track of all the observed navigations. This map is
93 // keyed on the resolved request url. In other words, in case of server
94 // redirects, its key is the last server redirect url, otherwise, it is the
95 // original target url. Since the same url can be requested multiple times
96 // across different tabs and frames, the value of this map is a vector of
97 // NavigationEvent ordered by navigation finish time.
98 // TODO(jialiul): Entries in navigation_map_ will be removed if they are older
99 // than 2 minutes since their corresponding navigations finish.
100 NavigationMap navigation_map_;
101
102 // user_gesture_map_ keeps track of the timestamp of last user gesture in
103 // in each WebContents. We assume for majority of cases, a navigation
104 // shortly after a user gesture indicate this navigation is user initiated.
105 UserGestureMap user_gesture_map_;
106
107 // Host to timestamped IP addresses map that covers all the main frame and
108 // subframe URLs' hosts. Since it is possible for a host to resolve to more
109 // than one IP in even a short period of time, we map a single host to a
110 // vector of ResolvedIPAddresss. This map is used to fill in ip_address field
111 // in URLChainEntry in ClientDownloadRequest.
112 HostToIpMap host_to_ip_map_;
113
114 content::NotificationRegistrar registrar_;
115
116 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingNavigationObserverManager);
117 };
118 } // namespace safe_browsing
119
120 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_NAVIGATION_OBSERVER_MANAGE R_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698