| OLD | NEW |
| 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 #include "chrome/browser/safe_browsing/safe_browsing_navigation_observer.h" | 5 #include "chrome/browser/safe_browsing/safe_browsing_navigation_observer.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/time/time.h" | 8 #include "base/time/time.h" |
| 9 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/safe_browsing/safe_browsing_navigation_observer_manager
.h" | 11 #include "chrome/browser/safe_browsing/safe_browsing_navigation_observer_manager
.h" |
| 12 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 10 #include "chrome/browser/sessions/session_tab_helper.h" | 13 #include "chrome/browser/sessions/session_tab_helper.h" |
| 11 #include "content/public/browser/navigation_handle.h" | 14 #include "content/public/browser/navigation_handle.h" |
| 12 #include "content/public/browser/render_frame_host.h" | 15 #include "content/public/browser/render_frame_host.h" |
| 13 #include "content/public/browser/resource_request_details.h" | 16 #include "content/public/browser/resource_request_details.h" |
| 14 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
| 15 #include "content/public/common/resource_type.h" | 18 #include "content/public/common/resource_type.h" |
| 16 | 19 |
| 17 using content::WebContents; | 20 using content::WebContents; |
| 18 | 21 |
| 19 namespace { | 22 namespace { |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 | 70 |
| 68 NavigationEvent::~NavigationEvent() {} | 71 NavigationEvent::~NavigationEvent() {} |
| 69 | 72 |
| 70 // SafeBrowsingNavigationObserver -------------------------------------------- | 73 // SafeBrowsingNavigationObserver -------------------------------------------- |
| 71 | 74 |
| 72 // static | 75 // static |
| 73 void SafeBrowsingNavigationObserver::MaybeCreateForWebContents( | 76 void SafeBrowsingNavigationObserver::MaybeCreateForWebContents( |
| 74 content::WebContents* web_contents) { | 77 content::WebContents* web_contents) { |
| 75 if (FromWebContents(web_contents)) | 78 if (FromWebContents(web_contents)) |
| 76 return; | 79 return; |
| 77 // TODO(jialiul): This method will be called by TabHelpers::AttachTabHelpers. | 80 |
| 78 // Complete this method when the entire class is ready. | 81 if (safe_browsing::SafeBrowsingNavigationObserverManager::IsEnabledAndReady( |
| 79 NOTIMPLEMENTED(); | 82 Profile::FromBrowserContext(web_contents->GetBrowserContext()))) { |
| 83 web_contents->SetUserData( |
| 84 kWebContentsUserDataKey, |
| 85 new SafeBrowsingNavigationObserver( |
| 86 web_contents, |
| 87 g_browser_process->safe_browsing_service() |
| 88 ->navigation_observer_manager())); |
| 89 } |
| 80 } | 90 } |
| 81 | 91 |
| 82 // static | 92 // static |
| 83 SafeBrowsingNavigationObserver* SafeBrowsingNavigationObserver::FromWebContents( | 93 SafeBrowsingNavigationObserver* SafeBrowsingNavigationObserver::FromWebContents( |
| 84 content::WebContents* web_contents) { | 94 content::WebContents* web_contents) { |
| 85 return static_cast<SafeBrowsingNavigationObserver*>( | 95 return static_cast<SafeBrowsingNavigationObserver*>( |
| 86 web_contents->GetUserData(kWebContentsUserDataKey)); | 96 web_contents->GetUserData(kWebContentsUserDataKey)); |
| 87 } | 97 } |
| 88 | 98 |
| 89 SafeBrowsingNavigationObserver::SafeBrowsingNavigationObserver( | 99 SafeBrowsingNavigationObserver::SafeBrowsingNavigationObserver( |
| (...skipping 18 matching lines...) Expand all Loading... |
| 108 // we need to copy the is_user_initiated field. | 118 // we need to copy the is_user_initiated field. |
| 109 if (it != navigation_handle_map_.end()) { | 119 if (it != navigation_handle_map_.end()) { |
| 110 nav_event.is_user_initiated = it->second.is_user_initiated; | 120 nav_event.is_user_initiated = it->second.is_user_initiated; |
| 111 } else { | 121 } else { |
| 112 // If this is the first time we see this navigation_handle, create a new | 122 // If this is the first time we see this navigation_handle, create a new |
| 113 // NavigationEvent, and decide if it is triggered by user. | 123 // NavigationEvent, and decide if it is triggered by user. |
| 114 if ((has_user_gesture_ && | 124 if ((has_user_gesture_ && |
| 115 !SafeBrowsingNavigationObserverManager::IsUserGestureExpired( | 125 !SafeBrowsingNavigationObserverManager::IsUserGestureExpired( |
| 116 last_user_gesture_timestamp_)) || | 126 last_user_gesture_timestamp_)) || |
| 117 !navigation_handle->IsRendererInitiated()) { | 127 !navigation_handle->IsRendererInitiated()) { |
| 118 nav_event.is_user_initiated = has_user_gesture_; | 128 nav_event.is_user_initiated = true; |
| 119 manager_->OnUserGestureConsumed(web_contents(), | 129 if (has_user_gesture_) { |
| 120 last_user_gesture_timestamp_); | 130 manager_->OnUserGestureConsumed(web_contents(), |
| 131 last_user_gesture_timestamp_); |
| 132 has_user_gesture_ = false; |
| 133 } |
| 121 } | 134 } |
| 122 has_user_gesture_ = false; | |
| 123 } | 135 } |
| 124 | 136 |
| 125 // All the other fields are reconstructed based on current content of | 137 // All the other fields are reconstructed based on current content of |
| 126 // navigation_handle. | 138 // navigation_handle. |
| 127 nav_event.frame_id = navigation_handle->GetFrameTreeNodeId(); | 139 nav_event.frame_id = navigation_handle->GetFrameTreeNodeId(); |
| 128 | 140 |
| 129 // If there was a URL previously committed in the current RenderFrameHost, | 141 // If there was a URL previously committed in the current RenderFrameHost, |
| 130 // set it as the source url of this navigation. Otherwise, this is the | 142 // set it as the source url of this navigation. Otherwise, this is the |
| 131 // first url going to commit in this frame. We set navigation_handle's URL as | 143 // first url going to commit in this frame. We set navigation_handle's URL as |
| 132 // the source url. | 144 // the source url. |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 last_user_gesture_timestamp_); | 239 last_user_gesture_timestamp_); |
| 228 } | 240 } |
| 229 | 241 |
| 230 void SafeBrowsingNavigationObserver::WebContentsDestroyed() { | 242 void SafeBrowsingNavigationObserver::WebContentsDestroyed() { |
| 231 manager_->OnWebContentDestroyed(web_contents()); | 243 manager_->OnWebContentDestroyed(web_contents()); |
| 232 web_contents()->RemoveUserData(kWebContentsUserDataKey); | 244 web_contents()->RemoveUserData(kWebContentsUserDataKey); |
| 233 // web_contents is null after this function. | 245 // web_contents is null after this function. |
| 234 } | 246 } |
| 235 | 247 |
| 236 } // namespace safe_browsing | 248 } // namespace safe_browsing |
| OLD | NEW |