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

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: address comments from creis@ and holte@ + rebase 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 {
33 SUCCESS = 1, // Identified referrer chain is not empty.
34 SUCCESS_LANDING_PAGE = 2, // Successfully identified landing page.
35 SUCCESS_LANDING_REFERRER = 3, // Successfully identified landing referrer.
36 INVALID_URL = 4,
37 NAVIGATION_EVENT_NOT_FOUND = 5,
38
39 // Always at the end.
40 ATTRIBUTION_FAILURE_TYPE_MAX
41 };
42
30 // Helper function to check if user gesture is older than 43 // Helper function to check if user gesture is older than
31 // kUserGestureTTLInSecond. 44 // kUserGestureTTLInSecond.
32 static bool IsUserGestureExpired(const base::Time& timestamp); 45 static bool IsUserGestureExpired(const base::Time& timestamp);
33 // Helper function to strip empty ref fragment from a URL. Many pages 46 // 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 47 // 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 48 // href="#" and javascript onclick function. We don't want to have separate
36 // entries for these cases in the maps. 49 // entries for these cases in the maps.
37 static GURL ClearEmptyRef(const GURL& url); 50 static GURL ClearEmptyRef(const GURL& url);
38 51
39 SafeBrowsingNavigationObserverManager(); 52 SafeBrowsingNavigationObserverManager();
40 53
41 // Add |nav_event| to |navigation_map_| based on |nav_event_key|. Object 54 // 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. 55 // pointed to by |nav_event| will be no longer accessible after this function.
43 void RecordNavigationEvent(const GURL& nav_event_key, 56 void RecordNavigationEvent(const GURL& nav_event_key,
44 NavigationEvent* nav_event); 57 NavigationEvent* nav_event);
45 void RecordUserGestureForWebContents(content::WebContents* web_contents, 58 void RecordUserGestureForWebContents(content::WebContents* web_contents,
46 const base::Time& timestamp); 59 const base::Time& timestamp);
47 void OnUserGestureConsumed(content::WebContents* web_contents, 60 void OnUserGestureConsumed(content::WebContents* web_contents,
48 const base::Time& timestamp); 61 const base::Time& timestamp);
49 void RecordHostToIpMapping(const std::string& host, const std::string& ip); 62 void RecordHostToIpMapping(const std::string& host, const std::string& ip);
63
50 // Clean-ups need to be done when a WebContents gets destroyed. 64 // Clean-ups need to be done when a WebContents gets destroyed.
51 void OnWebContentDestroyed(content::WebContents* web_contents); 65 void OnWebContentDestroyed(content::WebContents* web_contents);
52 66
53 // TODO(jialiul): more functions are coming for managing navigation_map_. 67 // Remove all the observed NavigationEvents, user gestures, and resolved IP
68 // addresses that are older than kNavigationFootprintTTLInSecond.
69 void CleanUpStaleNavigationFootprints();
70
71 // Based on the |target_url| and |target_tab_id|, backtrace observed
Charlie Reis 2016/12/14 07:43:58 nit: s/backtrace/trace back the/
Jialiu Lin 2016/12/14 19:06:29 Done.
72 // NavigationEvents in navigation_map_ to identify the sequence of navigations
Charlie Reis 2016/12/14 07:43:58 user-initiated navigations?
Jialiu Lin 2016/12/14 19:06:29 Not only user-initiated navigations but also redir
73 // lead to the target with the coverage limited to |user_gesture_count_limit|
Charlie Reis 2016/12/14 07:43:58 nit: s/lead/leading/ nit: comma after target
Jialiu Lin 2016/12/14 19:06:29 Done.
74 // number of user gestures. Then convert these identified NavigationEvents
75 // into ReferrerChainEntrys and append them to |out_referrer_chain|.
76 AttributionResult IdentifyReferrerChain(
77 const GURL& target_url,
78 int target_tab_id, // -1 if tab id is not valid
79 int user_gesture_count_limit,
80 std::vector<ReferrerChainEntry>* out_referrer_chain);
81
82 // Identify and add referrer chain info of a download to ClientDownloadRequest
83 // proto. This function also record UMA stats of download attribution result.
84 // TODO(jialiul): This function will be moved to DownloadProtectionService
85 // class shortly.
86 void AddReferrerChainToClientDownloadRequest(
87 const GURL& download_url,
88 content::WebContents* source_contents,
89 ClientDownloadRequest* out_request);
54 90
55 private: 91 private:
56 friend class base::RefCountedThreadSafe< 92 friend class base::RefCountedThreadSafe<
57 SafeBrowsingNavigationObserverManager>; 93 SafeBrowsingNavigationObserverManager>;
58 friend class TestNavigationObserverManager; 94 friend class TestNavigationObserverManager;
59 friend class SBNavigationObserverBrowserTest; 95 friend class SBNavigationObserverBrowserTest;
60 friend class SBNavigationObserverTest; 96 friend class SBNavigationObserverTest;
61 97
62 struct GurlHash { 98 struct GurlHash {
63 std::size_t operator()(const GURL& url) const { 99 std::size_t operator()(const GURL& url) const {
(...skipping 13 matching lines...) Expand all
77 void Observe(int type, 113 void Observe(int type,
78 const content::NotificationSource& source, 114 const content::NotificationSource& source,
79 const content::NotificationDetails& details) override; 115 const content::NotificationDetails& details) override;
80 116
81 void RecordRetargeting(const content::NotificationDetails& details); 117 void RecordRetargeting(const content::NotificationDetails& details);
82 118
83 NavigationMap* navigation_map() { return &navigation_map_; } 119 NavigationMap* navigation_map() { return &navigation_map_; }
84 120
85 HostToIpMap* host_to_ip_map() { return &host_to_ip_map_; } 121 HostToIpMap* host_to_ip_map() { return &host_to_ip_map_; }
86 122
123 // Remove stale entries from navigation_map_ if they are older than
124 // kNavigationFootprintTTLInSecond (2 minutes).
125 void CleanUpNavigationEvents();
126
127 // Remove stale entries from user_gesture_map_ if they are older than
128 // kUserGestureTTLInSecond (1 sec).
129 void CleanUpUserGestures();
130
131 // Remove stale entries from host_to_ip_map_ if they are older than
132 // kNavigationFootprintTTLInSecond (2 minutes).
133 void CleanUpIpAddresses();
134
135 bool IsCleanUpScheduled() const;
136
137 void ScheduleNextCleanUpAfterInterval(base::TimeDelta interval);
138
139 // Find the most recent navigation event that navigated to |target_url| in the
140 // tab with ID |target_tab_id|. If |target_tab_id| is not available (-1), we
141 // look for all tabs for the most recent navigation to |target_url|.
142 // For some cases, the most recent navigation to |target_url| may not be
143 // relevant.
144 // For example, url1 in window A opens url2 in window B, url1 then opens an
145 // about::blank page window C and injects script code in it to trigger a
Charlie Reis 2016/12/14 07:43:58 nit: about:blank
Jialiu Lin 2016/12/14 19:06:29 Oops. Fixed.
146 // delayed download in Window D. Before the download occurs, url2 in window B
147 // opens a different about:blank page in window C.
148 // A ---- C - D
149 // \ /
150 // B
151 // In this case, FindNavigationEvent() will think url2 in Window B is the
152 // referrer of about::blank in Window C since this navigation is more recent.
153 // However, it does not prevent us to attribute url1 in Window A as the cause
154 // of all these navigations.
155 NavigationEvent* FindNavigationEvent(const GURL& target_url,
156 int target_tab_id);
157
158 void AddToReferrerChain(std::vector<ReferrerChainEntry>* referrer_chain,
159 NavigationEvent* nav_event,
160 ReferrerChainEntry::URLType type);
161
87 // navigation_map_ keeps track of all the observed navigations. This map is 162 // 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 163 // 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 164 // 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 165 // 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 166 // across different tabs and frames, the value of this map is a vector of
92 // NavigationEvent ordered by navigation finish time. 167 // NavigationEvent ordered by navigation finish time.
93 // TODO(jialiul): Entries in navigation_map_ will be removed if they are older 168 // TODO(jialiul): Entries in navigation_map_ will be removed if they are older
94 // than 2 minutes since their corresponding navigations finish. 169 // than 2 minutes since their corresponding navigations finish.
95 NavigationMap navigation_map_; 170 NavigationMap navigation_map_;
96 171
97 // user_gesture_map_ keeps track of the timestamp of last user gesture in 172 // 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 173 // in each WebContents. We assume for majority of cases, a navigation
99 // shortly after a user gesture indicate this navigation is user initiated. 174 // shortly after a user gesture indicate this navigation is user initiated.
100 UserGestureMap user_gesture_map_; 175 UserGestureMap user_gesture_map_;
101 176
102 // Host to timestamped IP addresses map that covers all the main frame and 177 // 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 178 // 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 179 // 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 180 // vector of ResolvedIPAddresss. This map is used to fill in ip_address field
106 // in URLChainEntry in ClientDownloadRequest. 181 // in URLChainEntry in ClientDownloadRequest.
107 HostToIpMap host_to_ip_map_; 182 HostToIpMap host_to_ip_map_;
108 183
109 content::NotificationRegistrar registrar_; 184 content::NotificationRegistrar registrar_;
110 185
186 base::OneShotTimer cleanup_timer_;
187
111 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingNavigationObserverManager); 188 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingNavigationObserverManager);
112 }; 189 };
113 } // namespace safe_browsing 190 } // namespace safe_browsing
114 191
115 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_NAVIGATION_OBSERVER_MANAGE R_H_ 192 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_NAVIGATION_OBSERVER_MANAGE R_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698