summaryrefslogtreecommitdiffstats
path: root/webkit/quota/quota_task.h
blob: dbd5ca04fcf022edf25a3dc46931c010276b89cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (c) 2011 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.

#ifndef WEBKIT_QUOTA_QUOTA_TASK_H_
#define WEBKIT_QUOTA_QUOTA_TASK_H_
#pragma once

#include <set>

#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/memory/ref_counted.h"
#include "base/message_loop_proxy.h"

namespace base {
class MessageLoopProxy;
}

namespace quota {

class QuotaTaskObserver;

// A base class for quota tasks.
class QuotaTask {
 public:
  virtual ~QuotaTask();
  void Start();

 protected:
  explicit QuotaTask(QuotaTaskObserver* observer);

  // The task body.
  virtual void Run() = 0;

  // Called upon completion, on the original message loop.
  virtual void Completed() = 0;

  // Called when the task is aborted.
  virtual void Aborted() {}

  void CallCompleted();

  // Call this to delete itself.
  void DeleteSoon();

  QuotaTaskObserver* observer() const { return observer_; }
  scoped_refptr<base::MessageLoopProxy> original_message_loop() const {
    return original_message_loop_;
  }

 private:
  friend class QuotaTaskObserver;
  void Abort();
  QuotaTaskObserver* observer_;
  scoped_refptr<base::MessageLoopProxy> original_message_loop_;
};

// For tasks that post tasks to the other thread.
class QuotaThreadTask : public QuotaTask,
                        public base::RefCountedThreadSafe<QuotaThreadTask> {
 public:
  QuotaThreadTask(QuotaTaskObserver* observer,
                  scoped_refptr<base::MessageLoopProxy> target_message_loop);

 protected:
  virtual ~QuotaThreadTask();

  // One of the following Run methods should be overridden for execution
  // on the target thread.

  // A task to invoke the CallCompleted() method on the original thread will
  // be scheduled immediately upon return from RunOnTargetThread().
  virtual void RunOnTargetThread();

  // A task to invoke the CallCompleted() method on the original thread will
  // only be scheduled if RunOnTargetThreadAsync returns true. If false is
  // returned, the derived class should schedule a task to do so upon actual
  // completion.
  virtual bool RunOnTargetThreadAsync();

  virtual void Run() OVERRIDE;
  scoped_refptr<base::MessageLoopProxy> target_message_loop() const {
    return target_message_loop_;
  }

 private:
  friend class base::RefCountedThreadSafe<QuotaThreadTask>;
  friend class QuotaTaskObserver;
  void CallRunOnTargetThread();

  scoped_refptr<base::MessageLoopProxy> target_message_loop_;
};

class QuotaTaskObserver {
 public:
  virtual ~QuotaTaskObserver();

 protected:
  friend class QuotaTask;
  friend class QuotaThreadTask;

  QuotaTaskObserver();
  void RegisterTask(QuotaTask* task);
  void UnregisterTask(QuotaTask* task);

  typedef std::set<QuotaTask*> TaskSet;
  TaskSet running_quota_tasks_;
};
}

#endif  // WEBKIT_QUOTA_QUOTA_TASK_H_