OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/files/file.h" | 5 #include "base/files/file.h" |
6 #include "base/files/file_path.h" | 6 #include "base/files/file_path.h" |
7 #include "base/metrics/histogram.h" | 7 #include "base/metrics/histogram.h" |
8 #include "base/timer/elapsed_timer.h" | 8 #include "base/timer/elapsed_timer.h" |
9 #include "base/trace_event/trace_event.h" | |
9 | 10 |
10 #if defined(OS_POSIX) | 11 #if defined(OS_POSIX) |
11 #include "base/files/file_posix_hooks_internal.h" | 12 #include "base/files/file_posix_hooks_internal.h" |
12 #endif | 13 #endif |
13 | 14 |
14 namespace base { | 15 namespace base { |
15 | 16 |
16 File::Info::Info() | 17 File::Info::Info() |
17 : size(0), | 18 : size(0), |
18 is_directory(false), | 19 is_directory(false), |
19 is_symbolic_link(false) { | 20 is_symbolic_link(false) { |
20 } | 21 } |
21 | 22 |
22 File::Info::~Info() { | 23 File::Info::~Info() { |
23 } | 24 } |
24 | 25 |
25 File::File() | 26 File::File() |
26 : error_details_(FILE_ERROR_FAILED), | 27 : error_details_(FILE_ERROR_FAILED), |
27 created_(false), | 28 created_(false), |
28 async_(false) { | 29 async_(false) { |
29 } | 30 } |
30 | 31 |
31 #if !defined(OS_NACL) | 32 #if !defined(OS_NACL) |
32 File::File(const FilePath& name, uint32 flags) | 33 File::File(const FilePath& path, uint32 flags) |
33 : error_details_(FILE_OK), | 34 : error_details_(FILE_OK), |
34 created_(false), | 35 created_(false), |
35 async_(false) { | 36 async_(false) { |
36 Initialize(name, flags); | 37 Initialize(path, flags); |
37 } | 38 } |
38 #endif | 39 #endif |
39 | 40 |
40 File::File(PlatformFile platform_file) | 41 File::File(PlatformFile platform_file) |
41 : file_(platform_file), | 42 : file_(platform_file), |
42 error_details_(FILE_OK), | 43 error_details_(FILE_OK), |
43 created_(false), | 44 created_(false), |
44 async_(false) { | 45 async_(false) { |
45 #if defined(OS_POSIX) | 46 #if defined(OS_POSIX) |
46 DCHECK_GE(platform_file, -1); | 47 DCHECK_GE(platform_file, -1); |
47 if (IsValid()) | 48 if (IsValid()) |
48 ProtectFileDescriptor(platform_file); | 49 ProtectFileDescriptor(platform_file); |
49 #endif | 50 #endif |
50 } | 51 } |
51 | 52 |
52 File::File(Error error_details) | 53 File::File(Error error_details) |
53 : error_details_(error_details), | 54 : error_details_(error_details), |
54 created_(false), | 55 created_(false), |
55 async_(false) { | 56 async_(false) { |
56 } | 57 } |
57 | 58 |
58 File::File(RValue other) | 59 File::File(RValue other) |
59 : file_(other.object->TakePlatformFile()), | 60 : file_(other.object->TakePlatformFile()), |
61 path_(other.object->path_), | |
60 error_details_(other.object->error_details()), | 62 error_details_(other.object->error_details()), |
61 created_(other.object->created()), | 63 created_(other.object->created()), |
62 async_(other.object->async_) { | 64 async_(other.object->async_) { |
63 #if defined(OS_POSIX) | 65 #if defined(OS_POSIX) |
64 if (IsValid()) | 66 if (IsValid()) |
65 ProtectFileDescriptor(GetPlatformFile()); | 67 ProtectFileDescriptor(GetPlatformFile()); |
66 #endif | 68 #endif |
67 } | 69 } |
68 | 70 |
69 File::~File() { | 71 File::~File() { |
70 // Go through the AssertIOAllowed logic. | 72 // Go through the AssertIOAllowed logic. |
71 Close(); | 73 Close(); |
74 TRACE_EVENT_ASYNC_END1(kTraceGroup, "File", this, | |
Lei Zhang
2015/04/27 22:55:55
We may not always call TRACE_EVENT_ASYNC_BEGIN1()
Dan Beam
2015/04/28 03:17:11
not sure but just made this TRACE_EVENT_ASYNC_{BEG
| |
75 "path", path_.AsUTF8Unsafe()); | |
Dan Beam
2015/04/24 22:27:02
should I be passing along the path here?
| |
72 } | 76 } |
73 | 77 |
74 File& File::operator=(RValue other) { | 78 File& File::operator=(RValue other) { |
75 if (this != other.object) { | 79 if (this != other.object) { |
76 Close(); | 80 Close(); |
77 SetPlatformFile(other.object->TakePlatformFile()); | 81 SetPlatformFile(other.object->TakePlatformFile()); |
78 error_details_ = other.object->error_details(); | 82 error_details_ = other.object->error_details(); |
79 created_ = other.object->created(); | 83 created_ = other.object->created(); |
Lei Zhang
2015/04/27 22:55:55
Set |path_| here too?
Dan Beam
2015/04/28 03:17:11
Done.
| |
80 async_ = other.object->async_; | 84 async_ = other.object->async_; |
81 } | 85 } |
82 return *this; | 86 return *this; |
83 } | 87 } |
84 | 88 |
85 #if !defined(OS_NACL) | 89 #if !defined(OS_NACL) |
86 void File::Initialize(const FilePath& name, uint32 flags) { | 90 void File::Initialize(const FilePath& path, uint32 flags) { |
87 if (name.ReferencesParent()) { | 91 path_ = path; |
92 if (path_.ReferencesParent()) { | |
88 error_details_ = FILE_ERROR_ACCESS_DENIED; | 93 error_details_ = FILE_ERROR_ACCESS_DENIED; |
89 return; | 94 return; |
90 } | 95 } |
91 DoInitialize(name, flags); | 96 TRACE_EVENT_ASYNC_BEGIN1(kTraceGroup, "File", this, |
97 "path", path_.AsUTF8Unsafe()); | |
98 DoInitialize(flags); | |
92 } | 99 } |
93 #endif | 100 #endif |
94 | 101 |
95 std::string File::ErrorToString(Error error) { | 102 std::string File::ErrorToString(Error error) { |
96 switch (error) { | 103 switch (error) { |
97 case FILE_OK: | 104 case FILE_OK: |
98 return "FILE_OK"; | 105 return "FILE_OK"; |
99 case FILE_ERROR_FAILED: | 106 case FILE_ERROR_FAILED: |
100 return "FILE_ERROR_FAILED"; | 107 return "FILE_ERROR_FAILED"; |
101 case FILE_ERROR_IN_USE: | 108 case FILE_ERROR_IN_USE: |
(...skipping 27 matching lines...) Expand all Loading... | |
129 case FILE_ERROR_IO: | 136 case FILE_ERROR_IO: |
130 return "FILE_ERROR_IO"; | 137 return "FILE_ERROR_IO"; |
131 case FILE_ERROR_MAX: | 138 case FILE_ERROR_MAX: |
132 break; | 139 break; |
133 } | 140 } |
134 | 141 |
135 NOTREACHED(); | 142 NOTREACHED(); |
136 return ""; | 143 return ""; |
137 } | 144 } |
138 | 145 |
146 // static | |
147 const char File::kTraceGroup[] = TRACE_DISABLED_BY_DEFAULT("file"); | |
148 | |
139 bool File::Flush() { | 149 bool File::Flush() { |
140 ElapsedTimer timer; | 150 ElapsedTimer timer; |
141 bool return_value = DoFlush(); | 151 bool return_value = DoFlush(); |
142 UMA_HISTOGRAM_TIMES("PlatformFile.FlushTime", timer.Elapsed()); | 152 UMA_HISTOGRAM_TIMES("PlatformFile.FlushTime", timer.Elapsed()); |
143 return return_value; | 153 return return_value; |
144 } | 154 } |
145 | 155 |
146 } // namespace base | 156 } // namespace base |
OLD | NEW |