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

Unified Diff: sync/internal_api/public/util/weak_handle.h

Issue 2130453004: [Sync] Move //sync to //components/sync. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase. 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 side-by-side diff with in-line comments
Download patch
Index: sync/internal_api/public/util/weak_handle.h
diff --git a/sync/internal_api/public/util/weak_handle.h b/sync/internal_api/public/util/weak_handle.h
deleted file mode 100644
index 2712c8a0cdb6deb9cc255d85f067a35935e92dbf..0000000000000000000000000000000000000000
--- a/sync/internal_api/public/util/weak_handle.h
+++ /dev/null
@@ -1,209 +0,0 @@
-// Copyright 2012 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.
-
-// Weak handles provides a way to refer to weak pointers from another
-// thread. This is useful because it is not safe to reference a weak
-// pointer from a thread other than the thread on which it was
-// created.
-//
-// Weak handles can be passed across threads, so for example, you can
-// use them to do the "real" work on one thread and get notified on
-// another thread:
-//
-// class FooIOWorker {
-// public:
-// FooIOWorker(const WeakHandle<Foo>& foo) : foo_(foo) {}
-//
-// void OnIOStart() {
-// foo_.Call(FROM_HERE, &Foo::OnIOStart);
-// }
-//
-// void OnIOEvent(IOEvent e) {
-// foo_.Call(FROM_HERE, &Foo::OnIOEvent, e);
-// }
-//
-// void OnIOError(IOError err) {
-// foo_.Call(FROM_HERE, &Foo::OnIOError, err);
-// }
-//
-// private:
-// const WeakHandle<Foo> foo_;
-// };
-//
-// class Foo : public SupportsWeakPtr<Foo>, public NonThreadSafe {
-// public:
-// Foo() {
-// SpawnFooIOWorkerOnIOThread(base::MakeWeakHandle(AsWeakPtr()));
-// }
-//
-// /* Will always be called on the correct thread, and only if this
-// object hasn't been destroyed. */
-// void OnIOStart() { DCHECK(CalledOnValidThread(); ... }
-// void OnIOEvent(IOEvent e) { DCHECK(CalledOnValidThread(); ... }
-// void OnIOError(IOError err) { DCHECK(CalledOnValidThread(); ... }
-// };
-
-#ifndef SYNC_INTERNAL_API_PUBLIC_UTIL_WEAK_HANDLE_H_
-#define SYNC_INTERNAL_API_PUBLIC_UTIL_WEAK_HANDLE_H_
-
-#include <cstddef>
-#include <utility>
-
-#include "base/bind.h"
-#include "base/compiler_specific.h"
-#include "base/gtest_prod_util.h"
-#include "base/location.h"
-#include "base/logging.h"
-#include "base/macros.h"
-#include "base/memory/ref_counted.h"
-#include "base/memory/weak_ptr.h"
-#include "base/single_thread_task_runner.h"
-#include "sync/base/sync_export.h"
-
-namespace tracked_objects {
-class Location;
-} // namespace tracked_objects
-
-namespace syncer {
-
-template <typename T> class WeakHandle;
-
-namespace internal {
-// These classes are part of the WeakHandle implementation. DO NOT
-// USE THESE CLASSES DIRECTLY YOURSELF.
-
-// Base class for WeakHandleCore<T> to avoid template bloat. Handles
-// the interaction with the owner thread and its message loop.
-class SYNC_EXPORT WeakHandleCoreBase {
- public:
- // Assumes the current thread is the owner thread.
- WeakHandleCoreBase();
-
- // May be called on any thread.
- bool IsOnOwnerThread() const;
-
- protected:
- // May be destroyed on any thread.
- ~WeakHandleCoreBase();
-
- // May be called on any thread.
- void PostToOwnerThread(const tracked_objects::Location& from_here,
- const base::Closure& fn) const;
-
- private:
- // May be used on any thread.
- const scoped_refptr<base::SingleThreadTaskRunner> owner_loop_task_runner_;
-
- DISALLOW_COPY_AND_ASSIGN(WeakHandleCoreBase);
-};
-
-// WeakHandleCore<T> contains all the logic for WeakHandle<T>.
-template <typename T>
-class WeakHandleCore
- : public WeakHandleCoreBase,
- public base::RefCountedThreadSafe<WeakHandleCore<T> > {
- public:
- // Must be called on |ptr|'s owner thread, which is assumed to be
- // the current thread.
- explicit WeakHandleCore(const base::WeakPtr<T>& ptr) : ptr_(ptr) {}
-
- // Must be called on |ptr_|'s owner thread.
- base::WeakPtr<T> Get() const {
- CHECK(IsOnOwnerThread());
- return ptr_;
- }
-
- // Call(...) may be called on any thread, but all its arguments
- // should be safe to be bound and copied across threads.
- template <typename Method, typename... Args>
- void Call(const tracked_objects::Location& from_here,
- Method method, Args&&... args) const {
- PostToOwnerThread(
- from_here, base::Bind(method, ptr_, std::forward<Args>(args)...));
- }
-
- private:
- friend class base::RefCountedThreadSafe<WeakHandleCore<T> >;
-
- // May be destroyed on any thread.
- ~WeakHandleCore() {}
-
- // Must be dereferenced only on the owner thread. May be destroyed
- // from any thread.
- base::WeakPtr<T> ptr_;
-
- DISALLOW_COPY_AND_ASSIGN(WeakHandleCore);
-};
-
-} // namespace internal
-
-// May be destroyed on any thread.
-// Copying and assignment are welcome.
-template <typename T>
-class WeakHandle {
- public:
- // Creates an uninitialized WeakHandle.
- WeakHandle() {}
-
- // Creates an initialized WeakHandle from |ptr|.
- explicit WeakHandle(const base::WeakPtr<T>& ptr)
- : core_(new internal::WeakHandleCore<T>(ptr)) {}
-
- // Allow conversion from WeakHandle<U> to WeakHandle<T> if U is
- // convertible to T, but we *must* be on |other|'s owner thread.
- // Note that this doesn't override the regular copy constructor, so
- // that one can be called on any thread.
- template <typename U>
- WeakHandle(const WeakHandle<U>& other) // NOLINT
- : core_(
- other.IsInitialized() ?
- new internal::WeakHandleCore<T>(other.Get()) :
- NULL) {}
-
- // Returns true iff this WeakHandle is initialized. Note that being
- // initialized isn't a guarantee that the underlying object is still
- // alive.
- bool IsInitialized() const {
- return core_.get() != NULL;
- }
-
- // Resets to an uninitialized WeakHandle.
- void Reset() {
- core_ = NULL;
- }
-
- // Must be called only on the underlying object's owner thread.
- base::WeakPtr<T> Get() const {
- CHECK(IsInitialized());
- CHECK(core_->IsOnOwnerThread());
- return core_->Get();
- }
-
- // Call(...) may be called on any thread, but all its arguments
- // should be safe to be bound and copied across threads.
- template <typename Method, typename... Args>
- void Call(const tracked_objects::Location& from_here,
- Method method, Args&&... args) const {
- CHECK(IsInitialized());
- core_->Call(from_here, method, std::forward<Args>(args)...);
- }
-
- private:
- FRIEND_TEST_ALL_PREFIXES(WeakHandleTest,
- TypeConversionConstructor);
- FRIEND_TEST_ALL_PREFIXES(WeakHandleTest,
- TypeConversionConstructorAssignment);
-
- scoped_refptr<internal::WeakHandleCore<T> > core_;
-};
-
-// Makes a WeakHandle from a WeakPtr.
-template <typename T>
-WeakHandle<T> MakeWeakHandle(const base::WeakPtr<T>& ptr) {
- return WeakHandle<T>(ptr);
-}
-
-} // namespace syncer
-
-#endif // SYNC_INTERNAL_API_PUBLIC_UTIL_WEAK_HANDLE_H_
« no previous file with comments | « sync/internal_api/public/util/unrecoverable_error_info.cc ('k') | sync/internal_api/public/util/weak_handle.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698