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

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

Issue 2302913003: Add SafeBrowsingNavigationObserver to listen to navigation events (Closed)
Patch Set: nit Created 4 years, 3 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_H_
6 #define CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_NAVIGATION_OBSERVER_H_
7
8 #include "base/supports_user_data.h"
9 #include "content/public/browser/navigation_handle.h"
10 #include "content/public/browser/notification_observer.h"
11 #include "content/public/browser/notification_registrar.h"
12 #include "content/public/browser/web_contents_observer.h"
13 #include "url/gurl.h"
14
15 namespace content {
16 class NavigationHandle;
17 class NotificationRegistrar;
Charlie Reis 2016/09/23 23:14:14 nit: You're including NavigationHandle and Notific
Jialiu Lin 2016/09/27 17:51:00 Done.
18 class RenderFrameHost;
19 struct ResourceRedirectDetails;
20 }
21
22 namespace safe_browsing {
23
24 // Struct to record the details of a navigation event for all frames.
Charlie Reis 2016/09/23 23:14:14 nit: for any frame. (A single struct won't record
Jialiu Lin 2016/09/27 17:51:00 Done.
25 // This information will be used to fill |url_chain| field in safe browsing
26 // download pings.
27 struct NavigationEvent {
28 NavigationEvent();
29 NavigationEvent(NavigationEvent&& nav_event);
30 NavigationEvent& operator=(NavigationEvent&& nav_event);
31 ~NavigationEvent();
32 GURL source_url;
Charlie Reis 2016/09/23 23:14:14 nit: Add blank line before, and add a comment sayi
Jialiu Lin 2016/09/27 17:51:00 Done.
33 int source_tab_id; // Tab ID of the source WebContents returned by
34 // SessionTabHelper. It is immutable for a given tab
35 // and unique across Chrome within the current session.
36
37 GURL target_url;
Charlie Reis 2016/09/23 23:14:14 Let's be clear about whether this is the original
Jialiu Lin 2016/09/27 17:50:59 This is the original request URL. But NavigationMa
38 int target_tab_id;
39
40 int frame_id; // Frame tree node id.
41 GURL main_frame_url; // Main frame url of the source.
42
43 base::Time timestamp; // Timestamp of last time this object is updated.
44
45 bool is_user_initiated; // browser_initiated || has_user_gesture.
46 bool has_committed;
47 bool is_server_redirect;
48 GURL server_redirect_url; // Last server redirect url in redirect chain.
Charlie Reis 2016/09/23 23:14:14 Please clarify in the comment whether this is alwa
Jialiu Lin 2016/09/27 17:51:00 Yes, in case no server redirect, this field will b
49 };
50
51 // Manager class for SafeBrowsingNavigationObserver.
Charlie Reis 2016/09/23 23:14:14 Can you say more about what this class manages? T
Jialiu Lin 2016/09/27 17:51:00 Done.
52 // TODO(jialiul): Remove base class content::NotificationObserver when
53 // WebContentsObserver::DidOpenRequestedURL() covers all retargeting cases.
54 class SBNavigationObserverManager
Charlie Reis 2016/09/23 23:14:14 Is declaring a top-level class in the same file ok
Jialiu Lin 2016/09/27 17:51:00 Moved this manager class into a separate file. Don
55 : public content::NotificationObserver,
56 public base::RefCountedThreadSafe<SBNavigationObserverManager> {
57 public:
58 struct GurlHash {
59 std::size_t operator()(const GURL& url) const {
60 return std::hash<std::string>()(url.spec());
61 }
62 };
63 typedef std::unordered_map<GURL, std::vector<NavigationEvent>, GurlHash>
64 NavigationMap;
65 SBNavigationObserverManager();
Charlie Reis 2016/09/23 23:14:14 nit: Blank line before (after typedefs).
Jialiu Lin 2016/09/27 17:51:00 Done.
66 void RecordNavigationEvent(const GURL& nav_event_key,
67 NavigationEvent* nav_event);
68 void RecordUserGestureForWebContents(content::WebContents* web_contents,
69 const base::Time& timestamp);
70 void OnUserGestureConsumed(content::WebContents* web_contents,
71 const base::Time& timestamp);
72 // Clean-ups need to be done when a WebContents gets destroyed.
73 void OnWebContentDestroyed(content::WebContents* web_contents);
74
75 // TODO(jialiul): more functions coming for managing navigation_map_.
76
77 private:
78 friend class base::RefCountedThreadSafe<SBNavigationObserverManager>;
79 friend class TestNavigationObserverManager;
80 friend class SBNavigationObserverBrowserTest;
81 friend class SBNavigationObserverTest;
82
83 typedef std::unordered_map<content::WebContents*, base::Time> UserGestureMap;
84
85 ~SBNavigationObserverManager() override;
86
87 // content::NotificationObserver:
88 void Observe(int type,
89 const content::NotificationSource& source,
90 const content::NotificationDetails& details) override;
91
92 void RecordRetargeting(const content::NotificationDetails& details);
93
94 NavigationMap* navigation_map() { return &navigation_map_; }
95
96 // navigation_map_ keeps track of all the observed navigations. This map is
97 // keyed on target url. Since the same url can be requested multiple times
Charlie Reis 2016/09/23 23:14:14 Is it correct to key this on the pre-redirect URL?
Jialiu Lin 2016/09/27 17:50:59 Fixed this comments. Map actually keyed on post-re
98 // across different tabs and frames, the value of this map is a vector of
99 // NavigationEvent ordered by navigation finish time. Entries in
100 // navigation_map_ will be removed if they are older than 2 minutes since
101 // their corresponding navigations finish.
Charlie Reis 2016/09/23 23:14:14 Is this timer implemented yet? I'm curious how it
Jialiu Lin 2016/09/27 17:50:59 Not yet. It will be in my next CL.
102 NavigationMap navigation_map_;
103
104 // user_gesture_map_ keeps track of the timestamp of last user gesture in
105 // in each WebContents. This is used to determin if any retargeting navigation
Charlie Reis 2016/09/23 23:14:14 nit: determine This seems like it won't be accura
Jialiu Lin 2016/09/27 17:51:00 Safe browsing backend will aggregate download ping
106 // is user_initiated.
107 UserGestureMap user_gesture_map_;
108
109 content::NotificationRegistrar registrar_;
110 DISALLOW_COPY_AND_ASSIGN(SBNavigationObserverManager);
111 };
112
113 // Observes the navigation events for a single WebContents (both main-frame
114 // and sub-frame navigations)
115 class SafeBrowsingNavigationObserver : public base::SupportsUserData::Data,
116 public content::WebContentsObserver {
117 public:
118 static void MaybeCreateForWebContents(content::WebContents* web_contents);
119 static SafeBrowsingNavigationObserver* FromWebContents(
120 content::WebContents* web_contents);
121
122 SafeBrowsingNavigationObserver(
123 content::WebContents* contents,
124 const scoped_refptr<SBNavigationObserverManager>& manager);
125
126 ~SafeBrowsingNavigationObserver() override;
127
128 private:
129 static const char kWebContentsUserDataKey[];
Charlie Reis 2016/09/23 23:14:14 nit: Let's just declare this in an anonymous names
Jialiu Lin 2016/09/27 17:51:00 Done.
130 void RecordNavigationPendingEntry(
Charlie Reis 2016/09/23 23:14:14 I don't see this implemented anywhere.
Jialiu Lin 2016/09/27 17:50:59 Oops, forgot to delete this one.
131 const content::NotificationSource& source,
132 const content::NotificationDetails& details);
133
134 // content::WebContentsObserver:
135 void DidStartNavigation(
136 content::NavigationHandle* navigation_handle) override;
137 void DidRedirectNavigation(
138 content::NavigationHandle* navigation_handle) override;
139 void DidFinishNavigation(
140 content::NavigationHandle* navigation_handle) override;
141 void DidGetUserInteraction(const blink::WebInputEvent::Type type) override;
142 void WebContentsDestroyed() override;
143
144 // Map keyed on NavigationHandle* to keep track of all the ongoing navigation
145 // events. NavigationHandle pointers are owned by RenderFrameHost. Since a
146 // NavigationHandle object will be destructed after navigation is done,
147 // corresponding entry in this map will be removed from navigation_handle_map_
148 // and added to navigation_map_ at the end of DidFinishNavigation(...).
149 typedef std::unordered_map<content::NavigationHandle*, NavigationEvent>
150 NavigationHandleMap;
Charlie Reis 2016/09/23 23:14:14 nit: Typedefs should be declared at the beginning
Jialiu Lin 2016/09/27 17:50:59 Done
151 NavigationHandleMap navigation_handle_map_;
152 scoped_refptr<SBNavigationObserverManager> manager_;
153 // If current on-going navigation triggered by a user gesture.
154 bool has_user_gesture_;
Charlie Reis 2016/09/23 23:14:14 I don't think we can track this on a per-tab basis
Jialiu Lin 2016/09/27 17:51:00 Oh, sorry for the misleading comment. This field
155 base::Time last_user_gesture_timestamp_;
156 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingNavigationObserver);
157 };
158
159 } // namespace safe_browsing
160
161 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_NAVIGATION_OBSERVER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698