Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 | |
| 23 // Manager class for SafeBrowsingNavigationObserver, which in charge of cleaning | |
| 24 // up stale navigation events, and identifing landing page/landing referrer for | |
| 25 // a specific download. | |
| 26 // TODO(jialiul): For now, SafeBrowsingNavigationObserverManager also listens to | |
| 27 // NOTIFICATION_RETARGETING as a way to detect cross frame/tab navigation. | |
| 28 // Remove base class content::NotificationObserver when | |
| 29 // WebContentsObserver::DidOpenRequestedURL() covers all retargeting cases. | |
| 30 class SafeBrowsingNavigationObserverManager | |
| 31 : public content::NotificationObserver, | |
| 32 public base::RefCountedThreadSafe<SafeBrowsingNavigationObserverManager> { | |
| 33 public: | |
| 34 struct GurlHash { | |
| 35 std::size_t operator()(const GURL& url) const { | |
| 36 return std::hash<std::string>()(url.spec()); | |
| 37 } | |
| 38 }; | |
| 39 typedef std::unordered_map<GURL, std::vector<NavigationEvent>, GurlHash> | |
| 40 NavigationMap; | |
| 41 | |
| 42 // Helper function to check if user gesture is older than | |
| 43 // kUserGestureTTLInSecond; | |
| 44 static bool IsUserGestureExpired(const base::Time& timestamp); | |
| 45 // Helper function to strip ref fragment from a URL. | |
| 46 static GURL ClearURLRef(const GURL& url); | |
| 47 | |
| 48 SafeBrowsingNavigationObserverManager(); | |
| 49 void RecordNavigationEvent(const GURL& nav_event_key, | |
| 50 NavigationEvent* nav_event); | |
| 51 void RecordUserGestureForWebContents(content::WebContents* web_contents, | |
| 52 const base::Time& timestamp); | |
| 53 void OnUserGestureConsumed(content::WebContents* web_contents, | |
| 54 const base::Time& timestamp); | |
| 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<content::WebContents*, base::Time> UserGestureMap; | |
| 68 | |
| 69 ~SafeBrowsingNavigationObserverManager() override; | |
| 70 | |
| 71 // content::NotificationObserver: | |
| 72 void Observe(int type, | |
| 73 const content::NotificationSource& source, | |
| 74 const content::NotificationDetails& details) override; | |
| 75 | |
| 76 void RecordRetargeting(const content::NotificationDetails& details); | |
| 77 | |
| 78 NavigationMap* navigation_map() { return &navigation_map_; } | |
| 79 | |
| 80 // navigation_map_ keeps track of all the observed navigations. This map is | |
| 81 // keyed on the resolved request url. In other words, in case of server | |
| 82 // redirects, its key is the last server redirect url, otherwise, it is the | |
| 83 // original target url. Since the same url can be requested multiple times | |
| 84 // across different tabs and frames, the value of this map is a vector of | |
| 85 // NavigationEvent ordered by navigation finish time. Entries in | |
| 86 // navigation_map_ will be removed if they are older than 2 minutes since | |
| 87 // their corresponding navigations finish. | |
|
Charlie Reis
2016/09/28 21:08:13
Please make this last sentence a TODO, since it's
Jialiu Lin
2016/09/28 21:39:14
Done.
| |
| 88 NavigationMap navigation_map_; | |
| 89 | |
| 90 // user_gesture_map_ keeps track of the timestamp of last user gesture in | |
| 91 // in each WebContents. We assume for majority of cases, a navigation | |
| 92 // shortly after a user gesture indicate this navigation is user initiated. | |
| 93 UserGestureMap user_gesture_map_; | |
| 94 | |
| 95 content::NotificationRegistrar registrar_; | |
| 96 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingNavigationObserverManager); | |
| 97 }; | |
| 98 } // namespace safe_browsing | |
| 99 | |
| 100 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_NAVIGATION_OBSERVER_MANAGE R_H_ | |
| OLD | NEW |