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

Unified Diff: content/browser/android/scoped_surface_request_manager.cc

Issue 2285593002: Add ScopedSurfaceRequestManager (Closed)
Patch Set: Fixing comments. Created 4 years, 3 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/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..4180bd7403a743768d4263161f5073902de69713
--- /dev/null
+++ b/content/browser/android/scoped_surface_request_manager.cc
@@ -0,0 +1,101 @@
+// 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();
+}
+
+std::string ScopedSurfaceRequestManager::RegisterScopedSurfaceRequest(
+ ScopedSurfaceRequestCB request_cb) {
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ DCHECK(!request_cb.is_null());
+
+ std::string request_token = GetBase64UnguessableToken();
+
+ DCHECK(!request_callbacks_.count(request_token));
+ request_callbacks_.insert(std::make_pair(request_token, request_cb));
+
+ return request_token;
+}
+
+void ScopedSurfaceRequestManager::UnregisterScopedSurfaceRequest(
+ std::string request_token) {
dcheng 2016/09/09 20:51:08 Strings (with few exceptions) should be passed by
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ GetAndUnregisterInternal(request_token);
+}
+
+ScopedSurfaceRequestManager::ScopedSurfaceRequestCB
+ScopedSurfaceRequestManager::GetAndUnregisterInternal(
+ std::string request_token) {
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ DCHECK(kPaddedStringTokenSize == request_token.length());
dcheng 2016/09/09 20:51:07 Nit: DCHECK_EQ, but this is not a safe assumption,
+
+ 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(
+ std::string request_token,
+ gl::SurfaceTexture* surface_texture) {
+ gl::ScopedJavaSurface surface(surface_texture);
+ FulfillScopedSurfaceRequest(request_token, std::move(surface));
+}
+
+void ScopedSurfaceRequestManager::FulfillScopedSurfaceRequest(
+ std::string request_token,
+ gl::ScopedJavaSurface surface) {
+ if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
+ // 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));
+}
+
+std::string ScopedSurfaceRequestManager::GetBase64UnguessableToken() {
+ char token_bits[kRequestTokenSize];
+ crypto::RandBytes(token_bits, sizeof(token_bits));
+
+ std::string result;
+ base::Base64Encode(base::StringPiece(token_bits, kRequestTokenSize), &result);
+ return result;
+}
+
+ScopedSurfaceRequestManager::ScopedSurfaceRequestManager() {}
+
+ScopedSurfaceRequestManager::~ScopedSurfaceRequestManager() {}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698