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::getFileInfo(const WebString
& path
,
29 WebKit::WebFileInfo
& web_file_info
) {
30 if (sandbox_enabled_
) {
34 base::PlatformFileInfo file_info
;
35 if (!file_util::GetFileInfo(base::FilePath::FromUTF16Unsafe(path
),
39 webkit_glue::PlatformFileInfoToWebFileInfo(file_info
, &web_file_info
);
40 web_file_info
.platformPath
= path
;
44 WebString
WebFileUtilitiesImpl::directoryName(const WebString
& path
) {
45 return base::FilePath::FromUTF16Unsafe(path
).DirName().AsUTF16Unsafe();
48 WebString
WebFileUtilitiesImpl::baseName(const WebString
& path
) {
49 return base::FilePath::FromUTF16Unsafe(path
).BaseName().AsUTF16Unsafe();
52 WebKit::WebURL
WebFileUtilitiesImpl::filePathToURL(const WebString
& path
) {
53 return net::FilePathToFileURL(base::FilePath::FromUTF16Unsafe(path
));
56 base::PlatformFile
WebFileUtilitiesImpl::openFile(const WebString
& path
,
58 if (sandbox_enabled_
) {
60 return base::kInvalidPlatformFileValue
;
62 // mode==0 (read-only) is the only supported mode.
63 // TODO(kinuko): Remove this parameter.
65 return base::CreatePlatformFile(
66 base::FilePath::FromUTF16Unsafe(path
),
67 base::PLATFORM_FILE_OPEN
| base::PLATFORM_FILE_READ
,
71 void WebFileUtilitiesImpl::closeFile(base::PlatformFile
& handle
) {
72 if (handle
== base::kInvalidPlatformFileValue
)
74 if (base::ClosePlatformFile(handle
))
75 handle
= base::kInvalidPlatformFileValue
;
78 int WebFileUtilitiesImpl::readFromFile(base::PlatformFile handle
,
81 if (handle
== base::kInvalidPlatformFileValue
|| !data
|| length
<= 0)
83 return base::ReadPlatformFileCurPosNoBestEffort(handle
, data
, length
);
86 } // namespace webkit_glue