1 // Copyright (c) 2011 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_path.h"
8 #include "base/file_util.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/Source/WebKit/chromium/public/WebString.h"
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h"
14 #include "webkit/glue/webkit_glue.h"
16 using WebKit::WebString
;
18 namespace webkit_glue
{
20 WebFileUtilitiesImpl::WebFileUtilitiesImpl()
21 : sandbox_enabled_(true) {
24 WebFileUtilitiesImpl::~WebFileUtilitiesImpl() {
27 void WebFileUtilitiesImpl::revealFolderInOS(const WebString
& path
) {
31 bool WebFileUtilitiesImpl::fileExists(const WebString
& path
) {
32 FilePath::StringType file_path
= WebStringToFilePathString(path
);
33 return file_util::PathExists(FilePath(file_path
));
36 bool WebFileUtilitiesImpl::deleteFile(const WebString
& path
) {
41 bool WebFileUtilitiesImpl::deleteEmptyDirectory(const WebString
& path
) {
46 bool WebFileUtilitiesImpl::getFileSize(const WebString
& path
,
48 if (sandbox_enabled_
) {
52 return file_util::GetFileSize(WebStringToFilePath(path
),
53 reinterpret_cast<int64
*>(&result
));
56 bool WebFileUtilitiesImpl::getFileModificationTime(const WebString
& path
,
58 if (sandbox_enabled_
) {
62 base::PlatformFileInfo info
;
63 if (!file_util::GetFileInfo(WebStringToFilePath(path
), &info
))
65 result
= info
.last_modified
.ToDoubleT();
69 WebString
WebFileUtilitiesImpl::directoryName(const WebString
& path
) {
70 FilePath
file_path(WebStringToFilePathString(path
));
71 return FilePathToWebString(file_path
.DirName());
74 WebString
WebFileUtilitiesImpl::pathByAppendingComponent(
75 const WebString
& webkit_path
,
76 const WebString
& webkit_component
) {
77 FilePath
path(WebStringToFilePathString(webkit_path
));
78 FilePath
component(WebStringToFilePathString(webkit_component
));
79 FilePath combined_path
= path
.Append(component
);
80 return FilePathStringToWebString(combined_path
.value());
83 bool WebFileUtilitiesImpl::makeAllDirectories(const WebString
& path
) {
84 DCHECK(!sandbox_enabled_
);
85 FilePath::StringType file_path
= WebStringToFilePathString(path
);
86 return file_util::CreateDirectory(FilePath(file_path
));
89 WebString
WebFileUtilitiesImpl::getAbsolutePath(const WebString
& path
) {
90 FilePath
file_path(WebStringToFilePathString(path
));
91 file_util::AbsolutePath(&file_path
);
92 return FilePathStringToWebString(file_path
.value());
95 bool WebFileUtilitiesImpl::isDirectory(const WebString
& path
) {
96 FilePath
file_path(WebStringToFilePathString(path
));
97 return file_util::DirectoryExists(file_path
);
100 WebKit::WebURL
WebFileUtilitiesImpl::filePathToURL(const WebString
& path
) {
101 return net::FilePathToFileURL(WebStringToFilePath(path
));
104 base::PlatformFile
WebFileUtilitiesImpl::openFile(const WebString
& path
,
106 if (sandbox_enabled_
) {
108 return base::kInvalidPlatformFileValue
;
110 return base::CreatePlatformFile(
111 WebStringToFilePath(path
),
112 (mode
== 0) ? (base::PLATFORM_FILE_OPEN
| base::PLATFORM_FILE_READ
)
113 : (base::PLATFORM_FILE_CREATE_ALWAYS
|
114 base::PLATFORM_FILE_WRITE
),
118 void WebFileUtilitiesImpl::closeFile(base::PlatformFile
& handle
) {
119 if (handle
== base::kInvalidPlatformFileValue
)
121 if (base::ClosePlatformFile(handle
))
122 handle
= base::kInvalidPlatformFileValue
;
125 long long WebFileUtilitiesImpl::seekFile(base::PlatformFile handle
,
128 if (handle
== base::kInvalidPlatformFileValue
)
130 net::FileStream
file_stream(handle
, 0);
131 return file_stream
.Seek(static_cast<net::Whence
>(origin
), offset
);
134 bool WebFileUtilitiesImpl::truncateFile(base::PlatformFile handle
,
136 if (handle
== base::kInvalidPlatformFileValue
|| offset
< 0)
138 net::FileStream
file_stream(handle
, base::PLATFORM_FILE_WRITE
);
139 return file_stream
.Truncate(offset
) >= 0;
142 int WebFileUtilitiesImpl::readFromFile(base::PlatformFile handle
,
145 if (handle
== base::kInvalidPlatformFileValue
|| !data
|| length
<= 0)
148 buffer
.resize(length
);
149 net::FileStream
file_stream(handle
, base::PLATFORM_FILE_READ
);
150 return file_stream
.Read(data
, length
, net::CompletionCallback());
153 int WebFileUtilitiesImpl::writeToFile(base::PlatformFile handle
,
156 if (handle
== base::kInvalidPlatformFileValue
|| !data
|| length
<= 0)
158 net::FileStream
file_stream(handle
, base::PLATFORM_FILE_WRITE
);
159 return file_stream
.Write(data
, length
, net::CompletionCallback());
162 } // namespace webkit_glue