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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "content/browser/android/scoped_surface_request_manager.h"
6
7 #include "base/base64.h"
8 #include "content/public/browser/browser_thread.h"
9 #include "crypto/random.h"
10
11 namespace content {
12
13 const int kRequestTokenSize = 16;
14 // 128 bits / (6 bits per base64 char) = 22.33 chars, or 24 chars with padding.
15 const size_t kPaddedStringTokenSize = 24;
16
17 // static
18 ScopedSurfaceRequestManager* ScopedSurfaceRequestManager::GetInstance() {
19 return base::Singleton<
20 ScopedSurfaceRequestManager,
21 base::LeakySingletonTraits<ScopedSurfaceRequestManager>>::get();
22 }
23
24 std::string ScopedSurfaceRequestManager::RegisterScopedSurfaceRequest(
25 ScopedSurfaceRequestCB request_cb) {
26 DCHECK_CURRENTLY_ON(BrowserThread::UI);
27 DCHECK(!request_cb.is_null());
28
29 std::string request_token = GetBase64UnguessableToken();
30
31 DCHECK(!request_callbacks_.count(request_token));
32 request_callbacks_.insert(std::make_pair(request_token, request_cb));
33
34 return request_token;
35 }
36
37 void ScopedSurfaceRequestManager::UnregisterScopedSurfaceRequest(
38 std::string request_token) {
dcheng 2016/09/09 20:51:08 Strings (with few exceptions) should be passed by
39 DCHECK_CURRENTLY_ON(BrowserThread::UI);
40 GetAndUnregisterInternal(request_token);
41 }
42
43 ScopedSurfaceRequestManager::ScopedSurfaceRequestCB
44 ScopedSurfaceRequestManager::GetAndUnregisterInternal(
45 std::string request_token) {
46 DCHECK_CURRENTLY_ON(BrowserThread::UI);
47 DCHECK(kPaddedStringTokenSize == request_token.length());
dcheng 2016/09/09 20:51:07 Nit: DCHECK_EQ, but this is not a safe assumption,
48
49 ScopedSurfaceRequestManager::ScopedSurfaceRequestCB request;
50
51 auto it = request_callbacks_.find(request_token);
52 if (it != request_callbacks_.end()) {
53 request = it->second;
54 request_callbacks_.erase(it);
55 }
56
57 return request;
58 }
59
60 void ScopedSurfaceRequestManager::ForwardSurfaceTextureForSurfaceRequest(
61 std::string request_token,
62 gl::SurfaceTexture* surface_texture) {
63 gl::ScopedJavaSurface surface(surface_texture);
64 FulfillScopedSurfaceRequest(request_token, std::move(surface));
65 }
66
67 void ScopedSurfaceRequestManager::FulfillScopedSurfaceRequest(
68 std::string request_token,
69 gl::ScopedJavaSurface surface) {
70 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
71 // base::Unretained is safe because the lifetime of this object is tied to
72 // the lifetime of the browser process.
73 BrowserThread::PostTask(
74 BrowserThread::UI, FROM_HERE,
75 base::Bind(&ScopedSurfaceRequestManager::FulfillScopedSurfaceRequest,
76 base::Unretained(this), request_token,
77 base::Passed(std::move(surface))));
78 return;
79 }
80
81 ScopedSurfaceRequestManager::ScopedSurfaceRequestCB request =
82 GetAndUnregisterInternal(request_token);
83
84 if (!request.is_null())
85 request.Run(std::move(surface));
86 }
87
88 std::string ScopedSurfaceRequestManager::GetBase64UnguessableToken() {
89 char token_bits[kRequestTokenSize];
90 crypto::RandBytes(token_bits, sizeof(token_bits));
91
92 std::string result;
93 base::Base64Encode(base::StringPiece(token_bits, kRequestTokenSize), &result);
94 return result;
95 }
96
97 ScopedSurfaceRequestManager::ScopedSurfaceRequestManager() {}
98
99 ScopedSurfaceRequestManager::~ScopedSurfaceRequestManager() {}
100
101 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698