1 // Copyright (c) 2012 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 "webkit/glue/webfileutilities_impl.h"
7 #include "base/file_util.h"
8 #include "base/files/file_path.h"
9 #include "base/logging.h"
10 #include "net/base/file_stream.h"
11 #include "net/base/net_util.h"
12 #include "third_party/WebKit/public/platform/WebFileInfo.h"
13 #include "third_party/WebKit/public/platform/WebString.h"
14 #include "third_party/WebKit/public/platform/WebURL.h"
15 #include "webkit/glue/webkit_glue.h"
17 using WebKit::WebString
;
19 namespace webkit_glue
{
21 WebFileUtilitiesImpl::WebFileUtilitiesImpl()
22 : sandbox_enabled_(true) {
25 WebFileUtilitiesImpl::~WebFileUtilitiesImpl() {
28 bool WebFileUtilitiesImpl::fileExists(const WebString
& path
) {
29 return file_util::PathExists(base::FilePath::FromUTF16Unsafe(path
));
32 bool WebFileUtilitiesImpl::deleteFile(const WebString
& path
) {
37 bool WebFileUtilitiesImpl::deleteEmptyDirectory(const WebString
& path
) {
42 bool WebFileUtilitiesImpl::getFileInfo(const WebString
& path
,
43 WebKit::WebFileInfo
& web_file_info
) {
44 if (sandbox_enabled_
) {
48 base::PlatformFileInfo file_info
;
49 if (!file_util::GetFileInfo(base::FilePath::FromUTF16Unsafe(path
),
53 webkit_glue::PlatformFileInfoToWebFileInfo(file_info
, &web_file_info
);
54 web_file_info
.platformPath
= path
;
58 WebString
WebFileUtilitiesImpl::directoryName(const WebString
& path
) {
59 return base::FilePath::FromUTF16Unsafe(path
).DirName().AsUTF16Unsafe();
62 WebString
WebFileUtilitiesImpl::pathByAppendingComponent(
63 const WebString
& webkit_path
,
64 const WebString
& webkit_component
) {
65 base::FilePath
path(base::FilePath::FromUTF16Unsafe(webkit_path
));
66 base::FilePath
component(base::FilePath::FromUTF16Unsafe(webkit_component
));
67 base::FilePath combined_path
= path
.Append(component
);
68 return combined_path
.AsUTF16Unsafe();
71 bool WebFileUtilitiesImpl::makeAllDirectories(const WebString
& path
) {
72 DCHECK(!sandbox_enabled_
);
73 return file_util::CreateDirectory(base::FilePath::FromUTF16Unsafe(path
));
76 bool WebFileUtilitiesImpl::isDirectory(const WebString
& path
) {
77 return file_util::DirectoryExists(base::FilePath::FromUTF16Unsafe(path
));
80 WebKit::WebURL
WebFileUtilitiesImpl::filePathToURL(const WebString
& path
) {
81 return net::FilePathToFileURL(base::FilePath::FromUTF16Unsafe(path
));
84 base::PlatformFile
WebFileUtilitiesImpl::openFile(const WebString
& path
,
86 if (sandbox_enabled_
) {
88 return base::kInvalidPlatformFileValue
;
90 return base::CreatePlatformFile(
91 base::FilePath::FromUTF16Unsafe(path
),
92 (mode
== 0) ? (base::PLATFORM_FILE_OPEN
| base::PLATFORM_FILE_READ
)
93 : (base::PLATFORM_FILE_CREATE_ALWAYS
|
94 base::PLATFORM_FILE_WRITE
),
98 void WebFileUtilitiesImpl::closeFile(base::PlatformFile
& handle
) {
99 if (handle
== base::kInvalidPlatformFileValue
)
101 if (base::ClosePlatformFile(handle
))
102 handle
= base::kInvalidPlatformFileValue
;
105 long long WebFileUtilitiesImpl::seekFile(base::PlatformFile handle
,
108 if (handle
== base::kInvalidPlatformFileValue
)
110 return base::SeekPlatformFile(handle
,
111 static_cast<base::PlatformFileWhence
>(origin
),
115 bool WebFileUtilitiesImpl::truncateFile(base::PlatformFile handle
,
117 if (handle
== base::kInvalidPlatformFileValue
|| offset
< 0)
119 return base::TruncatePlatformFile(handle
, offset
);
122 int WebFileUtilitiesImpl::readFromFile(base::PlatformFile handle
,
125 if (handle
== base::kInvalidPlatformFileValue
|| !data
|| length
<= 0)
127 return base::ReadPlatformFileCurPosNoBestEffort(handle
, data
, length
);
130 int WebFileUtilitiesImpl::writeToFile(base::PlatformFile handle
,
133 if (handle
== base::kInvalidPlatformFileValue
|| !data
|| length
<= 0)
135 return base::WritePlatformFileCurPosNoBestEffort(handle
, data
, length
);
138 } // namespace webkit_glue