OLD | NEW |
---|---|
(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 base::Nonce ScopedSurfaceRequestManager::RegisterScopedSurfaceRequest( | |
25 ScopedSurfaceRequestCB request_cb) { | |
26 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
27 DCHECK(!request_cb.is_null()); | |
28 | |
29 base::Nonce request_token = base::Nonce::Generate(); | |
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 base::Nonce request_token) { | |
39 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
40 GetAndUnregisterInternal(request_token); | |
41 } | |
42 | |
43 ScopedSurfaceRequestManager::ScopedSurfaceRequestCB | |
44 ScopedSurfaceRequestManager::GetAndUnregisterInternal( | |
45 base::Nonce request_token) { | |
46 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
47 | |
48 ScopedSurfaceRequestManager::ScopedSurfaceRequestCB request; | |
49 | |
50 auto it = request_callbacks_.find(request_token); | |
51 if (it != request_callbacks_.end()) { | |
52 request = it->second; | |
53 request_callbacks_.erase(it); | |
54 } | |
55 | |
56 return request; | |
57 } | |
58 | |
59 void ScopedSurfaceRequestManager::ForwardSurfaceTextureForSurfaceRequest( | |
60 base::Nonce request_token, | |
61 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.
| |
62 gl::ScopedJavaSurface surface(surface_texture); | |
63 FulfillScopedSurfaceRequest(request_token, std::move(surface)); | |
64 } | |
65 | |
66 void ScopedSurfaceRequestManager::FulfillScopedSurfaceRequest( | |
67 base::Nonce request_token, | |
68 gl::ScopedJavaSurface surface) { | |
69 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,
| |
70 // base::Unretained is safe because the lifetime of this object is tied to | |
71 // the lifetime of the browser process. | |
72 BrowserThread::PostTask( | |
73 BrowserThread::UI, FROM_HERE, | |
74 base::Bind(&ScopedSurfaceRequestManager::FulfillScopedSurfaceRequest, | |
75 base::Unretained(this), request_token, | |
76 base::Passed(std::move(surface)))); | |
77 return; | |
78 } | |
79 | |
80 ScopedSurfaceRequestManager::ScopedSurfaceRequestCB request = | |
81 GetAndUnregisterInternal(request_token); | |
82 | |
83 if (!request.is_null()) | |
84 request.Run(std::move(surface)); | |
85 } | |
86 | |
87 ScopedSurfaceRequestManager::ScopedSurfaceRequestManager() {} | |
88 | |
89 ScopedSurfaceRequestManager::~ScopedSurfaceRequestManager() {} | |
90 | |
91 } // namespace content | |
OLD | NEW |