1 // Copyright 2013 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.
5 #include "content/browser/renderer_host/pepper/pepper_security_helper.h"
7 #include "base/logging.h"
8 #include "content/browser/child_process_security_policy_impl.h"
9 #include "ppapi/c/ppb_file_io.h"
15 template <typename CanRead
,
18 typename CanCreateReadWrite
,
20 bool CanOpenFileWithPepperFlags(CanRead can_read
,
23 CanCreateReadWrite can_create_read_write
,
27 ChildProcessSecurityPolicyImpl
* policy
=
28 ChildProcessSecurityPolicyImpl::GetInstance();
30 bool pp_read
= !!(pp_open_flags
& PP_FILEOPENFLAG_READ
);
31 bool pp_write
= !!(pp_open_flags
& PP_FILEOPENFLAG_WRITE
);
32 bool pp_create
= !!(pp_open_flags
& PP_FILEOPENFLAG_CREATE
);
33 bool pp_truncate
= !!(pp_open_flags
& PP_FILEOPENFLAG_TRUNCATE
);
34 bool pp_exclusive
= !!(pp_open_flags
& PP_FILEOPENFLAG_EXCLUSIVE
);
35 bool pp_append
= !!(pp_open_flags
& PP_FILEOPENFLAG_APPEND
);
37 if (pp_read
&& !(policy
->*can_read
)(child_id
, file
))
40 if (pp_write
&& !(policy
->*can_write
)(child_id
, file
))
43 // TODO(tommycli): Maybe tighten up required permission. crbug.com/284792
44 if (pp_append
&& !(policy
->*can_create_read_write
)(child_id
, file
))
47 if (pp_truncate
&& !pp_write
)
52 return (policy
->*can_create
)(child_id
, file
);
54 // Asks for too much, but this is the only grant that allows overwrite.
55 return (policy
->*can_create_read_write
)(child_id
, file
);
57 } else if (pp_truncate
) {
58 return (policy
->*can_create_read_write
)(child_id
, file
);
65 bool CanOpenWithPepperFlags(int pp_open_flags
,
67 const base::FilePath
& file
) {
68 return CanOpenFileWithPepperFlags(
69 &ChildProcessSecurityPolicyImpl::CanReadFile
,
70 &ChildProcessSecurityPolicyImpl::CanCreateReadWriteFile
,
71 &ChildProcessSecurityPolicyImpl::CanCreateReadWriteFile
,
72 &ChildProcessSecurityPolicyImpl::CanCreateReadWriteFile
,
78 bool CanOpenFileSystemURLWithPepperFlags(int pp_open_flags
,
80 const fileapi::FileSystemURL
& url
) {
81 return CanOpenFileWithPepperFlags(
82 &ChildProcessSecurityPolicyImpl::CanReadFileSystemFile
,
83 &ChildProcessSecurityPolicyImpl::CanWriteFileSystemFile
,
84 &ChildProcessSecurityPolicyImpl::CanCreateFileSystemFile
,
85 &ChildProcessSecurityPolicyImpl::CanCreateReadWriteFileSystemFile
,
91 } // namespace content