message_loop_proxy_impl.h revision c407dc5cd9bdc5668497f21b26b09d988ab439de
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
3// found in the LICENSE file.
4
5#ifndef BASE_MESSAGE_LOOP_PROXY_IMPL_H_
6#define BASE_MESSAGE_LOOP_PROXY_IMPL_H_
7
8#include "base/lock.h"
9#include "base/message_loop.h"
10#include "base/message_loop_proxy.h"
11
12namespace base {
13
14// A stock implementation of MessageLoopProxy that takes in a MessageLoop
15// and keeps track of its lifetime using the MessageLoop DestructionObserver.
16// For now a MessageLoopProxyImpl can only be created for the current thread.
17class MessageLoopProxyImpl : public MessageLoopProxy,
18                             public MessageLoop::DestructionObserver {
19 public:
20  ~MessageLoopProxyImpl();
21
22  // MessageLoopProxy implementation
23  virtual bool PostTask(const tracked_objects::Location& from_here,
24                        Task* task);
25  virtual bool PostDelayedTask(const tracked_objects::Location& from_here,
26                               Task* task, int64 delay_ms);
27  virtual bool PostNonNestableTask(const tracked_objects::Location& from_here,
28                                   Task* task);
29  virtual bool PostNonNestableDelayedTask(
30      const tracked_objects::Location& from_here,
31      Task* task,
32      int64 delay_ms);
33  virtual bool BelongsToCurrentThread();
34
35// MessageLoop::DestructionObserver implementation
36  void WillDestroyCurrentMessageLoop();
37
38 protected:
39  // Override OnDestruct so that we can delete the object on the target message
40  // loop if it still exists.
41  virtual void OnDestruct();
42
43 private:
44  MessageLoopProxyImpl();
45  bool PostTaskHelper(const tracked_objects::Location& from_here,
46                      Task* task, int64 delay_ms, bool nestable);
47
48  // For the factory method to work
49  friend class MessageLoopProxy;
50
51  // The lock that protects access to target_message_loop_.
52  Lock message_loop_lock_;
53  MessageLoop* target_message_loop_;
54
55  DISALLOW_COPY_AND_ASSIGN(MessageLoopProxyImpl);
56};
57
58}  // namespace base
59
60#endif  // BASE_MESSAGE_LOOP_PROXY_IMPL_H_
61
62