Fix playing of 8-bit sample files via webaudio.
[chromium-blink-merge.git] / webkit / glue / webfileutilities_impl.cc
blob555003f1d7cda5edf9642fd3d9d839fcf5f9001d
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) {
28 NOTREACHED();
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) {
37 NOTREACHED();
38 return false;
41 bool WebFileUtilitiesImpl::deleteEmptyDirectory(const WebString& path) {
42 NOTREACHED();
43 return false;
46 bool WebFileUtilitiesImpl::getFileSize(const WebString& path,
47 long long& result) {
48 if (sandbox_enabled_) {
49 NOTREACHED();
50 return false;
52 return file_util::GetFileSize(WebStringToFilePath(path),
53 reinterpret_cast<int64*>(&result));
56 bool WebFileUtilitiesImpl::getFileModificationTime(const WebString& path,
57 double& result) {
58 if (sandbox_enabled_) {
59 NOTREACHED();
60 return false;
62 base::PlatformFileInfo info;
63 if (!file_util::GetFileInfo(WebStringToFilePath(path), &info))
64 return false;
65 result = info.last_modified.ToDoubleT();
66 return true;
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,
105 int mode) {
106 if (sandbox_enabled_) {
107 NOTREACHED();
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),
115 NULL, NULL);
118 void WebFileUtilitiesImpl::closeFile(base::PlatformFile& handle) {
119 if (handle == base::kInvalidPlatformFileValue)
120 return;
121 if (base::ClosePlatformFile(handle))
122 handle = base::kInvalidPlatformFileValue;
125 long 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);
134 bool 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;
142 int 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, net::CompletionCallback());
153 int 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, net::CompletionCallback());
162 } // namespace webkit_glue