| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/sync/notifier/chrome_system_resources.h" | 5 #include "chrome/browser/sync/notifier/chrome_system_resources.h" |
| 6 | 6 |
| 7 #include <cstdlib> | 7 #include <cstdlib> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/stl_util-inl.h" |
| 12 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 13 #include "base/task.h" | |
| 14 #include "chrome/browser/sync/notifier/invalidation_util.h" | 14 #include "chrome/browser/sync/notifier/invalidation_util.h" |
| 15 | 15 |
| 16 namespace sync_notifier { | 16 namespace sync_notifier { |
| 17 | 17 |
| 18 ChromeSystemResources::ChromeSystemResources() | 18 ChromeSystemResources::ChromeSystemResources() { |
| 19 : scheduler_active_(false) {} | 19 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 20 } |
| 20 | 21 |
| 21 ChromeSystemResources::~ChromeSystemResources() { | 22 ChromeSystemResources::~ChromeSystemResources() { |
| 22 DCHECK(!scheduler_active_); | 23 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 24 StopScheduler(); |
| 23 } | 25 } |
| 24 | 26 |
| 25 invalidation::Time ChromeSystemResources::current_time() { | 27 invalidation::Time ChromeSystemResources::current_time() { |
| 28 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 26 return base::Time::Now(); | 29 return base::Time::Now(); |
| 27 } | 30 } |
| 28 | 31 |
| 29 void ChromeSystemResources::StartScheduler() { | 32 void ChromeSystemResources::StartScheduler() { |
| 30 DCHECK(!scheduler_active_); | 33 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 31 scheduler_active_ = true; | 34 scoped_runnable_method_factory_.reset( |
| 35 new ScopedRunnableMethodFactory<ChromeSystemResources>(this)); |
| 32 } | 36 } |
| 33 | 37 |
| 34 void ChromeSystemResources::StopScheduler() { | 38 void ChromeSystemResources::StopScheduler() { |
| 35 DCHECK(scheduler_active_); | 39 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 36 scheduler_active_ = false; | 40 scoped_runnable_method_factory_.reset(); |
| 41 STLDeleteElements(&posted_tasks_); |
| 37 } | 42 } |
| 38 | 43 |
| 39 void ChromeSystemResources::ScheduleWithDelay( | 44 void ChromeSystemResources::ScheduleWithDelay( |
| 40 invalidation::TimeDelta delay, | 45 invalidation::TimeDelta delay, |
| 41 invalidation::Closure* task) { | 46 invalidation::Closure* task) { |
| 42 if (!scheduler_active_) { | 47 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 43 delete task; | 48 Task* task_to_post = MakeTaskToPost(task); |
| 49 if (!task_to_post) { |
| 44 return; | 50 return; |
| 45 } | 51 } |
| 46 DCHECK(invalidation::IsCallbackRepeatable(task)); | |
| 47 MessageLoop::current()->PostDelayedTask( | 52 MessageLoop::current()->PostDelayedTask( |
| 48 FROM_HERE, | 53 FROM_HERE, task_to_post, delay.InMillisecondsRoundedUp()); |
| 49 NewRunnableFunction(&RunAndDeleteClosure, task), | |
| 50 delay.InMillisecondsRoundedUp()); | |
| 51 } | 54 } |
| 52 | 55 |
| 53 void ChromeSystemResources::ScheduleImmediately( | 56 void ChromeSystemResources::ScheduleImmediately( |
| 54 invalidation::Closure* task) { | 57 invalidation::Closure* task) { |
| 55 if (!scheduler_active_) { | 58 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 56 delete task; | 59 Task* task_to_post = MakeTaskToPost(task); |
| 60 if (!task_to_post) { |
| 57 return; | 61 return; |
| 58 } | 62 } |
| 59 DCHECK(invalidation::IsCallbackRepeatable(task)); | 63 MessageLoop::current()->PostTask(FROM_HERE, task_to_post); |
| 60 MessageLoop::current()->PostTask( | |
| 61 FROM_HERE, NewRunnableFunction(&RunAndDeleteClosure, task)); | |
| 62 } | 64 } |
| 63 | 65 |
| 64 void ChromeSystemResources::Log( | 66 void ChromeSystemResources::Log( |
| 65 LogLevel level, const char* file, int line, | 67 LogLevel level, const char* file, int line, |
| 66 const char* format, ...) { | 68 const char* format, ...) { |
| 69 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 67 va_list ap; | 70 va_list ap; |
| 68 va_start(ap, format); | 71 va_start(ap, format); |
| 69 std::string result; | 72 std::string result; |
| 70 StringAppendV(&result, format, ap); | 73 StringAppendV(&result, format, ap); |
| 71 logging::LogMessage(file, line).stream() << result; | 74 logging::LogMessage(file, line).stream() << result; |
| 72 va_end(ap); | 75 va_end(ap); |
| 73 } | 76 } |
| 74 | 77 |
| 78 Task* ChromeSystemResources::MakeTaskToPost( |
| 79 invalidation::Closure* task) { |
| 80 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 81 DCHECK(invalidation::IsCallbackRepeatable(task)); |
| 82 if (!scoped_runnable_method_factory_.get()) { |
| 83 delete task; |
| 84 return NULL; |
| 85 } |
| 86 posted_tasks_.insert(task); |
| 87 Task* task_to_post = |
| 88 scoped_runnable_method_factory_->NewRunnableMethod( |
| 89 &ChromeSystemResources::RunPostedTask, task); |
| 90 return task_to_post; |
| 91 } |
| 92 |
| 93 void ChromeSystemResources::RunPostedTask(invalidation::Closure* task) { |
| 94 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 95 RunAndDeleteClosure(task); |
| 96 posted_tasks_.erase(task); |
| 97 } |
| 98 |
| 75 } // namespace sync_notifier | 99 } // namespace sync_notifier |
| OLD | NEW |