1// Copyright (c) 2010 The Chromium Authors. All rights reserved.  Use of this
2// source code is governed by a BSD-style license that can be found in the
3// LICENSE file.
4
5#include "webkit/glue/webfileutilities_impl.h"
6
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"
15
16using WebKit::WebString;
17
18namespace webkit_glue {
19
20WebFileUtilitiesImpl::WebFileUtilitiesImpl()
21    : sandbox_enabled_(true) {
22}
23
24WebFileUtilitiesImpl::~WebFileUtilitiesImpl() {
25}
26
27void WebFileUtilitiesImpl::revealFolderInOS(const WebString& path) {
28  NOTREACHED();
29}
30
31bool WebFileUtilitiesImpl::fileExists(const WebString& path) {
32  FilePath::StringType file_path = WebStringToFilePathString(path);
33  return file_util::PathExists(FilePath(file_path));
34}
35
36bool WebFileUtilitiesImpl::deleteFile(const WebString& path) {
37  NOTREACHED();
38  return false;
39}
40
41bool WebFileUtilitiesImpl::deleteEmptyDirectory(const WebString& path) {
42  NOTREACHED();
43  return false;
44}
45
46bool WebFileUtilitiesImpl::getFileSize(const WebString& path,
47                                       long long& result) {
48  if (sandbox_enabled_) {
49    NOTREACHED();
50    return false;
51  }
52  return file_util::GetFileSize(WebStringToFilePath(path),
53                                reinterpret_cast<int64*>(&result));
54}
55
56bool WebFileUtilitiesImpl::getFileModificationTime(const WebString& path,
57                                                   double& result) {
58  if (sandbox_enabled_) {
59    NOTREACHED();
60    return false;
61  }
62  base::PlatformFileInfo info;
63  if (!file_util::GetFileInfo(WebStringToFilePath(path), &info))
64    return false;
65  result = info.last_modified.ToDoubleT();
66  return true;
67}
68
69WebString WebFileUtilitiesImpl::directoryName(const WebString& path) {
70  FilePath file_path(WebStringToFilePathString(path));
71  return FilePathToWebString(file_path.DirName());
72}
73
74WebString 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());
81}
82
83bool WebFileUtilitiesImpl::makeAllDirectories(const WebString& path) {
84  DCHECK(!sandbox_enabled_);
85  FilePath::StringType file_path = WebStringToFilePathString(path);
86  return file_util::CreateDirectory(FilePath(file_path));
87}
88
89WebString WebFileUtilitiesImpl::getAbsolutePath(const WebString& path) {
90  FilePath file_path(WebStringToFilePathString(path));
91  file_util::AbsolutePath(&file_path);
92  return FilePathStringToWebString(file_path.value());
93}
94
95bool WebFileUtilitiesImpl::isDirectory(const WebString& path) {
96  FilePath file_path(WebStringToFilePathString(path));
97  return file_util::DirectoryExists(file_path);
98}
99
100WebKit::WebURL WebFileUtilitiesImpl::filePathToURL(const WebString& path) {
101  return net::FilePathToFileURL(WebStringToFilePath(path));
102}
103
104base::PlatformFile WebFileUtilitiesImpl::openFile(const WebString& path,
105                                                  int mode) {
106  if (sandbox_enabled_) {
107    NOTREACHED();
108    return base::kInvalidPlatformFileValue;
109  }
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),
115      NULL, NULL);
116}
117
118void WebFileUtilitiesImpl::closeFile(base::PlatformFile& handle) {
119  if (handle == base::kInvalidPlatformFileValue)
120    return;
121  if (base::ClosePlatformFile(handle))
122    handle = base::kInvalidPlatformFileValue;
123}
124
125long long WebFileUtilitiesImpl::seekFile(base::PlatformFile handle,
126                                         long long offset,
127                                         int origin) {
128  if (handle == base::kInvalidPlatformFileValue)
129    return -1;
130  net::FileStream file_stream(handle, 0);
131  return file_stream.Seek(static_cast<net::Whence>(origin), offset);
132}
133
134bool WebFileUtilitiesImpl::truncateFile(base::PlatformFile handle,
135                                        long long offset) {
136  if (handle == base::kInvalidPlatformFileValue || offset < 0)
137    return false;
138  net::FileStream file_stream(handle, base::PLATFORM_FILE_WRITE);
139  return file_stream.Truncate(offset) >= 0;
140}
141
142int WebFileUtilitiesImpl::readFromFile(base::PlatformFile handle,
143                                       char* data,
144                                       int length) {
145  if (handle == base::kInvalidPlatformFileValue || !data || length <= 0)
146    return -1;
147  std::string buffer;
148  buffer.resize(length);
149  net::FileStream file_stream(handle, base::PLATFORM_FILE_READ);
150  return file_stream.Read(data, length, NULL);
151}
152
153int WebFileUtilitiesImpl::writeToFile(base::PlatformFile handle,
154                                      const char* data,
155                                      int length) {
156  if (handle == base::kInvalidPlatformFileValue || !data || length <= 0)
157    return -1;
158  net::FileStream file_stream(handle, base::PLATFORM_FILE_WRITE);
159  return file_stream.Write(data, length, NULL);
160}
161
162}  // namespace webkit_glue
163