| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/memory/low_memory_observer.h" | 5 #include "chrome/browser/chromeos/memory/low_memory_observer.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 int file_descriptor_; | 110 int file_descriptor_; |
| 111 base::OneShotTimer<LowMemoryObserverImpl> timer_; | 111 base::OneShotTimer<LowMemoryObserverImpl> timer_; |
| 112 | 112 |
| 113 DISALLOW_COPY_AND_ASSIGN(LowMemoryObserverImpl); | 113 DISALLOW_COPY_AND_ASSIGN(LowMemoryObserverImpl); |
| 114 }; | 114 }; |
| 115 | 115 |
| 116 void LowMemoryObserverImpl::StartObservingOnFileThread() { | 116 void LowMemoryObserverImpl::StartObservingOnFileThread() { |
| 117 DCHECK_LE(file_descriptor_, 0) | 117 DCHECK_LE(file_descriptor_, 0) |
| 118 << "Attempted to start observation when it was already started."; | 118 << "Attempted to start observation when it was already started."; |
| 119 DCHECK(watcher_.get() == NULL); | 119 DCHECK(watcher_.get() == NULL); |
| 120 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 120 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 121 DCHECK(base::MessageLoopForIO::current()); | 121 DCHECK(base::MessageLoopForIO::current()); |
| 122 | 122 |
| 123 file_descriptor_ = ::open(kLowMemFile, O_RDONLY); | 123 file_descriptor_ = ::open(kLowMemFile, O_RDONLY); |
| 124 // Don't report this error unless we're really running on ChromeOS | 124 // Don't report this error unless we're really running on ChromeOS |
| 125 // to avoid testing spam. | 125 // to avoid testing spam. |
| 126 if (file_descriptor_ < 0 && base::SysInfo::IsRunningOnChromeOS()) { | 126 if (file_descriptor_ < 0 && base::SysInfo::IsRunningOnChromeOS()) { |
| 127 PLOG(ERROR) << "Unable to open " << kLowMemFile; | 127 PLOG(ERROR) << "Unable to open " << kLowMemFile; |
| 128 return; | 128 return; |
| 129 } | 129 } |
| 130 watcher_.reset(new base::MessageLoopForIO::FileDescriptorWatcher); | 130 watcher_.reset(new base::MessageLoopForIO::FileDescriptorWatcher); |
| 131 StartWatchingDescriptor(); | 131 StartWatchingDescriptor(); |
| 132 } | 132 } |
| 133 | 133 |
| 134 void LowMemoryObserverImpl::StopObservingOnFileThread() { | 134 void LowMemoryObserverImpl::StopObservingOnFileThread() { |
| 135 // If StartObserving failed, StopObserving will still get called. | 135 // If StartObserving failed, StopObserving will still get called. |
| 136 timer_.Stop(); | 136 timer_.Stop(); |
| 137 if (file_descriptor_ >= 0) { | 137 if (file_descriptor_ >= 0) { |
| 138 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 138 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 139 watcher_.reset(NULL); | 139 watcher_.reset(NULL); |
| 140 ::close(file_descriptor_); | 140 ::close(file_descriptor_); |
| 141 file_descriptor_ = -1; | 141 file_descriptor_ = -1; |
| 142 } | 142 } |
| 143 } | 143 } |
| 144 | 144 |
| 145 void LowMemoryObserverImpl::ScheduleNextObservation() { | 145 void LowMemoryObserverImpl::ScheduleNextObservation() { |
| 146 timer_.Start(FROM_HERE, | 146 timer_.Start(FROM_HERE, |
| 147 base::TimeDelta::FromMilliseconds(kLowMemoryCheckTimeoutMs), | 147 base::TimeDelta::FromMilliseconds(kLowMemoryCheckTimeoutMs), |
| 148 this, | 148 this, |
| 149 &LowMemoryObserverImpl::StartWatchingDescriptor); | 149 &LowMemoryObserverImpl::StartWatchingDescriptor); |
| 150 } | 150 } |
| 151 | 151 |
| 152 void LowMemoryObserverImpl::StartWatchingDescriptor() { | 152 void LowMemoryObserverImpl::StartWatchingDescriptor() { |
| 153 DCHECK(watcher_.get()); | 153 DCHECK(watcher_.get()); |
| 154 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 154 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 155 DCHECK(base::MessageLoopForIO::current()); | 155 DCHECK(base::MessageLoopForIO::current()); |
| 156 if (file_descriptor_ < 0) | 156 if (file_descriptor_ < 0) |
| 157 return; | 157 return; |
| 158 if (!base::MessageLoopForIO::current()->WatchFileDescriptor( | 158 if (!base::MessageLoopForIO::current()->WatchFileDescriptor( |
| 159 file_descriptor_, | 159 file_descriptor_, |
| 160 false, // persistent=false: We want it to fire once and reschedule. | 160 false, // persistent=false: We want it to fire once and reschedule. |
| 161 base::MessageLoopForIO::WATCH_READ, | 161 base::MessageLoopForIO::WATCH_READ, |
| 162 watcher_.get(), | 162 watcher_.get(), |
| 163 &watcher_delegate_)) { | 163 &watcher_delegate_)) { |
| 164 LOG(ERROR) << "Unable to watch " << kLowMemFile; | 164 LOG(ERROR) << "Unable to watch " << kLowMemFile; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 182 | 182 |
| 183 void LowMemoryObserver::Stop() { | 183 void LowMemoryObserver::Stop() { |
| 184 BrowserThread::PostTask( | 184 BrowserThread::PostTask( |
| 185 BrowserThread::FILE, | 185 BrowserThread::FILE, |
| 186 FROM_HERE, | 186 FROM_HERE, |
| 187 base::Bind(&LowMemoryObserverImpl::StopObservingOnFileThread, | 187 base::Bind(&LowMemoryObserverImpl::StopObservingOnFileThread, |
| 188 observer_.get())); | 188 observer_.get())); |
| 189 } | 189 } |
| 190 | 190 |
| 191 } // namespace chromeos | 191 } // namespace chromeos |
| OLD | NEW |