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

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

Issue 2538483002: Add management related code to SafeBrowsingNavigationObserverManager (Closed)
Patch Set: filter out browser side navigation browser tests Created 4 years 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_NAVIGATION_OBSERVER_MANAGER_H _ 5 #ifndef CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_NAVIGATION_OBSERVER_MANAGER_H _
6 #define CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_NAVIGATION_OBSERVER_MANAGER_H _ 6 #define CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_NAVIGATION_OBSERVER_MANAGER_H _
7 7
8 #include "chrome/common/safe_browsing/csd.pb.h"
8 #include "content/public/browser/notification_observer.h" 9 #include "content/public/browser/notification_observer.h"
9 #include "content/public/browser/notification_registrar.h" 10 #include "content/public/browser/notification_registrar.h"
10 #include "content/public/browser/web_contents_observer.h" 11 #include "content/public/browser/web_contents_observer.h"
11 #include "url/gurl.h" 12 #include "url/gurl.h"
12 13
13 namespace safe_browsing { 14 namespace safe_browsing {
14 15
15 class SafeBrowsingNavigationObserver; 16 class SafeBrowsingNavigationObserver;
16 struct NavigationEvent; 17 struct NavigationEvent;
17 struct ResolvedIPAddress; 18 struct ResolvedIPAddress;
18 19
19 // Manager class for SafeBrowsingNavigationObserver, which is in charge of 20 // Manager class for SafeBrowsingNavigationObserver, which is in charge of
20 // cleaning up stale navigation events, and identifing landing page/landing 21 // cleaning up stale navigation events, and identifying landing page/landing
21 // referrer for a specific download. 22 // referrer for a specific download.
22 // TODO(jialiul): For now, SafeBrowsingNavigationObserverManager also listens to 23 // TODO(jialiul): For now, SafeBrowsingNavigationObserverManager also listens to
23 // NOTIFICATION_RETARGETING as a way to detect cross frame/tab navigation. 24 // NOTIFICATION_RETARGETING as a way to detect cross frame/tab navigation.
24 // Remove base class content::NotificationObserver when 25 // Remove base class content::NotificationObserver when
25 // WebContentsObserver::DidOpenRequestedURL() covers all retargeting cases. 26 // WebContentsObserver::DidOpenRequestedURL() covers all retargeting cases.
26 class SafeBrowsingNavigationObserverManager 27 class SafeBrowsingNavigationObserverManager
27 : public content::NotificationObserver, 28 : public content::NotificationObserver,
28 public base::RefCountedThreadSafe<SafeBrowsingNavigationObserverManager> { 29 public base::RefCountedThreadSafe<SafeBrowsingNavigationObserverManager> {
29 public: 30 public:
31 // For UMA histogram counting. Do NOT change order.
32 enum AttributionResult {
Nathan Parker 2016/12/05 22:21:24 This can be private.
Jialiu Lin 2016/12/06 23:10:06 Since I'm going to make AttributionResult Identify
33 SUCCESS, // Identified referrer chain is not empty.
Nathan Parker 2016/12/05 22:21:24 Assign numbers to these so you can verify they mat
Jialiu Lin 2016/12/06 23:10:06 Done.
34 SUCCESS_LANDING_PAGE, // Successfully identified landing page.
35 SUCCESS_LANDING_REFERRER, // Successfully identified landing referrer.
36 INVALID_URL,
37 NAVIGATION_EVENT_NOT_FOUND,
38
39 // Always at the end
40 ATTRIBUTION_FAILURE_TYPE_MAX
41 };
30 // Helper function to check if user gesture is older than 42 // Helper function to check if user gesture is older than
31 // kUserGestureTTLInSecond. 43 // kUserGestureTTLInSecond.
32 static bool IsUserGestureExpired(const base::Time& timestamp); 44 static bool IsUserGestureExpired(const base::Time& timestamp);
33 // Helper function to strip empty ref fragment from a URL. Many pages 45 // Helper function to strip empty ref fragment from a URL. Many pages
34 // end up with a "#" at the end of their URLs due to navigation triggered by 46 // end up with a "#" at the end of their URLs due to navigation triggered by
35 // href="#" and javascript onclick function. We don't want to have separate 47 // href="#" and javascript onclick function. We don't want to have separate
36 // entries for these cases in the maps. 48 // entries for these cases in the maps.
37 static GURL ClearEmptyRef(const GURL& url); 49 static GURL ClearEmptyRef(const GURL& url);
38 50
39 SafeBrowsingNavigationObserverManager(); 51 SafeBrowsingNavigationObserverManager();
40 52
41 // Add |nav_event| to |navigation_map_| based on |nav_event_key|. Object 53 // Add |nav_event| to |navigation_map_| based on |nav_event_key|. Object
42 // pointed to by |nav_event| will be no longer accessible after this function. 54 // pointed to by |nav_event| will be no longer accessible after this function.
43 void RecordNavigationEvent(const GURL& nav_event_key, 55 void RecordNavigationEvent(const GURL& nav_event_key,
44 NavigationEvent* nav_event); 56 NavigationEvent* nav_event);
45 void RecordUserGestureForWebContents(content::WebContents* web_contents, 57 void RecordUserGestureForWebContents(content::WebContents* web_contents,
46 const base::Time& timestamp); 58 const base::Time& timestamp);
47 void OnUserGestureConsumed(content::WebContents* web_contents, 59 void OnUserGestureConsumed(content::WebContents* web_contents,
48 const base::Time& timestamp); 60 const base::Time& timestamp);
49 void RecordHostToIpMapping(const std::string& host, const std::string& ip); 61 void RecordHostToIpMapping(const std::string& host, const std::string& ip);
50 // Clean-ups need to be done when a WebContents gets destroyed. 62 // Clean-ups need to be done when a WebContents gets destroyed.
51 void OnWebContentDestroyed(content::WebContents* web_contents); 63 void OnWebContentDestroyed(content::WebContents* web_contents);
52 64 // Remove all the observed NavigationEvents, user gestures, and resolved IP
53 // TODO(jialiul): more functions are coming for managing navigation_map_. 65 // addresses that are older than kNavigationFootPrintTTLInSecond.
66 void CleanUpStaleNavigationFootPrints();
Nathan Parker 2016/12/05 22:21:24 nit: I'd say Footprint (lowercase P) since it's on
Jialiu Lin 2016/12/06 23:10:06 Oops. my spelling... Thanks for catching this.
67 // Identify and add referrer chain to ClientDownloadRequest proto.
68 void AddReferrerChainToClientDownloadRequest(
69 const GURL& download_url,
70 content::WebContents* source_contents,
71 ClientDownloadRequest* request);
54 72
55 private: 73 private:
56 friend class base::RefCountedThreadSafe< 74 friend class base::RefCountedThreadSafe<
57 SafeBrowsingNavigationObserverManager>; 75 SafeBrowsingNavigationObserverManager>;
58 friend class TestNavigationObserverManager; 76 friend class TestNavigationObserverManager;
59 friend class SBNavigationObserverBrowserTest; 77 friend class SBNavigationObserverBrowserTest;
60 friend class SBNavigationObserverTest; 78 friend class SBNavigationObserverTest;
79 FRIEND_TEST_ALL_PREFIXES(SBNavigationObserverTest,
80 TestCleanUpStaleNavigationEvents);
81 FRIEND_TEST_ALL_PREFIXES(SBNavigationObserverTest,
82 TestCleanUpStaleUserGestures);
83 FRIEND_TEST_ALL_PREFIXES(SBNavigationObserverTest,
84 TestCleanUpStaleIPAddresses);
61 85
62 struct GurlHash { 86 struct GurlHash {
63 std::size_t operator()(const GURL& url) const { 87 std::size_t operator()(const GURL& url) const {
64 return std::hash<std::string>()(url.spec()); 88 return std::hash<std::string>()(url.spec());
65 } 89 }
66 }; 90 };
67 91
68 typedef std::unordered_map<GURL, std::vector<NavigationEvent>, GurlHash> 92 typedef std::unordered_map<GURL, std::vector<NavigationEvent>, GurlHash>
69 NavigationMap; 93 NavigationMap;
70 typedef std::unordered_map<content::WebContents*, base::Time> UserGestureMap; 94 typedef std::unordered_map<content::WebContents*, base::Time> UserGestureMap;
71 typedef std::unordered_map<std::string, std::vector<ResolvedIPAddress>> 95 typedef std::unordered_map<std::string, std::vector<ResolvedIPAddress>>
72 HostToIpMap; 96 HostToIpMap;
73 97
74 ~SafeBrowsingNavigationObserverManager() override; 98 ~SafeBrowsingNavigationObserverManager() override;
75 99
76 // content::NotificationObserver: 100 // content::NotificationObserver:
77 void Observe(int type, 101 void Observe(int type,
78 const content::NotificationSource& source, 102 const content::NotificationSource& source,
79 const content::NotificationDetails& details) override; 103 const content::NotificationDetails& details) override;
80 104
81 void RecordRetargeting(const content::NotificationDetails& details); 105 void RecordRetargeting(const content::NotificationDetails& details);
82 106
83 NavigationMap* navigation_map() { return &navigation_map_; } 107 NavigationMap* navigation_map() { return &navigation_map_; }
84 108
85 HostToIpMap* host_to_ip_map() { return &host_to_ip_map_; } 109 HostToIpMap* host_to_ip_map() { return &host_to_ip_map_; }
86 110
111 void CleanUpNavigationEvents();
Nathan Parker 2016/12/05 22:21:24 Add some comments as to what these do.
Jialiu Lin 2016/12/06 23:10:06 Done.
112
113 void CleanUpUserGestures();
114
115 void CleanUpIpAddresses();
116
117 bool IsCleanUpScheduled() const;
118
119 void ScheduleNextCleanUpAfterInterval(base::TimeDelta interval);
120
121 NavigationEvent* FindNavigationEvent(const GURL& target_url,
122 int target_tab_id);
123 void AddToReferrerChain(
Nathan Parker 2016/12/05 22:21:24 An idea: Would it be cleaner to have the code that
Jialiu Lin 2016/12/06 23:10:06 I'm thinking about move ReferrerChainEntry message
Nathan Parker 2016/12/06 23:31:32 SGTM
124 std::vector<safe_browsing::ClientDownloadRequest::ReferrerChainEntry>*
125 referrer_chain,
126 NavigationEvent* nav_event,
127 ClientDownloadRequest::ReferrerChainEntry::URLType type);
128
129 AttributionResult IdentifyReferrerChain(
130 const GURL& target_url,
131 int target_tab_id, // -1 if tab id is not valid
132 int user_gesture_count_max,
133 std::vector<ClientDownloadRequest::ReferrerChainEntry>* referrer_chain);
134
87 // navigation_map_ keeps track of all the observed navigations. This map is 135 // navigation_map_ keeps track of all the observed navigations. This map is
88 // keyed on the resolved request url. In other words, in case of server 136 // keyed on the resolved request url. In other words, in case of server
89 // redirects, its key is the last server redirect url, otherwise, it is the 137 // redirects, its key is the last server redirect url, otherwise, it is the
90 // original target url. Since the same url can be requested multiple times 138 // original target url. Since the same url can be requested multiple times
91 // across different tabs and frames, the value of this map is a vector of 139 // across different tabs and frames, the value of this map is a vector of
92 // NavigationEvent ordered by navigation finish time. 140 // NavigationEvent ordered by navigation finish time.
93 // TODO(jialiul): Entries in navigation_map_ will be removed if they are older 141 // TODO(jialiul): Entries in navigation_map_ will be removed if they are older
94 // than 2 minutes since their corresponding navigations finish. 142 // than 2 minutes since their corresponding navigations finish.
95 NavigationMap navigation_map_; 143 NavigationMap navigation_map_;
96 144
97 // user_gesture_map_ keeps track of the timestamp of last user gesture in 145 // user_gesture_map_ keeps track of the timestamp of last user gesture in
98 // in each WebContents. We assume for majority of cases, a navigation 146 // in each WebContents. We assume for majority of cases, a navigation
99 // shortly after a user gesture indicate this navigation is user initiated. 147 // shortly after a user gesture indicate this navigation is user initiated.
100 UserGestureMap user_gesture_map_; 148 UserGestureMap user_gesture_map_;
101 149
102 // Host to timestamped IP addresses map that covers all the main frame and 150 // Host to timestamped IP addresses map that covers all the main frame and
103 // subframe URLs' hosts. Since it is possible for a host to resolve to more 151 // subframe URLs' hosts. Since it is possible for a host to resolve to more
104 // than one IP in even a short period of time, we map a single host to a 152 // than one IP in even a short period of time, we map a single host to a
105 // vector of ResolvedIPAddresss. This map is used to fill in ip_address field 153 // vector of ResolvedIPAddresss. This map is used to fill in ip_address field
106 // in URLChainEntry in ClientDownloadRequest. 154 // in URLChainEntry in ClientDownloadRequest.
107 HostToIpMap host_to_ip_map_; 155 HostToIpMap host_to_ip_map_;
108 156
109 content::NotificationRegistrar registrar_; 157 content::NotificationRegistrar registrar_;
110 158
159 base::OneShotTimer cleanup_timer_;
160
111 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingNavigationObserverManager); 161 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingNavigationObserverManager);
112 }; 162 };
113 } // namespace safe_browsing 163 } // namespace safe_browsing
114 164
115 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_NAVIGATION_OBSERVER_MANAGE R_H_ 165 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_NAVIGATION_OBSERVER_MANAGE R_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698