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 "content/browser/network_context_client_base_impl.h"
6 
7 #include "base/bind.h"
8 #include "base/task/post_task.h"
9 #include "base/task/task_traits.h"
10 #include "base/task/thread_pool.h"
11 #include "base/task_runner.h"
12 #include "build/build_config.h"
13 #include "content/browser/child_process_security_policy_impl.h"
14 #include "content/public/browser/network_context_client_base.h"
15 #include "mojo/public/cpp/bindings/remote.h"
16 
17 #if defined(OS_ANDROID)
18 #include "base/android/content_uri_utils.h"
19 #endif
20 
21 namespace content {
22 
23 namespace {
24 
HandleFileUploadRequest(int32_t process_id,bool async,const std::vector<base::FilePath> & file_paths,network::mojom::NetworkContextClient::OnFileUploadRequestedCallback callback,scoped_refptr<base::TaskRunner> task_runner)25 void HandleFileUploadRequest(
26     int32_t process_id,
27     bool async,
28     const std::vector<base::FilePath>& file_paths,
29     network::mojom::NetworkContextClient::OnFileUploadRequestedCallback
30         callback,
31     scoped_refptr<base::TaskRunner> task_runner) {
32   std::vector<base::File> files;
33   uint32_t file_flags = base::File::FLAG_OPEN | base::File::FLAG_READ |
34                         (async ? base::File::FLAG_ASYNC : 0);
35   ChildProcessSecurityPolicy* cpsp = ChildProcessSecurityPolicy::GetInstance();
36   for (const auto& file_path : file_paths) {
37     if (process_id != network::mojom::kBrowserProcessId &&
38         !cpsp->CanReadFile(process_id, file_path)) {
39       task_runner->PostTask(
40           FROM_HERE, base::BindOnce(std::move(callback), net::ERR_ACCESS_DENIED,
41                                     std::vector<base::File>()));
42       return;
43     }
44 #if defined(OS_ANDROID)
45     if (file_path.IsContentUri()) {
46       files.push_back(base::OpenContentUriForRead(file_path));
47     } else {
48       files.emplace_back(file_path, file_flags);
49     }
50 #else
51     files.emplace_back(file_path, file_flags);
52 #endif
53     if (!files.back().IsValid()) {
54       task_runner->PostTask(
55           FROM_HERE,
56           base::BindOnce(std::move(callback),
57                          net::FileErrorToNetError(files.back().error_details()),
58                          std::vector<base::File>()));
59       return;
60     }
61   }
62   task_runner->PostTask(FROM_HERE, base::BindOnce(std::move(callback), net::OK,
63                                                   std::move(files)));
64 }
65 
66 }  // namespace
67 
NetworkContextOnFileUploadRequested(int32_t process_id,bool async,const std::vector<base::FilePath> & file_paths,network::mojom::NetworkContextClient::OnFileUploadRequestedCallback callback)68 void NetworkContextOnFileUploadRequested(
69     int32_t process_id,
70     bool async,
71     const std::vector<base::FilePath>& file_paths,
72     network::mojom::NetworkContextClient::OnFileUploadRequestedCallback
73         callback) {
74   base::ThreadPool::PostTask(
75       FROM_HERE, {base::MayBlock(), base::TaskPriority::USER_BLOCKING},
76       base::BindOnce(&HandleFileUploadRequest, process_id, async, file_paths,
77                      std::move(callback),
78                      base::SequencedTaskRunnerHandle::Get()));
79 }
80 
81 NetworkContextClientBase::NetworkContextClientBase() = default;
82 NetworkContextClientBase::~NetworkContextClientBase() = default;
83 
OnAuthRequired(const base::Optional<base::UnguessableToken> & window_id,int32_t process_id,int32_t routing_id,uint32_t request_id,const GURL & url,bool first_auth_attempt,const net::AuthChallengeInfo & auth_info,network::mojom::URLResponseHeadPtr head,mojo::PendingRemote<network::mojom::AuthChallengeResponder> auth_challenge_responder)84 void NetworkContextClientBase::OnAuthRequired(
85     const base::Optional<base::UnguessableToken>& window_id,
86     int32_t process_id,
87     int32_t routing_id,
88     uint32_t request_id,
89     const GURL& url,
90     bool first_auth_attempt,
91     const net::AuthChallengeInfo& auth_info,
92     network::mojom::URLResponseHeadPtr head,
93     mojo::PendingRemote<network::mojom::AuthChallengeResponder>
94         auth_challenge_responder) {
95   mojo::Remote<network::mojom::AuthChallengeResponder>
96       auth_challenge_responder_remote(std::move(auth_challenge_responder));
97   auth_challenge_responder_remote->OnAuthCredentials(base::nullopt);
98 }
99 
OnCertificateRequested(const base::Optional<base::UnguessableToken> & window_id,int32_t process_id,int32_t routing_id,uint32_t request_id,const scoped_refptr<net::SSLCertRequestInfo> & cert_info,mojo::PendingRemote<network::mojom::ClientCertificateResponder> cert_responder_remote)100 void NetworkContextClientBase::OnCertificateRequested(
101     const base::Optional<base::UnguessableToken>& window_id,
102     int32_t process_id,
103     int32_t routing_id,
104     uint32_t request_id,
105     const scoped_refptr<net::SSLCertRequestInfo>& cert_info,
106     mojo::PendingRemote<network::mojom::ClientCertificateResponder>
107         cert_responder_remote) {
108   mojo::Remote<network::mojom::ClientCertificateResponder> cert_responder(
109       std::move(cert_responder_remote));
110   cert_responder->CancelRequest();
111 }
112 
OnSSLCertificateError(int32_t process_id,int32_t routing_id,const GURL & url,int net_error,const net::SSLInfo & ssl_info,bool fatal,OnSSLCertificateErrorCallback response)113 void NetworkContextClientBase::OnSSLCertificateError(
114     int32_t process_id,
115     int32_t routing_id,
116     const GURL& url,
117     int net_error,
118     const net::SSLInfo& ssl_info,
119     bool fatal,
120     OnSSLCertificateErrorCallback response) {
121   std::move(response).Run(net::ERR_ABORTED);
122 }
123 
OnFileUploadRequested(int32_t process_id,bool async,const std::vector<base::FilePath> & file_paths,OnFileUploadRequestedCallback callback)124 void NetworkContextClientBase::OnFileUploadRequested(
125     int32_t process_id,
126     bool async,
127     const std::vector<base::FilePath>& file_paths,
128     OnFileUploadRequestedCallback callback) {
129   NetworkContextOnFileUploadRequested(process_id, async, file_paths,
130                                       std::move(callback));
131 }
132 
OnCanSendReportingReports(const std::vector<url::Origin> & origins,OnCanSendReportingReportsCallback callback)133 void NetworkContextClientBase::OnCanSendReportingReports(
134     const std::vector<url::Origin>& origins,
135     OnCanSendReportingReportsCallback callback) {
136   std::move(callback).Run(std::vector<url::Origin>());
137 }
138 
OnCanSendDomainReliabilityUpload(const GURL & origin,OnCanSendDomainReliabilityUploadCallback callback)139 void NetworkContextClientBase::OnCanSendDomainReliabilityUpload(
140     const GURL& origin,
141     OnCanSendDomainReliabilityUploadCallback callback) {
142   std::move(callback).Run(false);
143 }
144 
OnClearSiteData(int32_t process_id,int32_t routing_id,const GURL & url,const std::string & header_value,int load_flags,OnClearSiteDataCallback callback)145 void NetworkContextClientBase::OnClearSiteData(
146     int32_t process_id,
147     int32_t routing_id,
148     const GURL& url,
149     const std::string& header_value,
150     int load_flags,
151     OnClearSiteDataCallback callback) {
152   std::move(callback).Run();
153 }
154 
155 #if defined(OS_ANDROID)
OnGenerateHttpNegotiateAuthToken(const std::string & server_auth_token,bool can_delegate,const std::string & auth_negotiate_android_account_type,const std::string & spn,OnGenerateHttpNegotiateAuthTokenCallback callback)156 void NetworkContextClientBase::OnGenerateHttpNegotiateAuthToken(
157     const std::string& server_auth_token,
158     bool can_delegate,
159     const std::string& auth_negotiate_android_account_type,
160     const std::string& spn,
161     OnGenerateHttpNegotiateAuthTokenCallback callback) {
162   std::move(callback).Run(net::ERR_FAILED, server_auth_token);
163 }
164 #endif
165 
166 #if defined(OS_CHROMEOS)
OnTrustAnchorUsed()167 void NetworkContextClientBase::OnTrustAnchorUsed() {}
168 #endif
169 
OnTrustTokenIssuanceDivertedToSystem(network::mojom::FulfillTrustTokenIssuanceRequestPtr request,OnTrustTokenIssuanceDivertedToSystemCallback callback)170 void NetworkContextClientBase::OnTrustTokenIssuanceDivertedToSystem(
171     network::mojom::FulfillTrustTokenIssuanceRequestPtr request,
172     OnTrustTokenIssuanceDivertedToSystemCallback callback) {
173   auto response = network::mojom::FulfillTrustTokenIssuanceAnswer::New();
174   response->status =
175       network::mojom::FulfillTrustTokenIssuanceAnswer::Status::kNotFound;
176   std::move(callback).Run(std::move(response));
177 }
178 
179 }  // namespace content
180