| Index: base/memory/low_memory_observer_chromeos.cc
|
| diff --git a/base/memory/low_memory_observer_chromeos.cc b/base/memory/low_memory_observer_chromeos.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..b580972e649bd89f9bc689980de98b8db3b77170
|
| --- /dev/null
|
| +++ b/base/memory/low_memory_observer_chromeos.cc
|
| @@ -0,0 +1,53 @@
|
| +// Copyright 2017 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "base/memory/low_memory_observer_chromeos.h"
|
| +
|
| +#include <fcntl.h>
|
| +#include <sys/select.h>
|
| +#include <sys/time.h>
|
| +
|
| +#include "base/posix/eintr_wrapper.h"
|
| +#include "base/sys_info.h"
|
| +
|
| +namespace base {
|
| +namespace chromeos {
|
| +
|
| +namespace {
|
| +
|
| +// This is the file that will exist if low memory notification is available
|
| +// on the device. Whenever it becomes readable, it signals a low memory
|
| +// condition.
|
| +const char kLowMemFile[] = "/dev/chromeos-low-mem";
|
| +
|
| +} // namespace
|
| +
|
| +LowMemoryObserver::LowMemoryObserver()
|
| + : low_mem_file_(HANDLE_EINTR(::open(kLowMemFile, O_RDONLY))) {
|
| + LOG_IF(ERROR,
|
| + base::SysInfo::IsRunningOnChromeOS() && !low_mem_file_.is_valid())
|
| + << "Cannot open kernel listener";
|
| +}
|
| +
|
| +LowMemoryObserver::~LowMemoryObserver() {}
|
| +
|
| +bool LowMemoryObserver::IsLowMemoryCondition() const {
|
| + if (!is_valid())
|
| + return false;
|
| +
|
| + int fd = low_mem_file_.get();
|
| + fd_set fds;
|
| + struct timeval tv;
|
| +
|
| + FD_ZERO(&fds);
|
| + FD_SET(fd, &fds);
|
| +
|
| + tv.tv_sec = 0;
|
| + tv.tv_usec = 0;
|
| +
|
| + return HANDLE_EINTR(select(fd + 1, &fds, NULL, NULL, &tv)) > 0;
|
| +}
|
| +
|
| +} // namespace chromeos
|
| +} // namespace base
|
|
|