Chromium Code Reviews| 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 #ifndef COMPONENTS_SYNC_BASE_WEAK_HANDLE_H_ | 5 #ifndef COMPONENTS_SYNC_BASE_WEAK_HANDLE_H_ |
| 6 #define COMPONENTS_SYNC_BASE_WEAK_HANDLE_H_ | 6 #define COMPONENTS_SYNC_BASE_WEAK_HANDLE_H_ |
| 7 | 7 |
| 8 #include <cstddef> | 8 #include <cstddef> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
| 14 #include "base/location.h" | 14 #include "base/location.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 18 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
| 19 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
| 20 | 20 |
| 21 // Weak handles provides a way to refer to weak pointers from another | 21 // SEQUENCE_CHECKER(sequence_checker_); |
|
stanisc
2017/05/30 17:00:08
It looks like the script didn't handle this correc
gab
2017/05/31 17:42:23
Whoops..! Spent a lot of time to make the script h
| |
| 22 //// Weak handles provides a way to refer to weak pointers from another | |
| 22 // thread. This is useful because it is not safe to reference a weak | 23 // thread. This is useful because it is not safe to reference a weak |
| 23 // pointer from a thread other than the thread on which it was | 24 // pointer from a thread other than the thread on which it was |
| 24 // created. | 25 // created. |
| 25 // | 26 // |
| 26 // Weak handles can be passed across threads, so for example, you can | 27 // Weak handles can be passed across threads, so for example, you can |
| 27 // use them to do the "real" work on one thread and get notified on | 28 // use them to do the "real" work on one thread and get notified on |
| 28 // another thread: | 29 // another thread: |
| 29 // | 30 // |
| 30 // class FooIOWorker { | 31 // class FooIOWorker { |
| 31 // public: | 32 // public: |
| 32 // FooIOWorker(const WeakHandle<Foo>& foo) : foo_(foo) {} | 33 // FooIOWorker(const WeakHandle<Foo>& foo) : foo_(foo) {} |
| 33 // | 34 // |
| 34 // void OnIOStart() { | 35 // void OnIOStart() { |
| 35 // foo_.Call(FROM_HERE, &Foo::OnIOStart); | 36 // foo_.Call(FROM_HERE, &Foo::OnIOStart); |
| 36 // } | 37 // } |
| 37 // | 38 // |
| 38 // void OnIOEvent(IOEvent e) { | 39 // void OnIOEvent(IOEvent e) { |
| 39 // foo_.Call(FROM_HERE, &Foo::OnIOEvent, e); | 40 // foo_.Call(FROM_HERE, &Foo::OnIOEvent, e); |
| 40 // } | 41 // } |
| 41 // | 42 // |
| 42 // void OnIOError(IOError err) { | 43 // void OnIOError(IOError err) { |
| 43 // foo_.Call(FROM_HERE, &Foo::OnIOError, err); | 44 // foo_.Call(FROM_HERE, &Foo::OnIOError, err); |
| 44 // } | 45 // } |
| 45 // | 46 // |
| 46 // private: | 47 // private: |
| 47 // const WeakHandle<Foo> foo_; | 48 // const WeakHandle<Foo> foo_; |
| 48 // }; | 49 // }; |
| 49 // | 50 // |
| 50 // class Foo : public SupportsWeakPtr<Foo>, public NonThreadSafe { | 51 // class Foo : public SupportsWeakPtr<Foo> { |
| 51 // public: | 52 // public: |
| 52 // Foo() { | 53 // Foo() { |
| 53 // SpawnFooIOWorkerOnIOThread(base::MakeWeakHandle(AsWeakPtr())); | 54 // SpawnFooIOWorkerOnIOThread(base::MakeWeakHandle(AsWeakPtr())); |
| 54 // } | 55 // } |
| 55 // | 56 // |
| 56 // /* Will always be called on the correct thread, and only if this | 57 // /* Will always be called on the correct thread, and only if this |
| 57 // object hasn't been destroyed. */ | 58 // object hasn't been destroyed. */ |
| 58 // void OnIOStart() { DCHECK(CalledOnValidThread(); ... } | 59 // void OnIOStart() { DCHECK(CalledOnValidThread(); ... } |
| 59 // void OnIOEvent(IOEvent e) { DCHECK(CalledOnValidThread(); ... } | 60 // void OnIOEvent(IOEvent e) { DCHECK(CalledOnValidThread(); ... } |
| 60 // void OnIOError(IOError err) { DCHECK(CalledOnValidThread(); ... } | 61 // void OnIOError(IOError err) { DCHECK(CalledOnValidThread(); ... } |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 194 | 195 |
| 195 // Makes a WeakHandle from a WeakPtr. | 196 // Makes a WeakHandle from a WeakPtr. |
| 196 template <typename T> | 197 template <typename T> |
| 197 WeakHandle<T> MakeWeakHandle(const base::WeakPtr<T>& ptr) { | 198 WeakHandle<T> MakeWeakHandle(const base::WeakPtr<T>& ptr) { |
| 198 return WeakHandle<T>(ptr); | 199 return WeakHandle<T>(ptr); |
| 199 } | 200 } |
| 200 | 201 |
| 201 } // namespace syncer | 202 } // namespace syncer |
| 202 | 203 |
| 203 #endif // COMPONENTS_SYNC_BASE_WEAK_HANDLE_H_ | 204 #endif // COMPONENTS_SYNC_BASE_WEAK_HANDLE_H_ |
| OLD | NEW |