Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(776)

Side by Side Diff: services/shell/public/cpp/lib/shell_connection_ref.cc

Issue 2131493002: ShellConnection -> ServiceContext (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@st
Patch Set: . Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « services/shell/public/cpp/lib/shell_connection.cc ('k') | services/shell/public/cpp/service.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 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 #include "services/shell/public/cpp/shell_connection_ref.h"
6
7 #include "base/bind.h"
8 #include "base/memory/ptr_util.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/threading/thread_checker.h"
11 #include "base/threading/thread_task_runner_handle.h"
12
13 namespace shell {
14
15 class ShellConnectionRefImpl : public ShellConnectionRef {
16 public:
17 ShellConnectionRefImpl(
18 base::WeakPtr<ShellConnectionRefFactory> factory,
19 scoped_refptr<base::SingleThreadTaskRunner> service_task_runner)
20 : factory_(factory),
21 service_task_runner_(service_task_runner) {
22 // This object is not thread-safe but may be used exclusively on a different
23 // thread from the one which constructed it.
24 thread_checker_.DetachFromThread();
25 }
26
27 ~ShellConnectionRefImpl() override {
28 DCHECK(thread_checker_.CalledOnValidThread());
29
30 if (service_task_runner_->BelongsToCurrentThread() && factory_) {
31 factory_->Release();
32 } else {
33 service_task_runner_->PostTask(
34 FROM_HERE,
35 base::Bind(&ShellConnectionRefFactory::Release, factory_));
36 }
37 }
38
39 private:
40 // ShellConnectionRef:
41 std::unique_ptr<ShellConnectionRef> Clone() override {
42 DCHECK(thread_checker_.CalledOnValidThread());
43
44 if (service_task_runner_->BelongsToCurrentThread() && factory_) {
45 factory_->AddRef();
46 } else {
47 service_task_runner_->PostTask(
48 FROM_HERE,
49 base::Bind(&ShellConnectionRefFactory::AddRef, factory_));
50 }
51
52 return base::WrapUnique(
53 new ShellConnectionRefImpl(factory_, service_task_runner_));
54 }
55
56 base::WeakPtr<ShellConnectionRefFactory> factory_;
57 scoped_refptr<base::SingleThreadTaskRunner> service_task_runner_;
58 base::ThreadChecker thread_checker_;
59
60 DISALLOW_COPY_AND_ASSIGN(ShellConnectionRefImpl);
61 };
62
63 ShellConnectionRefFactory::ShellConnectionRefFactory(
64 const base::Closure& quit_closure)
65 : quit_closure_(quit_closure), weak_factory_(this) {
66 DCHECK(!quit_closure_.is_null());
67 }
68
69 ShellConnectionRefFactory::~ShellConnectionRefFactory() {}
70
71 std::unique_ptr<ShellConnectionRef> ShellConnectionRefFactory::CreateRef() {
72 AddRef();
73 return base::WrapUnique(
74 new ShellConnectionRefImpl(weak_factory_.GetWeakPtr(),
75 base::ThreadTaskRunnerHandle::Get()));
76 }
77
78 void ShellConnectionRefFactory::AddRef() {
79 ++ref_count_;
80 }
81
82 void ShellConnectionRefFactory::Release() {
83 if (!--ref_count_)
84 quit_closure_.Run();
85 }
86
87 } // namespace shell
OLDNEW
« no previous file with comments | « services/shell/public/cpp/lib/shell_connection.cc ('k') | services/shell/public/cpp/service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698