1 // Copyright 2020 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 #include "components/security_interstitials/content/unsafe_resource_util.h"
6
7 #include "base/bind.h"
8 #include "content/public/browser/navigation_entry.h"
9 #include "content/public/browser/render_frame_host.h"
10 #include "content/public/browser/web_contents.h"
11
12 namespace security_interstitials {
13
14 namespace {
15
GetWebContentsByFrameID(int render_process_id,int render_frame_id)16 content::WebContents* GetWebContentsByFrameID(int render_process_id,
17 int render_frame_id) {
18 content::RenderFrameHost* render_frame_host =
19 content::RenderFrameHost::FromID(render_process_id, render_frame_id);
20 return render_frame_host
21 ? content::WebContents::FromRenderFrameHost(render_frame_host)
22 : nullptr;
23 }
24
25 } // namespace
26
GetNavigationEntryForResource(const UnsafeResource & resource)27 content::NavigationEntry* GetNavigationEntryForResource(
28 const UnsafeResource& resource) {
29 content::WebContents* web_contents = resource.web_contents_getter.Run();
30 if (!web_contents)
31 return nullptr;
32 // If a safebrowsing hit occurs during main frame navigation, the navigation
33 // will not be committed, and the pending navigation entry refers to the hit.
34 if (resource.IsMainPageLoadBlocked())
35 return web_contents->GetController().GetPendingEntry();
36 // If a safebrowsing hit occurs on a subresource load, or on a main frame
37 // after the navigation is committed, the last committed navigation entry
38 // refers to the page with the hit. Note that there may concurrently be an
39 // unrelated pending navigation to another site, so GetActiveEntry() would be
40 // wrong.
41 return web_contents->GetController().GetLastCommittedEntry();
42 }
43
GetWebContentsGetter(int render_process_host_id,int render_frame_id)44 base::RepeatingCallback<content::WebContents*(void)> GetWebContentsGetter(
45 int render_process_host_id,
46 int render_frame_id) {
47 return base::BindRepeating(&GetWebContentsByFrameID, render_process_host_id,
48 render_frame_id);
49 }
50
51 } // namespace security_interstitials
52