| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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 // Weak handles provides a way to refer to weak pointers from another | 5 // Weak handles provides a way to refer to weak pointers from another |
| 6 // thread. This is useful because it is not safe to reference a weak | 6 // thread. This is useful because it is not safe to reference a weak |
| 7 // pointer from a thread other than the thread on which it was | 7 // pointer from a thread other than the thread on which it was |
| 8 // created. | 8 // created. |
| 9 // | 9 // |
| 10 // Weak handles can be passed across threads, so for example, you can | 10 // Weak handles can be passed across threads, so for example, you can |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 | 51 |
| 52 #include "base/basictypes.h" | 52 #include "base/basictypes.h" |
| 53 #include "base/bind.h" | 53 #include "base/bind.h" |
| 54 #include "base/callback_forward.h" | 54 #include "base/callback_forward.h" |
| 55 #include "base/compiler_specific.h" | 55 #include "base/compiler_specific.h" |
| 56 #include "base/gtest_prod_util.h" | 56 #include "base/gtest_prod_util.h" |
| 57 #include "base/location.h" | 57 #include "base/location.h" |
| 58 #include "base/logging.h" | 58 #include "base/logging.h" |
| 59 #include "base/memory/ref_counted.h" | 59 #include "base/memory/ref_counted.h" |
| 60 #include "base/memory/weak_ptr.h" | 60 #include "base/memory/weak_ptr.h" |
| 61 #include "sync/base/sync_export.h" |
| 61 | 62 |
| 62 namespace base { | 63 namespace base { |
| 63 class MessageLoopProxy; | 64 class MessageLoopProxy; |
| 64 } // namespace base | 65 } // namespace base |
| 65 | 66 |
| 66 namespace tracked_objects { | 67 namespace tracked_objects { |
| 67 class Location; | 68 class Location; |
| 68 } // namespace tracked_objects | 69 } // namespace tracked_objects |
| 69 | 70 |
| 70 namespace syncer { | 71 namespace syncer { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 92 typedef const T* ForwardType; | 93 typedef const T* ForwardType; |
| 93 }; | 94 }; |
| 94 | 95 |
| 95 template <typename T> | 96 template <typename T> |
| 96 struct ParamTraits<T[]> { | 97 struct ParamTraits<T[]> { |
| 97 typedef const T* ForwardType; | 98 typedef const T* ForwardType; |
| 98 }; | 99 }; |
| 99 | 100 |
| 100 // Base class for WeakHandleCore<T> to avoid template bloat. Handles | 101 // Base class for WeakHandleCore<T> to avoid template bloat. Handles |
| 101 // the interaction with the owner thread and its message loop. | 102 // the interaction with the owner thread and its message loop. |
| 102 class WeakHandleCoreBase { | 103 class SYNC_EXPORT WeakHandleCoreBase { |
| 103 public: | 104 public: |
| 104 // Assumes the current thread is the owner thread. | 105 // Assumes the current thread is the owner thread. |
| 105 WeakHandleCoreBase(); | 106 WeakHandleCoreBase(); |
| 106 | 107 |
| 107 // May be called on any thread. | 108 // May be called on any thread. |
| 108 bool IsOnOwnerThread() const; | 109 bool IsOnOwnerThread() const; |
| 109 | 110 |
| 110 protected: | 111 protected: |
| 111 // May be destroyed on any thread. | 112 // May be destroyed on any thread. |
| 112 ~WeakHandleCoreBase(); | 113 ~WeakHandleCoreBase(); |
| 113 | 114 |
| 114 // May be called on any thread. | 115 // May be called on any thread. |
| 115 void PostToOwnerThread(const tracked_objects::Location& from_here, | 116 void PostToOwnerThread(const tracked_objects::Location& from_here, |
| 116 const base::Closure& fn) const; | 117 const base::Closure& fn) const; |
| 117 | 118 |
| 118 private: | 119 private: |
| 119 // May be used on any thread. | 120 // May be used on any thread. |
| 120 const scoped_refptr<base::MessageLoopProxy> owner_loop_proxy_; | 121 const scoped_refptr<base::MessageLoopProxy> owner_loop_proxy_; |
| 121 | 122 |
| 122 DISALLOW_COPY_AND_ASSIGN(WeakHandleCoreBase); | 123 DISALLOW_COPY_AND_ASSIGN(WeakHandleCoreBase); |
| 123 }; | 124 }; |
| 124 | 125 |
| 125 // WeakHandleCore<T> contains all the logic for WeakHandle<T>. | 126 // WeakHandleCore<T> contains all the logic for WeakHandle<T>. |
| 126 template <typename T> | 127 template <typename T> |
| 127 class WeakHandleCore | 128 class WeakHandleCore |
| 128 : public NON_EXPORTED_BASE(WeakHandleCoreBase), | 129 : public WeakHandleCoreBase, |
| 129 public base::RefCountedThreadSafe<WeakHandleCore<T> > { | 130 public base::RefCountedThreadSafe<WeakHandleCore<T> > { |
| 130 public: | 131 public: |
| 131 // Must be called on |ptr|'s owner thread, which is assumed to be | 132 // Must be called on |ptr|'s owner thread, which is assumed to be |
| 132 // the current thread. | 133 // the current thread. |
| 133 explicit WeakHandleCore(const base::WeakPtr<T>& ptr) : ptr_(ptr) {} | 134 explicit WeakHandleCore(const base::WeakPtr<T>& ptr) : ptr_(ptr) {} |
| 134 | 135 |
| 135 // Must be called on |ptr_|'s owner thread. | 136 // Must be called on |ptr_|'s owner thread. |
| 136 base::WeakPtr<T> Get() const { | 137 base::WeakPtr<T> Get() const { |
| 137 CHECK(IsOnOwnerThread()); | 138 CHECK(IsOnOwnerThread()); |
| 138 return ptr_; | 139 return ptr_; |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 369 | 370 |
| 370 // Makes a WeakHandle from a WeakPtr. | 371 // Makes a WeakHandle from a WeakPtr. |
| 371 template <typename T> | 372 template <typename T> |
| 372 WeakHandle<T> MakeWeakHandle(const base::WeakPtr<T>& ptr) { | 373 WeakHandle<T> MakeWeakHandle(const base::WeakPtr<T>& ptr) { |
| 373 return WeakHandle<T>(ptr); | 374 return WeakHandle<T>(ptr); |
| 374 } | 375 } |
| 375 | 376 |
| 376 } // namespace syncer | 377 } // namespace syncer |
| 377 | 378 |
| 378 #endif // SYNC_UTIL_WEAK_HANDLE_H_ | 379 #endif // SYNC_UTIL_WEAK_HANDLE_H_ |
| OLD | NEW |