Move functions for controlling Caps Lock to CapsLockDelegate from SystemTrayDelegate.
[chromium-blink-merge.git] / base / platform_file.h
bloba9708f4fa57bfe8d56842dcad63643dcef0a7010
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 #ifndef BASE_PLATFORM_FILE_H_
6 #define BASE_PLATFORM_FILE_H_
8 #include "build/build_config.h"
9 #if defined(OS_WIN)
10 #include <windows.h>
11 #endif
13 #include <string>
15 #include "base/base_export.h"
16 #include "base/basictypes.h"
17 #include "base/file_path.h"
18 #include "base/time.h"
20 namespace base {
22 #if defined(OS_WIN)
23 typedef HANDLE PlatformFile;
24 const PlatformFile kInvalidPlatformFileValue = INVALID_HANDLE_VALUE;
25 #elif defined(OS_POSIX)
26 typedef int PlatformFile;
27 const PlatformFile kInvalidPlatformFileValue = -1;
28 #endif
30 // PLATFORM_FILE_(OPEN|CREATE).* are mutually exclusive. You should specify
31 // exactly one of the five (possibly combining with other flags) when opening
32 // or creating a file.
33 enum PlatformFileFlags {
34 PLATFORM_FILE_OPEN = 1 << 0, // Opens a file, only if it exists.
35 PLATFORM_FILE_CREATE = 1 << 1, // Creates a new file, only if it
36 // does not already exist.
37 PLATFORM_FILE_OPEN_ALWAYS = 1 << 2, // May create a new file.
38 PLATFORM_FILE_CREATE_ALWAYS = 1 << 3, // May overwrite an old file.
39 PLATFORM_FILE_OPEN_TRUNCATED = 1 << 4, // Opens a file and truncates it,
40 // only if it exists.
41 PLATFORM_FILE_READ = 1 << 5,
42 PLATFORM_FILE_WRITE = 1 << 6,
43 PLATFORM_FILE_EXCLUSIVE_READ = 1 << 7, // EXCLUSIVE is opposite of Windows
44 // SHARE
45 PLATFORM_FILE_EXCLUSIVE_WRITE = 1 << 8,
46 PLATFORM_FILE_ASYNC = 1 << 9,
47 PLATFORM_FILE_TEMPORARY = 1 << 10, // Used on Windows only
48 PLATFORM_FILE_HIDDEN = 1 << 11, // Used on Windows only
49 PLATFORM_FILE_DELETE_ON_CLOSE = 1 << 12,
51 PLATFORM_FILE_WRITE_ATTRIBUTES = 1 << 13, // Used on Windows only
52 PLATFORM_FILE_ENUMERATE = 1 << 14, // May enumerate directory
54 PLATFORM_FILE_SHARE_DELETE = 1 << 15, // Used on Windows only
56 PLATFORM_FILE_TERMINAL_DEVICE = 1 << 16, // Serial port flags
59 // PLATFORM_FILE_ERROR_ACCESS_DENIED is returned when a call fails because of
60 // a filesystem restriction. PLATFORM_FILE_ERROR_SECURITY is returned when a
61 // browser policy doesn't allow the operation to be executed.
62 enum PlatformFileError {
63 PLATFORM_FILE_OK = 0,
64 PLATFORM_FILE_ERROR_FAILED = -1,
65 PLATFORM_FILE_ERROR_IN_USE = -2,
66 PLATFORM_FILE_ERROR_EXISTS = -3,
67 PLATFORM_FILE_ERROR_NOT_FOUND = -4,
68 PLATFORM_FILE_ERROR_ACCESS_DENIED = -5,
69 PLATFORM_FILE_ERROR_TOO_MANY_OPENED = -6,
70 PLATFORM_FILE_ERROR_NO_MEMORY = -7,
71 PLATFORM_FILE_ERROR_NO_SPACE = -8,
72 PLATFORM_FILE_ERROR_NOT_A_DIRECTORY = -9,
73 PLATFORM_FILE_ERROR_INVALID_OPERATION = -10,
74 PLATFORM_FILE_ERROR_SECURITY = -11,
75 PLATFORM_FILE_ERROR_ABORT = -12,
76 PLATFORM_FILE_ERROR_NOT_A_FILE = -13,
77 PLATFORM_FILE_ERROR_NOT_EMPTY = -14,
78 PLATFORM_FILE_ERROR_INVALID_URL = -15,
81 // Used to hold information about a given file.
82 // If you add more fields to this structure (platform-specific fields are OK),
83 // make sure to update all functions that use it in file_util_{win|posix}.cc
84 // too, and the ParamTraits<base::PlatformFileInfo> implementation in
85 // chrome/common/common_param_traits.cc.
86 struct BASE_EXPORT PlatformFileInfo {
87 PlatformFileInfo();
88 ~PlatformFileInfo();
90 // The size of the file in bytes. Undefined when is_directory is true.
91 int64 size;
93 // True if the file corresponds to a directory.
94 bool is_directory;
96 // True if the file corresponds to a symbolic link.
97 bool is_symbolic_link;
99 // The last modified time of a file.
100 base::Time last_modified;
102 // The last accessed time of a file.
103 base::Time last_accessed;
105 // The creation time of a file.
106 base::Time creation_time;
109 // Creates or opens the given file. If |created| is provided, it will be set to
110 // true if a new file was created [or an old one truncated to zero length to
111 // simulate a new file, which can happen with PLATFORM_FILE_CREATE_ALWAYS], and
112 // false otherwise. |error_code| can be NULL.
113 BASE_EXPORT PlatformFile CreatePlatformFile(const FilePath& name,
114 int flags,
115 bool* created,
116 PlatformFileError* error_code);
118 // Closes a file handle. Returns |true| on success and |false| otherwise.
119 BASE_EXPORT bool ClosePlatformFile(PlatformFile file);
121 // Reads the given number of bytes (or until EOF is reached) starting with the
122 // given offset. Returns the number of bytes read, or -1 on error. Note that
123 // this function makes a best effort to read all data on all platforms, so it is
124 // not intended for stream oriented files but instead for cases when the normal
125 // expectation is that actually |size| bytes are read unless there is an error.
126 BASE_EXPORT int ReadPlatformFile(PlatformFile file, int64 offset,
127 char* data, int size);
129 // Same as above but without seek.
130 BASE_EXPORT int ReadPlatformFileAtCurrentPos(PlatformFile file,
131 char* data, int size);
133 // Reads the given number of bytes (or until EOF is reached) starting with the
134 // given offset, but does not make any effort to read all data on all platforms.
135 // Returns the number of bytes read, or -1 on error.
136 BASE_EXPORT int ReadPlatformFileNoBestEffort(PlatformFile file, int64 offset,
137 char* data, int size);
139 // Writes the given buffer into the file at the given offset, overwritting any
140 // data that was previously there. Returns the number of bytes written, or -1
141 // on error. Note that this function makes a best effort to write all data on
142 // all platforms.
143 BASE_EXPORT int WritePlatformFile(PlatformFile file, int64 offset,
144 const char* data, int size);
146 // Save as above but without seek.
147 BASE_EXPORT int WritePlatformFileAtCurrentPos(PlatformFile file,
148 const char* data, int size);
150 // Truncates the given file to the given length. If |length| is greater than
151 // the current size of the file, the file is extended with zeros. If the file
152 // doesn't exist, |false| is returned.
153 BASE_EXPORT bool TruncatePlatformFile(PlatformFile file, int64 length);
155 // Flushes the buffers of the given file.
156 BASE_EXPORT bool FlushPlatformFile(PlatformFile file);
158 // Touches the given file.
159 BASE_EXPORT bool TouchPlatformFile(PlatformFile file,
160 const Time& last_access_time,
161 const Time& last_modified_time);
163 // Returns some information for the given file.
164 BASE_EXPORT bool GetPlatformFileInfo(PlatformFile file, PlatformFileInfo* info);
166 // Use this class to pass ownership of a PlatformFile to a receiver that may or
167 // may not want to accept it. This class does not own the storage for the
168 // PlatformFile.
170 // EXAMPLE:
172 // void MaybeProcessFile(PassPlatformFile pass_file) {
173 // if (...) {
174 // PlatformFile file = pass_file.ReleaseValue();
175 // // Now, we are responsible for closing |file|.
176 // }
177 // }
179 // void OpenAndMaybeProcessFile(const FilePath& path) {
180 // PlatformFile file = CreatePlatformFile(path, ...);
181 // MaybeProcessFile(PassPlatformFile(&file));
182 // if (file != kInvalidPlatformFileValue)
183 // ClosePlatformFile(file);
184 // }
186 class BASE_EXPORT PassPlatformFile {
187 public:
188 explicit PassPlatformFile(PlatformFile* value) : value_(value) {
191 // Called to retrieve the PlatformFile stored in this object. The caller
192 // gains ownership of the PlatformFile and is now responsible for closing it.
193 // Any subsequent calls to this method will return an invalid PlatformFile.
194 PlatformFile ReleaseValue() {
195 PlatformFile temp = *value_;
196 *value_ = kInvalidPlatformFileValue;
197 return temp;
200 private:
201 PlatformFile* value_;
204 } // namespace base
206 #endif // BASE_PLATFORM_FILE_H_