Index: content/browser/android/scoped_surface_request_manager.cc |
diff --git a/content/browser/android/scoped_surface_request_manager.cc b/content/browser/android/scoped_surface_request_manager.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..77a587c28318e5d83a3b1ff688c18311d308d85c |
--- /dev/null |
+++ b/content/browser/android/scoped_surface_request_manager.cc |
@@ -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. |
+ |
+#include "content/browser/android/scoped_surface_request_manager.h" |
+ |
+#include "base/base64.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "crypto/random.h" |
+ |
+namespace content { |
+ |
+const int kRequestTokenSize = 16; |
+// 128 bits / (6 bits per base64 char) = 22.33 chars, or 24 chars with padding. |
+const size_t kPaddedStringTokenSize = 24; |
+ |
+// static |
+ScopedSurfaceRequestManager* ScopedSurfaceRequestManager::GetInstance() { |
+ return base::Singleton< |
+ ScopedSurfaceRequestManager, |
+ base::LeakySingletonTraits<ScopedSurfaceRequestManager>>::get(); |
+} |
+ |
+base::Nonce ScopedSurfaceRequestManager::RegisterScopedSurfaceRequest( |
+ ScopedSurfaceRequestCB request_cb) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ DCHECK(!request_cb.is_null()); |
+ |
+ base::Nonce request_token = base::Nonce::Generate(); |
+ |
+ DCHECK(!request_callbacks_.count(request_token)); |
+ request_callbacks_.insert(std::make_pair(request_token, request_cb)); |
+ |
+ return request_token; |
+} |
+ |
+void ScopedSurfaceRequestManager::UnregisterScopedSurfaceRequest( |
+ base::Nonce request_token) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ GetAndUnregisterInternal(request_token); |
+} |
+ |
+ScopedSurfaceRequestManager::ScopedSurfaceRequestCB |
+ScopedSurfaceRequestManager::GetAndUnregisterInternal( |
+ base::Nonce request_token) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ |
+ ScopedSurfaceRequestManager::ScopedSurfaceRequestCB request; |
+ |
+ auto it = request_callbacks_.find(request_token); |
+ if (it != request_callbacks_.end()) { |
+ request = it->second; |
+ request_callbacks_.erase(it); |
+ } |
+ |
+ return request; |
+} |
+ |
+void ScopedSurfaceRequestManager::ForwardSurfaceTextureForSurfaceRequest( |
+ base::Nonce request_token, |
+ gl::SurfaceTexture* surface_texture) { |
no sievers
2016/09/13 23:25:56
nit: should be |const gl::SurfaceTexture*|
tguilbert
2016/09/20 03:08:40
Done.
|
+ gl::ScopedJavaSurface surface(surface_texture); |
+ FulfillScopedSurfaceRequest(request_token, std::move(surface)); |
+} |
+ |
+void ScopedSurfaceRequestManager::FulfillScopedSurfaceRequest( |
+ base::Nonce request_token, |
+ gl::ScopedJavaSurface surface) { |
+ if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
no sievers
2016/09/13 23:25:56
You might want to DCHECK() that this is *not* call
tguilbert
2016/09/20 03:08:40
I changed this to always post onto the UI thread,
|
+ // base::Unretained is safe because the lifetime of this object is tied to |
+ // the lifetime of the browser process. |
+ BrowserThread::PostTask( |
+ BrowserThread::UI, FROM_HERE, |
+ base::Bind(&ScopedSurfaceRequestManager::FulfillScopedSurfaceRequest, |
+ base::Unretained(this), request_token, |
+ base::Passed(std::move(surface)))); |
+ return; |
+ } |
+ |
+ ScopedSurfaceRequestManager::ScopedSurfaceRequestCB request = |
+ GetAndUnregisterInternal(request_token); |
+ |
+ if (!request.is_null()) |
+ request.Run(std::move(surface)); |
+} |
+ |
+ScopedSurfaceRequestManager::ScopedSurfaceRequestManager() {} |
+ |
+ScopedSurfaceRequestManager::~ScopedSurfaceRequestManager() {} |
+ |
+} // namespace content |