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

Side by Side Diff: chrome/browser/sync/weak_handle.cc

Issue 7572010: [Sync] Avoid leaking in WeakHandle even when the owner thread is gone (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments Created 9 years, 4 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 | Annotate | Revision Log
« no previous file with comments | « chrome/browser/sync/weak_handle.h ('k') | chrome/browser/sync/weak_handle_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/sync/weak_handle.h" 5 #include "chrome/browser/sync/weak_handle.h"
6 6
7 #include <sstream>
8
9 #include "base/message_loop_proxy.h"
10 #include "base/tracked.h"
11
7 namespace browser_sync { 12 namespace browser_sync {
8 13
9 namespace internal { 14 namespace internal {
10 15
11 WeakHandleCoreBase::WeakHandleCoreBase() 16 WeakHandleCoreBase::WeakHandleCoreBase()
12 : message_loop_proxy_( 17 : owner_loop_(MessageLoop::current()),
13 base::MessageLoopProxy::CreateForCurrentThread()) {} 18 owner_loop_proxy_(base::MessageLoopProxy::CreateForCurrentThread()),
14 19 destroyed_on_owner_thread_(false) {
15 WeakHandleCoreBase::~WeakHandleCoreBase() {} 20 owner_loop_->AddDestructionObserver(this);
21 }
16 22
17 bool WeakHandleCoreBase::IsOnOwnerThread() const { 23 bool WeakHandleCoreBase::IsOnOwnerThread() const {
18 return message_loop_proxy_->BelongsToCurrentThread(); 24 // We can't use |owner_loop_proxy_->BelongsToCurrentThread()| as
25 // it may not work from within a MessageLoop::DestructionObserver
26 // callback.
27 return MessageLoop::current() == owner_loop_;
19 } 28 }
20 29
21 void WeakHandleCoreBase::PostOnOwnerThread( 30 void WeakHandleCoreBase::WillDestroyCurrentMessageLoop() {
31 CHECK(IsOnOwnerThread());
32 CHECK(!destroyed_on_owner_thread_);
33 // NOTE: This function dispatches to
34 // WeakHandle<T>::CleanupOnOwnerThread() (i.e., not just
35 // WeakHandleCoreBase::CleanupOnOwnerThread() is run).
36 CleanupOnOwnerThread();
37 CHECK(destroyed_on_owner_thread_);
38 }
39
40 WeakHandleCoreBase::~WeakHandleCoreBase() {
41 // It is safe to read |destroyed_on_owner_thread_| here even if
42 // we're not on the owner thread (see comments on
43 // base::AtomicRefCountDecN()).
44 CHECK(destroyed_on_owner_thread_);
45 }
46
47 void WeakHandleCoreBase::CleanupOnOwnerThread() {
48 CHECK(IsOnOwnerThread());
49 CHECK(!destroyed_on_owner_thread_);
50 owner_loop_->RemoveDestructionObserver(this);
51 destroyed_on_owner_thread_ = true;
52 }
53
54 namespace {
55
56 // TODO(akalin): Merge with similar function in
57 // js_transaction_observer.cc.
58 std::string GetLocationString(const tracked_objects::Location& location) {
59 std::ostringstream oss;
60 oss << location.function_name() << "@"
61 << location.file_name() << ":" << location.line_number();
62 return oss.str();
63 }
64
65 } // namespace
66
67 void WeakHandleCoreBase::PostToOwnerThread(
22 const tracked_objects::Location& from_here, 68 const tracked_objects::Location& from_here,
23 const base::Closure& fn) const { 69 const base::Closure& fn) const {
24 ignore_result(message_loop_proxy_->PostTask(from_here, fn)); 70 if (!owner_loop_proxy_->PostTask(from_here, fn)) {
71 VLOG(1) << "Could not post task from " << GetLocationString(from_here);
72 }
73 }
74
75 void WeakHandleCoreBase::Destroy() {
76 if (IsOnOwnerThread()) {
77 CHECK(!destroyed_on_owner_thread_);
78 CleanupAndDestroyOnOwnerThread();
79 } else if (!owner_loop_proxy_->PostTask(
80 FROM_HERE,
81 base::Bind(&WeakHandleCoreBase::CleanupAndDestroyOnOwnerThread,
82 base::Unretained(this)))) {
83 // If the post fails, that means that the owner loop is gone and
84 // therefore CleanupOnOwnerThread() should have already been
85 // called via WillDestroyCurrentMessageLoop(). Therefore, the
86 // only thing left is to self-destruct.
87 delete this;
88 }
89 }
90
91 void WeakHandleCoreBase::CleanupAndDestroyOnOwnerThread() {
92 CHECK(IsOnOwnerThread());
93 CHECK(!destroyed_on_owner_thread_);
94 CleanupOnOwnerThread();
95 CHECK(destroyed_on_owner_thread_);
96 delete this;
97 }
98
99 void WeakHandleCoreBaseTraits::Destruct(const WeakHandleCoreBase* core_base) {
100 const_cast<WeakHandleCoreBase*>(core_base)->Destroy();
25 } 101 }
26 102
27 } // namespace internal 103 } // namespace internal
28 104
29 } // namespace base 105 } // namespace base
OLDNEW
« no previous file with comments | « chrome/browser/sync/weak_handle.h ('k') | chrome/browser/sync/weak_handle_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698