BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chromecast / base / error_codes.cc
blobe26a82009cc7baf1a5e9e1b11dec6f5968fed422
1 // Copyright 2015 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 "chromecast/base/error_codes.h"
7 #include <fcntl.h>
9 #include <string>
11 #include "base/files/file_util.h"
12 #include "base/logging.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "chromecast/base/path_utils.h"
16 namespace chromecast {
18 namespace {
20 const char kInitialErrorFile[] = "initial_error";
22 base::FilePath GetInitialErrorFilePath() {
23 return GetHomePathASCII(kInitialErrorFile);
26 } // namespace
28 ErrorCode GetInitialErrorCode() {
29 std::string initial_error_code_str;
30 if (!base::ReadFileToString(GetInitialErrorFilePath(),
31 &initial_error_code_str)) {
32 return NO_ERROR;
35 int initial_error_code = 0;
36 if (base::StringToInt(initial_error_code_str, &initial_error_code) &&
37 initial_error_code >= NO_ERROR && initial_error_code <= ERROR_UNKNOWN) {
38 VLOG(1) << "Initial error from " << GetInitialErrorFilePath().value()
39 << ": " << initial_error_code;
40 return static_cast<ErrorCode>(initial_error_code);
43 LOG(ERROR) << "Unknown initial error code: " << initial_error_code_str;
44 return NO_ERROR;
47 bool SetInitialErrorCode(ErrorCode initial_error_code) {
48 // Note: Do not use Chromium IO methods in this function. When cast_shell
49 // crashes, this function can be called by any thread.
50 const std::string error_file_path = GetInitialErrorFilePath().value();
52 if (initial_error_code > NO_ERROR && initial_error_code <= ERROR_UNKNOWN) {
53 const std::string initial_error_code_str(
54 base::IntToString(initial_error_code));
55 int fd = creat(error_file_path.c_str(), 0640);
56 if (fd < 0) {
57 PLOG(ERROR) << "Could not open error code file";
58 return false;
61 int written =
62 write(fd, initial_error_code_str.data(), initial_error_code_str.size());
64 if (written != static_cast<int>(initial_error_code_str.size())) {
65 PLOG(ERROR) << "Could not write error code to file: written=" << written
66 << ", expected=" << initial_error_code_str.size();
67 close(fd);
68 return false;
71 close(fd);
72 return true;
75 // Remove initial error file if no error.
76 if (unlink(error_file_path.c_str()) == 0 || errno == ENOENT)
77 return true;
79 PLOG(ERROR) << "Failed to remove error file";
80 return false;
83 } // namespace chromecast