Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(320)

Side by Side Diff: chrome/browser/chromeos/system/automatic_reboot_manager.cc

Issue 191673003: Implement ScopedFD in terms of ScopedGeneric. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 "chrome/browser/chromeos/system/automatic_reboot_manager.h" 5 #include "chrome/browser/chromeos/system/automatic_reboot_manager.h"
6 6
7 #include <fcntl.h> 7 #include <fcntl.h>
8 #include <sys/stat.h> 8 #include <sys/stat.h>
9 #include <sys/types.h> 9 #include <sys/types.h>
10 10
11 #include <algorithm> 11 #include <algorithm>
12 #include <string> 12 #include <string>
13 13
14 #include "ash/shell.h" 14 #include "ash/shell.h"
15 #include "ash/wm/user_activity_detector.h" 15 #include "ash/wm/user_activity_detector.h"
16 #include "base/bind.h" 16 #include "base/bind.h"
17 #include "base/bind_helpers.h" 17 #include "base/bind_helpers.h"
18 #include "base/callback.h" 18 #include "base/callback.h"
19 #include "base/file_util.h" 19 #include "base/file_util.h"
20 #include "base/files/file_path.h" 20 #include "base/files/file_path.h"
21 #include "base/files/scoped_file.h"
21 #include "base/location.h" 22 #include "base/location.h"
22 #include "base/logging.h" 23 #include "base/logging.h"
23 #include "base/memory/ref_counted.h" 24 #include "base/memory/ref_counted.h"
24 #include "base/path_service.h" 25 #include "base/path_service.h"
25 #include "base/posix/eintr_wrapper.h" 26 #include "base/posix/eintr_wrapper.h"
26 #include "base/prefs/pref_registry_simple.h" 27 #include "base/prefs/pref_registry_simple.h"
27 #include "base/prefs/pref_service.h" 28 #include "base/prefs/pref_service.h"
28 #include "base/single_thread_task_runner.h" 29 #include "base/single_thread_task_runner.h"
29 #include "base/strings/string_number_conversions.h" 30 #include "base/strings/string_number_conversions.h"
30 #include "base/thread_task_runner_handle.h" 31 #include "base/thread_task_runner_handle.h"
(...skipping 18 matching lines...) Expand all
49 50
50 namespace { 51 namespace {
51 52
52 const int kMinRebootUptimeMs = 60 * 60 * 1000; // 1 hour. 53 const int kMinRebootUptimeMs = 60 * 60 * 1000; // 1 hour.
53 const int kLoginManagerIdleTimeoutMs = 60 * 1000; // 60 seconds. 54 const int kLoginManagerIdleTimeoutMs = 60 * 1000; // 60 seconds.
54 const int kGracePeriodMs = 24 * 60 * 60 * 1000; // 24 hours. 55 const int kGracePeriodMs = 24 * 60 * 60 * 1000; // 24 hours.
55 const int kOneKilobyte = 1 << 10; // 1 kB in bytes. 56 const int kOneKilobyte = 1 << 10; // 1 kB in bytes.
56 57
57 base::TimeDelta ReadTimeDeltaFromFile(const base::FilePath& path) { 58 base::TimeDelta ReadTimeDeltaFromFile(const base::FilePath& path) {
58 base::ThreadRestrictions::AssertIOAllowed(); 59 base::ThreadRestrictions::AssertIOAllowed();
59 int fd = HANDLE_EINTR(open(path.value().c_str(), O_RDONLY | O_NOFOLLOW)); 60 base::ScopedFD fd(
60 if (fd < 0) 61 HANDLE_EINTR(open(path.value().c_str(), O_RDONLY | O_NOFOLLOW)));
62 if (!fd.is_valid())
61 return base::TimeDelta(); 63 return base::TimeDelta();
62 file_util::ScopedFD fd_closer(&fd);
63 64
64 std::string contents; 65 std::string contents;
65 char buffer[kOneKilobyte]; 66 char buffer[kOneKilobyte];
66 ssize_t length; 67 ssize_t length;
67 while ((length = read(fd, buffer, sizeof(buffer))) > 0) 68 while ((length = read(fd.get(), buffer, sizeof(buffer))) > 0)
agl 2014/03/18 06:52:05 This read should be wrapped in HANDLE_EINTR.
68 contents.append(buffer, length); 69 contents.append(buffer, length);
69 70
70 double seconds; 71 double seconds;
71 if (!base::StringToDouble(contents.substr(0, contents.find(' ')), &seconds) || 72 if (!base::StringToDouble(contents.substr(0, contents.find(' ')), &seconds) ||
72 seconds < 0.0) { 73 seconds < 0.0) {
73 return base::TimeDelta(); 74 return base::TimeDelta();
74 } 75 }
75 return base::TimeDelta::FromMilliseconds(seconds * 1000.0); 76 return base::TimeDelta::FromMilliseconds(seconds * 1000.0);
76 } 77 }
77 78
(...skipping 23 matching lines...) Expand all
101 ReadTimeDeltaFromFile(update_reboot_needed_uptime_file); 102 ReadTimeDeltaFromFile(update_reboot_needed_uptime_file);
102 if (last_update_reboot_needed_uptime != kZeroTimeDelta) 103 if (last_update_reboot_needed_uptime != kZeroTimeDelta)
103 return; 104 return;
104 105
105 base::FilePath uptime_file; 106 base::FilePath uptime_file;
106 CHECK(PathService::Get(chromeos::FILE_UPTIME, &uptime_file)); 107 CHECK(PathService::Get(chromeos::FILE_UPTIME, &uptime_file));
107 const base::TimeDelta uptime = ReadTimeDeltaFromFile(uptime_file); 108 const base::TimeDelta uptime = ReadTimeDeltaFromFile(uptime_file);
108 if (uptime == kZeroTimeDelta) 109 if (uptime == kZeroTimeDelta)
109 return; 110 return;
110 111
111 int fd = HANDLE_EINTR(open(update_reboot_needed_uptime_file.value().c_str(), 112 base::ScopedFD fd(HANDLE_EINTR(
112 O_CREAT | O_WRONLY | O_TRUNC | O_NOFOLLOW, 113 open(update_reboot_needed_uptime_file.value().c_str(),
113 0666)); 114 O_CREAT | O_WRONLY | O_TRUNC | O_NOFOLLOW,
114 if (fd < 0) 115 0666)));
116 if (!fd.is_valid())
115 return; 117 return;
116 file_util::ScopedFD fd_closer(&fd);
117 118
118 std::string update_reboot_needed_uptime = 119 std::string update_reboot_needed_uptime =
119 base::DoubleToString(uptime.InSecondsF()); 120 base::DoubleToString(uptime.InSecondsF());
120 base::WriteFileDescriptor(fd, update_reboot_needed_uptime.c_str(), 121 base::WriteFileDescriptor(fd.get(), update_reboot_needed_uptime.c_str(),
121 update_reboot_needed_uptime.size()); 122 update_reboot_needed_uptime.size());
122 } 123 }
123 124
124 } // namespace 125 } // namespace
125 126
126 AutomaticRebootManager::SystemEventTimes::SystemEventTimes() 127 AutomaticRebootManager::SystemEventTimes::SystemEventTimes()
127 : has_boot_time(false), 128 : has_boot_time(false),
128 has_update_reboot_needed_time(false) { 129 has_update_reboot_needed_time(false) {
129 } 130 }
130 131
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after
409 } 410 }
410 411
411 login_screen_idle_timer_.reset(); 412 login_screen_idle_timer_.reset();
412 grace_start_timer_.reset(); 413 grace_start_timer_.reset();
413 grace_end_timer_.reset(); 414 grace_end_timer_.reset();
414 DBusThreadManager::Get()->GetPowerManagerClient()->RequestRestart(); 415 DBusThreadManager::Get()->GetPowerManagerClient()->RequestRestart();
415 } 416 }
416 417
417 } // namespace system 418 } // namespace system
418 } // namespace chromeos 419 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698