1 // Copyright 2019 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 "android_webview/browser/safe_browsing/aw_safe_browsing_subresource_helper.h"
6 
7 #include <memory>
8 
9 #include "android_webview/browser/aw_browser_process.h"
10 #include "android_webview/browser/network_service/aw_web_resource_request.h"
11 #include "android_webview/browser/safe_browsing/aw_safe_browsing_blocking_page.h"
12 #include "android_webview/browser/safe_browsing/aw_safe_browsing_ui_manager.h"
13 #include "base/memory/ptr_util.h"
14 #include "components/security_interstitials/content/security_interstitial_tab_helper.h"
15 #include "content/public/browser/navigation_handle.h"
16 #include "content/public/browser/web_contents.h"
17 #include "content/public/browser/web_contents_observer.h"
18 #include "net/base/net_errors.h"
19 
20 namespace android_webview {
21 
~AwSafeBrowsingSubresourceHelper()22 AwSafeBrowsingSubresourceHelper::~AwSafeBrowsingSubresourceHelper() {}
23 
ReadyToCommitNavigation(content::NavigationHandle * navigation_handle)24 void AwSafeBrowsingSubresourceHelper::ReadyToCommitNavigation(
25     content::NavigationHandle* navigation_handle) {
26   if (navigation_handle->GetNetErrorCode() == net::ERR_BLOCKED_BY_CLIENT) {
27     AwSafeBrowsingUIManager* manager =
28         AwBrowserProcess::GetInstance()->GetSafeBrowsingUIManager();
29     if (!manager)
30       return;
31     security_interstitials::UnsafeResource resource;
32     if (manager->PopUnsafeResourceForURL(navigation_handle->GetURL(),
33                                          &resource)) {
34       std::unique_ptr<AwWebResourceRequest> request =
35           std::make_unique<AwWebResourceRequest>(
36               navigation_handle->GetURL().spec(),
37               navigation_handle->IsPost() ? "POST" : "GET",
38               navigation_handle->IsInMainFrame(),
39               navigation_handle->HasUserGesture(),
40               navigation_handle->GetRequestHeaders());
41       request->is_renderer_initiated = navigation_handle->IsRendererInitiated();
42       AwSafeBrowsingBlockingPage* blocking_page =
43           AwSafeBrowsingBlockingPage::CreateBlockingPage(
44               manager, navigation_handle->GetWebContents(),
45               navigation_handle->GetURL(), resource, std::move(request));
46       security_interstitials::SecurityInterstitialTabHelper::
47           AssociateBlockingPage(navigation_handle->GetWebContents(),
48                                 navigation_handle->GetNavigationId(),
49                                 base::WrapUnique(blocking_page));
50     }
51   }
52 }
53 
AwSafeBrowsingSubresourceHelper(content::WebContents * web_contents)54 AwSafeBrowsingSubresourceHelper::AwSafeBrowsingSubresourceHelper(
55     content::WebContents* web_contents)
56     : WebContentsObserver(web_contents) {}
57 
58 WEB_CONTENTS_USER_DATA_KEY_IMPL(AwSafeBrowsingSubresourceHelper)
59 
60 }  // namespace android_webview
61