summaryrefslogtreecommitdiffstats
path: root/mojo/shell/public/cpp/message_loop_ref.h
blob: fffb7384021cdd8374bc9ddf85ea54675f35e648 (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
// Copyright 2016 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 MOJO_SHELL_PUBLIC_CPP_APPLICATION_RUNNER_REFERENCE_H_
#define MOJO_SHELL_PUBLIC_CPP_APPLICATION_RUNNER_REFERENCE_H_

#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "mojo/public/cpp/system/core.h"

namespace mojo {

class MessageLoopRefImpl;

// An interface implementation can keep this object as a member variable to
// hold a reference to the ShellConnection, keeping it alive as long as the
// bound implementation exists.
// Since interface implementations can be bound on different threads than the
// ShellConnection, this class is safe to use on any thread. However, each
// instance should only be used on one thread at a time (otherwise there'll be
// races between the AddRef resulting from cloning and destruction).
class MessageLoopRef {
 public:
  virtual ~MessageLoopRef() {}

  virtual scoped_ptr<MessageLoopRef> Clone() = 0;
};

class MessageLoopRefFactory {
 public:
  MessageLoopRefFactory();
  ~MessageLoopRefFactory();

  void set_quit_closure(const base::Closure& quit_closure) {
    quit_closure_ = quit_closure;
  }

  scoped_ptr<MessageLoopRef> CreateRef();

 private:
  friend MessageLoopRefImpl;

  // Called from MessageLoopRefImpl.
  void AddRef();
  void Release();

  base::Closure quit_closure_;
  int ref_count_ = 0;

  DISALLOW_COPY_AND_ASSIGN(MessageLoopRefFactory);
};

}  // namespace mojo

#endif  // MOJO_SHELL_PUBLIC_CPP_APPLICATION_RUNNER_REFERENCE_H_