Index: media/base/android/android_overlay_callback.h |
diff --git a/media/base/android/android_overlay_callback.h b/media/base/android/android_overlay_callback.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0911a52f0928c98ed2cd1957d232b0ee62a29f78 |
--- /dev/null |
+++ b/media/base/android/android_overlay_callback.h |
@@ -0,0 +1,91 @@ |
+// Copyright 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 MEDIA_BASE_ANDROID_ANDROID_OVERLAY_CALLBACK_H_ |
+#define MEDIA_BASE_ANDROID_ANDROID_OVERLAY_CALLBACK_H_ |
+ |
+#include <jni.h> |
+#include <stddef.h> |
+#include <stdint.h> |
+ |
+#include <set> |
+#include <string> |
+ |
+#include "base/android/scoped_java_ref.h" |
+#include "base/lazy_instance.h" |
+#include "base/macros.h" |
+#include "base/single_thread_task_runner.h" |
+#include "base/task_runner.h" |
+#include "base/threading/thread_checker.h" |
+#include "base/time/time.h" |
+#include "media/base/android/android_overlay_proxy.h" |
+#include "media/base/media_export.h" |
+#include "ui/gfx/geometry/point.h" |
+#include "ui/gfx/geometry/size.h" |
+ |
+namespace media { |
+ |
+// This class implements the C++ wrapper around the java AndroidOverlayCallback. |
+// Callbacks will happen on the thread that it's created on. |
+// It handles threading and object lifecycle issues. In particular, it's safe |
+// to delete this object on the same thread as it was created on at any time, |
+// even during a callback. Future callbacks from java will be cancelled |
+// automatically, regardless of java threading. |
+class MEDIA_EXPORT AndroidOverlayCallback { |
+ public: |
+ // |callback| will be called on the same thread that we're created on, |
+ // regardless of the thread that's used to call from java. |
+ explicit AndroidOverlayCallback( |
+ const AndroidOverlayProxy::Callback& callback); |
+ ~AndroidOverlayCallback(); |
+ |
+ // Callback that we should use to send the java surface when it arrives. |
+ using AndroidSurfaceCB = |
+ base::Callback<void(const base::android::JavaRef<jobject>& jsurface)>; |
+ |
+ // Sets the callback that we'll use to provide the surface to AndroidOverlay. |
+ void SetAndroidSurfaceCB(const AndroidSurfaceCB& cb); |
+ |
+ static scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner(); |
+ bool CalledOnValidThread() const; |
+ |
+ jobject obj() const { return j_surface_callback_.obj(); } |
+ |
+ // Called on the proper thread for the callback for |id|, though |id| might |
+ // no longer refer to a valid callback. |jsurface_opt| is an optional object |
+ // that goes with the callback. For OP_CREATED, it is the Android Surface. |
+ // |jcompletion_opt| is an optional IAndroidSurfaceCompletion that we'll |
+ // signal when done. |
+ static void OnCallbackProperThread( |
+ long id, |
+ AndroidOverlayProxy::CallbackOp what, |
+ base::android::ScopedJavaGlobalRef<jobject> jobject_opt, |
+ base::android::ScopedJavaGlobalRef<jobject> jcompletion_opt); |
+ |
+ static bool RegisterAndroidOverlayCallback(JNIEnv* env); |
+ |
+ private: |
+ // Java callback instance. |
+ base::android::ScopedJavaGlobalRef<jobject> j_surface_callback_; |
+ |
+ // |id_| must be non-repeating. We can't use |this| directly because of race |
+ // conditions with destruction and re-allocation. |
+ long id_; |
+ |
+ AndroidOverlayProxy::Callback callback_; |
+ |
+ base::ThreadChecker thread_checker_; |
+ |
+ AndroidSurfaceCB android_surface_cb_; |
+ |
+ // Task runner of the first callback that uses us. We'll use this for all |
+ // callbacks on all task runners. |
+ static scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(AndroidOverlayCallback); |
+}; |
+ |
+} // namespace media |
+ |
+#endif // MEDIA_BASE_ANDROID_ANDROID_OVERLAY_CALLBACK_H_ |