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

Unified Diff: content/public/browser/owned_interface.h

Issue 2072613003: Convert GetSearchProviderInstallState to Mojo (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Merge 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: content/public/browser/owned_interface.h
diff --git a/content/public/browser/owned_interface.h b/content/public/browser/owned_interface.h
new file mode 100644
index 0000000000000000000000000000000000000000..84be1a3151d346fb660c7751e1caacfe3846f686
--- /dev/null
+++ b/content/public/browser/owned_interface.h
@@ -0,0 +1,69 @@
+// Copyright (c) 2016 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.
+
+#ifndef CONTENT_BROWSER_OWNED_INTERFACE_H_
+#define CONTENT_BROWSER_OWNED_INTERFACE_H_
+
+#include <memory>
+
+#include "base/memory/ptr_util.h"
+#include "base/memory/ref_counted.h"
+
+namespace content {
+
+// Common base class for interface impls, allowing them to be stored in a
+// type-erased container for ownership management.
+class OwnedInterface {
+ public:
+ OwnedInterface() = default;
+ virtual ~OwnedInterface() = default;
+};
+
+template <typename InterfaceImpl>
+class DeleteOnTaskRunner {
+ public:
+ DeleteOnTaskRunner(
+ const scoped_refptr<base::SingleThreadTaskRunner>& task_runner = nullptr)
+ : task_runner_(task_runner) {}
+ void operator()(const InterfaceImpl* impl) {
+ if (task_runner_) {
+ if (!task_runner_->DeleteSoon(FROM_HERE, impl)) {
+#if defined(UNIT_TEST)
+ // Only logged under unit testing because leaks at shutdown
+ // are acceptable under normal circumstances.
+ LOG(ERROR) << "DeleteSoon failed on thread";
+#endif // UNIT_TEST
+ }
+ } else {
+ delete impl;
+ }
+ }
+ ~DeleteOnTaskRunner() = default;
+
+ private:
+ const scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
+};
+
+template <typename InterfaceImpl>
+class OwnedInterfaceImpl : public OwnedInterface {
+ public:
+ OwnedInterfaceImpl(
+ std::unique_ptr<InterfaceImpl> impl,
+ const scoped_refptr<base::SingleThreadTaskRunner>& task_runner = nullptr)
+ : impl_(impl.release(), DeleteOnTaskRunner<InterfaceImpl>(task_runner)) {}
+
+ ~OwnedInterfaceImpl() = default;
+
+ // The returned pointer is owned by this object, which must outlive it.
+ InterfaceImpl* get() { return impl_.get(); }
+
+ private:
+ std::unique_ptr<InterfaceImpl, DeleteOnTaskRunner<InterfaceImpl>> impl_;
+
+ DISALLOW_COPY_AND_ASSIGN(OwnedInterfaceImpl);
+};
+
+} // namespace content
+
+#endif // CONTENT_BROWSER_OWNED_INTERFACE_H_

Powered by Google App Engine
This is Rietveld 408576698