chrome_system_resources.h revision ddb351dbec246cf1fab5ec20d2d5520909041de1
1// Copyright (c) 2011 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.
4//
5// Simple system resources class that uses the current message loop
6// for scheduling.  Assumes the current message loop is already
7// running.
8
9#ifndef CHROME_BROWSER_SYNC_NOTIFIER_CHROME_SYSTEM_RESOURCES_H_
10#define CHROME_BROWSER_SYNC_NOTIFIER_CHROME_SYSTEM_RESOURCES_H_
11#pragma once
12
13#include <set>
14#include <string>
15
16#include "base/memory/scoped_ptr.h"
17#include "base/message_loop.h"
18#include "base/task.h"
19#include "base/threading/non_thread_safe.h"
20#include "chrome/browser/sync/notifier/state_writer.h"
21#include "google/cacheinvalidation/invalidation-client.h"
22
23namespace sync_notifier {
24
25class ChromeSystemResources : public invalidation::SystemResources {
26 public:
27  explicit ChromeSystemResources(StateWriter* state_writer);
28
29  ~ChromeSystemResources();
30
31  // invalidation::SystemResources implementation.
32
33  virtual invalidation::Time current_time();
34
35  virtual void StartScheduler();
36
37  virtual void StopScheduler();
38
39  virtual void ScheduleWithDelay(invalidation::TimeDelta delay,
40                                 invalidation::Closure* task);
41
42  virtual void ScheduleImmediately(invalidation::Closure* task);
43
44  virtual void ScheduleOnListenerThread(invalidation::Closure* task);
45
46  virtual bool IsRunningOnInternalThread();
47
48  virtual void Log(LogLevel level, const char* file, int line,
49                   const char* format, ...);
50
51  virtual void WriteState(const invalidation::string& state,
52                          invalidation::StorageCallback* callback);
53
54 private:
55  base::NonThreadSafe non_thread_safe_;
56  scoped_ptr<ScopedRunnableMethodFactory<ChromeSystemResources> >
57      scoped_runnable_method_factory_;
58  // Holds all posted tasks that have not yet been run.
59  std::set<invalidation::Closure*> posted_tasks_;
60  StateWriter* state_writer_;
61
62  // TODO(tim): Trying to debug bug crbug.com/64652.
63  const MessageLoop* created_on_loop_;
64
65  // If the scheduler has been started, inserts |task| into
66  // |posted_tasks_| and returns a Task* to post.  Otherwise,
67  // immediately deletes |task| and returns NULL.
68  Task* MakeTaskToPost(invalidation::Closure* task);
69
70  // Runs the task, deletes it, and removes it from |posted_tasks_|.
71  void RunPostedTask(invalidation::Closure* task);
72
73  // Runs the given storage callback with "true" and deletes it.
74  void RunAndDeleteStorageCallback(
75      invalidation::StorageCallback* callback);
76};
77
78}  // namespace sync_notifier
79
80#endif  // CHROME_BROWSER_SYNC_NOTIFIER_CHROME_SYSTEM_RESOURCES_H_
81