OLD | NEW |
| (Empty) |
1 // Copyright 2013 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. | |
4 | |
5 #include "base/logging.h" | |
6 #include "chrome/browser/extensions/api/image_writer_private/image_writer_utils.
h" | |
7 | |
8 namespace extensions { | |
9 namespace image_writer_utils | |
10 { | |
11 | |
12 const int kFsyncRatio = 1024; | |
13 | |
14 ImageWriter::ImageWriter() : file_(base::kInvalidPlatformFileValue), | |
15 writes_count_(0) { | |
16 } | |
17 | |
18 ImageWriter::~ImageWriter() { | |
19 if (file_ != base::kInvalidPlatformFileValue) | |
20 base::ClosePlatformFile(file_); | |
21 } | |
22 | |
23 bool ImageWriter::Open(const base::FilePath& path) { | |
24 if (file_ != base::kInvalidPlatformFileValue) | |
25 Close(); | |
26 DCHECK_EQ(base::kInvalidPlatformFileValue, file_); | |
27 base::PlatformFileError error; | |
28 file_ = base::CreatePlatformFile( | |
29 path, | |
30 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, | |
31 NULL, | |
32 &error); | |
33 if (error != base::PLATFORM_FILE_OK) { | |
34 LOG(ERROR) << "Couldn't open target path " | |
35 << path.value() << ": " << error; | |
36 return false; | |
37 } | |
38 return true; | |
39 } | |
40 | |
41 bool ImageWriter::Close() { | |
42 if (file_ != base::kInvalidPlatformFileValue && | |
43 base::ClosePlatformFile(file_)) { | |
44 file_ = base::kInvalidPlatformFileValue; | |
45 return true; | |
46 } else { | |
47 LOG(ERROR) << "Could not close target file"; | |
48 return false; | |
49 } | |
50 } | |
51 | |
52 int ImageWriter::Write(const char* data_block, int data_size) { | |
53 int written = base::WritePlatformFileAtCurrentPos(file_, data_block, | |
54 data_size); | |
55 if (written != data_size) { | |
56 LOG(ERROR) << "Error writing to target file"; | |
57 return -1; | |
58 } | |
59 | |
60 writes_count_++; | |
61 if (writes_count_ == kFsyncRatio) { | |
62 if (!base::FlushPlatformFile(file_)) { | |
63 LOG(ERROR) << "Error syncing target file"; | |
64 return -1; | |
65 } | |
66 writes_count_ = 0; | |
67 } | |
68 | |
69 return written; | |
70 } | |
71 | |
72 ImageReader::ImageReader() : file_(base::kInvalidPlatformFileValue) { | |
73 } | |
74 | |
75 ImageReader::~ImageReader() { | |
76 if (file_ != base::kInvalidPlatformFileValue) | |
77 base::ClosePlatformFile(file_); | |
78 } | |
79 | |
80 bool ImageReader::Open(const base::FilePath& path) { | |
81 if (file_ != base::kInvalidPlatformFileValue) | |
82 Close(); | |
83 DCHECK_EQ(base::kInvalidPlatformFileValue, file_); | |
84 base::PlatformFileError error; | |
85 file_ = base::CreatePlatformFile( | |
86 path, | |
87 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, | |
88 NULL, | |
89 &error); | |
90 if (error != base::PLATFORM_FILE_OK) { | |
91 LOG(ERROR) << "Couldn't open target path " | |
92 << path.value() << ": " << error; | |
93 return false; | |
94 } | |
95 return true; | |
96 } | |
97 | |
98 bool ImageReader::Close() { | |
99 if (file_ != base::kInvalidPlatformFileValue && | |
100 base::ClosePlatformFile(file_)) { | |
101 file_ = base::kInvalidPlatformFileValue; | |
102 return true; | |
103 } else { | |
104 LOG(ERROR) << "Could not close target file"; | |
105 return false; | |
106 } | |
107 return true; | |
108 } | |
109 | |
110 int ImageReader::Read(char* data_block, int data_size) { | |
111 int read = base::ReadPlatformFileAtCurrentPos(file_, data_block, data_size); | |
112 if (read < 0) | |
113 LOG(ERROR) << "Error reading from source file"; | |
114 return read; | |
115 } | |
116 | |
117 int64 ImageReader::GetSize() { | |
118 base::PlatformFileInfo info; | |
119 if (base::GetPlatformFileInfo(file_, &info)) { | |
120 return info.size; | |
121 } else { | |
122 return 0; | |
123 } | |
124 } | |
125 | |
126 } // namespace image_writer_utils | |
127 } // namespace extensions | |
OLD | NEW |