1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/renderer_host/pepper/pepper_internal_file_ref_backend.h"
#include <string.h>
#include <string>
#include <vector>
#include "base/compiler_specific.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/strings/escape.h"
#include "content/browser/child_process_security_policy_impl.h"
#include "content/browser/file_system/browser_file_system_helper.h"
#include "content/browser/renderer_host/pepper/pepper_file_system_browser_host.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/storage_partition.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/c/pp_file_info.h"
#include "ppapi/c/pp_instance.h"
#include "ppapi/c/pp_resource.h"
#include "ppapi/c/ppb_file_ref.h"
#include "ppapi/host/dispatch_host_message.h"
#include "ppapi/host/ppapi_host.h"
#include "ppapi/proxy/ppapi_messages.h"
#include "ppapi/shared_impl/file_ref_create_info.h"
#include "ppapi/shared_impl/file_ref_util.h"
#include "ppapi/shared_impl/file_type_conversion.h"
#include "ppapi/shared_impl/scoped_pp_var.h"
#include "ppapi/shared_impl/time_conversion.h"
#include "ppapi/shared_impl/var.h"
#include "ppapi/thunk/enter.h"
#include "ppapi/thunk/ppb_file_ref_api.h"
#include "ppapi/thunk/ppb_file_system_api.h"
#include "storage/browser/file_system/copy_or_move_hook_delegate.h"
#include "storage/browser/file_system/file_system_operation.h"
#include "storage/browser/file_system/file_system_operation_runner.h"
#include "storage/browser/file_system/file_system_url.h"
#include "storage/common/file_system/file_system_util.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
using ppapi::host::PpapiHost;
using ppapi::host::ResourceHost;
namespace content {
namespace {
void CallCreateDirectory(
scoped_refptr<storage::FileSystemContext> file_system_context,
const storage::FileSystemURL& url,
bool exclusive,
bool recursive,
storage::FileSystemOperationRunner::StatusCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
file_system_context->operation_runner()->CreateDirectory(
url, exclusive, recursive, std::move(callback));
}
void CallReadDirectory(
scoped_refptr<storage::FileSystemContext> file_system_context,
const storage::FileSystemURL& url,
const storage::FileSystemOperationRunner::ReadDirectoryCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
file_system_context->operation_runner()->ReadDirectory(url,
std::move(callback));
}
void CallRemove(scoped_refptr<storage::FileSystemContext> file_system_context,
const storage::FileSystemURL& url,
bool recursive,
storage::FileSystemOperationRunner::StatusCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
file_system_context->operation_runner()->Remove(url, recursive,
std::move(callback));
}
void CallTouchFile(
scoped_refptr<storage::FileSystemContext> file_system_context,
const storage::FileSystemURL& url,
const base::Time& last_access_time,
const base::Time& last_modified_time,
storage::FileSystemOperationRunner::StatusCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
file_system_context->operation_runner()->TouchFile(
url, last_access_time, last_modified_time, std::move(callback));
}
void CallMove(scoped_refptr<storage::FileSystemContext> file_system_context,
const storage::FileSystemURL& src_path,
const storage::FileSystemURL& dest_path,
storage::FileSystemOperationRunner::CopyOrMoveOptionSet options,
storage::FileSystemOperationRunner::StatusCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
file_system_context->operation_runner()->Move(
src_path, dest_path, options,
storage::FileSystemOperation::ERROR_BEHAVIOR_ABORT,
std::make_unique<storage::CopyOrMoveHookDelegate>(), std::move(callback));
}
void CallGetMetadata(
scoped_refptr<storage::FileSystemContext> file_system_context,
const storage::FileSystemURL& url,
storage::FileSystemOperation::GetMetadataFieldSet fields,
storage::FileSystemOperationRunner::GetMetadataCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
file_system_context->operation_runner()->GetMetadata(url, fields,
std::move(callback));
}
} // namespace
PepperInternalFileRefBackend::PepperInternalFileRefBackend(
PpapiHost* host,
int render_process_id,
base::WeakPtr<PepperFileSystemBrowserHost> fs_host,
const std::string& path)
: host_(host),
render_process_id_(render_process_id),
fs_host_(fs_host),
fs_type_(fs_host->GetType()),
path_(path) {
ppapi::NormalizeInternalPath(&path_);
}
PepperInternalFileRefBackend::~PepperInternalFileRefBackend() {}
storage::FileSystemURL PepperInternalFileRefBackend::GetFileSystemURL() const {
if (!fs_url_.is_valid() && fs_host_.get() && fs_host_->IsOpened()) {
GURL fs_path =
fs_host_->GetRootUrl().Resolve(base::EscapePath(path_.substr(1)));
scoped_refptr<storage::FileSystemContext> fs_context =
GetFileSystemContext();
if (fs_context.get())
fs_url_ = fs_context->CrackURL(
fs_path,
blink::StorageKey::CreateFirstParty(url::Origin::Create(fs_path)));
}
return fs_url_;
}
base::FilePath PepperInternalFileRefBackend::GetExternalFilePath() const {
return base::FilePath();
}
scoped_refptr<storage::FileSystemContext>
PepperInternalFileRefBackend::GetFileSystemContext() const {
return PepperFileSystemBrowserHost::GetFileSystemContextFromRenderId(
render_process_id_);
}
void PepperInternalFileRefBackend::DidFinish(
ppapi::host::ReplyMessageContext context,
const IPC::Message& msg,
base::File::Error error) {
context.params.set_result(ppapi::FileErrorToPepperError(error));
host_->SendReply(context, msg);
}
void PepperInternalFileRefBackend::DidFinishOnIOThread(
base::WeakPtr<PepperInternalFileRefBackend> weak_ptr,
ppapi::host::ReplyMessageContext reply_context,
const IPC::Message& msg,
base::File::Error error) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
GetUIThreadTaskRunner({})->PostTask(
FROM_HERE, base::BindOnce(&PepperInternalFileRefBackend::DidFinish,
weak_ptr, reply_context, msg, error));
}
void PepperInternalFileRefBackend::ReadDirectoryCompleteOnIOThread(
base::WeakPtr<PepperInternalFileRefBackend> weak_ptr,
ppapi::host::ReplyMessageContext reply_context,
storage::FileSystemOperation::FileEntryList* accumulated_file_list,
base::File::Error error,
storage::FileSystemOperation::FileEntryList file_list,
bool has_more) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
// Don't insert the last |file_list| since it'll be added in the post task
// below.
if (has_more) {
accumulated_file_list->insert(accumulated_file_list->end(),
file_list.begin(), file_list.end());
return;
}
// If there are no more, this callback will be deleted and with it
// |accumulated_file_list| so make a new copy.
auto* accumulated_file_list2 =
new storage::FileSystemOperation::FileEntryList;
accumulated_file_list2->swap(*accumulated_file_list);
GetUIThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(&PepperInternalFileRefBackend::ReadDirectoryComplete,
weak_ptr, reply_context,
base::Owned(accumulated_file_list2), error, file_list,
false));
}
void PepperInternalFileRefBackend::GetMetadataCompleteOnIOThread(
base::WeakPtr<PepperInternalFileRefBackend> weak_ptr,
ppapi::host::ReplyMessageContext reply_context,
base::File::Error result,
const base::File::Info& file_info) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
GetUIThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(&PepperInternalFileRefBackend::GetMetadataComplete,
weak_ptr, reply_context, result, file_info));
}
int32_t PepperInternalFileRefBackend::MakeDirectory(
ppapi::host::ReplyMessageContext reply_context,
int32_t make_directory_flags) {
if (!GetFileSystemURL().is_valid())
return PP_ERROR_FAILED;
bool exclusive = !!(make_directory_flags & PP_MAKEDIRECTORYFLAG_EXCLUSIVE);
bool recursive =
!!(make_directory_flags & PP_MAKEDIRECTORYFLAG_WITH_ANCESTORS);
GetIOThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(
CallCreateDirectory, GetFileSystemContext(), GetFileSystemURL(),
exclusive, recursive,
base::BindOnce(&PepperInternalFileRefBackend::DidFinishOnIOThread,
weak_factory_.GetWeakPtr(), reply_context,
PpapiPluginMsg_FileRef_MakeDirectoryReply())));
return PP_OK_COMPLETIONPENDING;
}
int32_t PepperInternalFileRefBackend::Touch(
ppapi::host::ReplyMessageContext reply_context,
PP_Time last_access_time_in,
PP_Time last_modified_time_in) {
if (!GetFileSystemURL().is_valid())
return PP_ERROR_FAILED;
base::Time last_access_time = ppapi::PPTimeToTime(last_access_time_in);
base::Time last_modified_time = ppapi::PPTimeToTime(last_modified_time_in);
GetIOThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(
CallTouchFile, GetFileSystemContext(), GetFileSystemURL(),
last_access_time, last_modified_time,
base::BindOnce(&PepperInternalFileRefBackend::DidFinishOnIOThread,
weak_factory_.GetWeakPtr(), reply_context,
PpapiPluginMsg_FileRef_TouchReply())));
return PP_OK_COMPLETIONPENDING;
}
int32_t PepperInternalFileRefBackend::Delete(
ppapi::host::ReplyMessageContext reply_context) {
if (!GetFileSystemURL().is_valid())
return PP_ERROR_FAILED;
GetIOThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(
CallRemove, GetFileSystemContext(), GetFileSystemURL(), false,
base::BindOnce(&PepperInternalFileRefBackend::DidFinishOnIOThread,
weak_factory_.GetWeakPtr(), reply_context,
PpapiPluginMsg_FileRef_DeleteReply())));
return PP_OK_COMPLETIONPENDING;
}
int32_t PepperInternalFileRefBackend::Rename(
ppapi::host::ReplyMessageContext reply_context,
PepperFileRefHost* new_file_ref) {
if (!GetFileSystemURL().is_valid())
return PP_ERROR_FAILED;
storage::FileSystemURL new_url = new_file_ref->GetFileSystemURL();
if (!new_url.is_valid())
return PP_ERROR_FAILED;
if (!new_url.IsInSameFileSystem(GetFileSystemURL()))
return PP_ERROR_FAILED;
storage::FileSystemOperationRunner::CopyOrMoveOptionSet options =
storage::FileSystemOperation::CopyOrMoveOptionSet();
GetIOThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(
CallMove, GetFileSystemContext(), GetFileSystemURL(), new_url,
options,
base::BindOnce(&PepperInternalFileRefBackend::DidFinishOnIOThread,
weak_factory_.GetWeakPtr(), reply_context,
PpapiPluginMsg_FileRef_RenameReply())));
return PP_OK_COMPLETIONPENDING;
}
int32_t PepperInternalFileRefBackend::Query(
ppapi::host::ReplyMessageContext reply_context) {
if (!GetFileSystemURL().is_valid())
return PP_ERROR_FAILED;
constexpr storage::FileSystemOperation::GetMetadataFieldSet fields = {
storage::FileSystemOperation::GetMetadataField::kIsDirectory,
storage::FileSystemOperation::GetMetadataField::kSize,
storage::FileSystemOperation::GetMetadataField::kLastModified};
GetIOThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(
CallGetMetadata, GetFileSystemContext(), GetFileSystemURL(), fields,
base::BindRepeating(
&PepperInternalFileRefBackend::GetMetadataCompleteOnIOThread,
weak_factory_.GetWeakPtr(), reply_context)));
return PP_OK_COMPLETIONPENDING;
}
void PepperInternalFileRefBackend::GetMetadataComplete(
ppapi::host::ReplyMessageContext reply_context,
base::File::Error error,
const base::File::Info& file_info) {
reply_context.params.set_result(ppapi::FileErrorToPepperError(error));
PP_FileInfo pp_file_info;
if (error == base::File::FILE_OK) {
ppapi::FileInfoToPepperFileInfo(file_info, fs_type_, &pp_file_info);
} else {
UNSAFE_TODO(memset(&pp_file_info, 0, sizeof(pp_file_info)));
}
host_->SendReply(reply_context,
PpapiPluginMsg_FileRef_QueryReply(pp_file_info));
}
int32_t PepperInternalFileRefBackend::ReadDirectoryEntries(
ppapi::host::ReplyMessageContext reply_context) {
if (!GetFileSystemURL().is_valid())
return PP_ERROR_FAILED;
storage::FileSystemOperation::FileEntryList* accumulated_file_list =
new storage::FileSystemOperation::FileEntryList;
GetIOThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(
CallReadDirectory, GetFileSystemContext(), GetFileSystemURL(),
base::BindRepeating(
&PepperInternalFileRefBackend::ReadDirectoryCompleteOnIOThread,
weak_factory_.GetWeakPtr(), reply_context,
base::Owned(accumulated_file_list))));
return PP_OK_COMPLETIONPENDING;
}
void PepperInternalFileRefBackend::ReadDirectoryComplete(
ppapi::host::ReplyMessageContext context,
storage::FileSystemOperation::FileEntryList* accumulated_file_list,
base::File::Error error,
storage::FileSystemOperation::FileEntryList file_list,
bool has_more) {
accumulated_file_list->insert(accumulated_file_list->end(), file_list.begin(),
file_list.end());
if (has_more)
return;
context.params.set_result(ppapi::FileErrorToPepperError(error));
std::vector<ppapi::FileRefCreateInfo> infos;
std::vector<PP_FileType> file_types;
if (error == base::File::FILE_OK && fs_host_.get()) {
std::string dir_path = path_;
if (dir_path.empty() || dir_path.back() != '/')
dir_path += '/';
for (const auto& it : *accumulated_file_list) {
file_types.push_back(it.type == filesystem::mojom::FsFileType::DIRECTORY
? PP_FILETYPE_DIRECTORY
: PP_FILETYPE_REGULAR);
ppapi::FileRefCreateInfo info;
info.file_system_type = fs_type_;
info.file_system_plugin_resource = fs_host_->pp_resource();
std::string path = dir_path + storage::FilePathToString(it.name.path());
info.internal_path = path;
info.display_name = ppapi::GetNameForInternalFilePath(path);
infos.push_back(info);
}
}
host_->SendReply(context, PpapiPluginMsg_FileRef_ReadDirectoryEntriesReply(
infos, file_types));
}
int32_t PepperInternalFileRefBackend::GetAbsolutePath(
ppapi::host::ReplyMessageContext reply_context) {
host_->SendReply(reply_context,
PpapiPluginMsg_FileRef_GetAbsolutePathReply(path_));
return PP_OK_COMPLETIONPENDING;
}
int32_t PepperInternalFileRefBackend::CanRead() const {
storage::FileSystemURL url = GetFileSystemURL();
if (!FileSystemURLIsValid(GetFileSystemContext().get(), url))
return PP_ERROR_FAILED;
if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFileSystemFile(
render_process_id_, url)) {
return PP_ERROR_NOACCESS;
}
return PP_OK;
}
int32_t PepperInternalFileRefBackend::CanWrite() const {
storage::FileSystemURL url = GetFileSystemURL();
if (!FileSystemURLIsValid(GetFileSystemContext().get(), url))
return PP_ERROR_FAILED;
if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanWriteFileSystemFile(
render_process_id_, url)) {
return PP_ERROR_NOACCESS;
}
return PP_OK;
}
int32_t PepperInternalFileRefBackend::CanCreate() const {
storage::FileSystemURL url = GetFileSystemURL();
if (!FileSystemURLIsValid(GetFileSystemContext().get(), url))
return PP_ERROR_FAILED;
if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanCreateFileSystemFile(
render_process_id_, url)) {
return PP_ERROR_NOACCESS;
}
return PP_OK;
}
int32_t PepperInternalFileRefBackend::CanReadWrite() const {
storage::FileSystemURL url = GetFileSystemURL();
if (!FileSystemURLIsValid(GetFileSystemContext().get(), url))
return PP_ERROR_FAILED;
ChildProcessSecurityPolicyImpl* policy =
ChildProcessSecurityPolicyImpl::GetInstance();
if (!policy->CanReadFileSystemFile(render_process_id_, url) ||
!policy->CanWriteFileSystemFile(render_process_id_, url)) {
return PP_ERROR_NOACCESS;
}
return PP_OK;
}
} // namespace content
|