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

Side by Side Diff: content/browser/android/scoped_surface_request_manager.cc

Issue 2285593002: Add ScopedSurfaceRequestManager (Closed)
Patch Set: Addressing 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 "content/public/browser/browser_thread.h"
8 #include "crypto/random.h"
9
10 namespace content {
11
12 // static
13 ScopedSurfaceRequestManager* ScopedSurfaceRequestManager::GetInstance() {
14 return base::Singleton<
15 ScopedSurfaceRequestManager,
16 base::LeakySingletonTraits<ScopedSurfaceRequestManager>>::get();
17 }
18
19 uint64_t ScopedSurfaceRequestManager::RegisterScopedSurfaceRequest(
20 ScopedSurfaceRequestCB request_cb) {
21 DCHECK_CURRENTLY_ON(BrowserThread::UI);
22
23 uint64_t request_token;
24 crypto::RandBytes(&request_token, sizeof(request_token));
25
26 DCHECK(!request_callbacks_.count(request_token));
27 request_callbacks_.insert(std::make_pair(request_token, request_cb));
28
29 return request_token;
30 }
31
32 void ScopedSurfaceRequestManager::UnregisterScopedSurfaceRequest(
33 uint64_t request_token) {
34 DCHECK_CURRENTLY_ON(BrowserThread::UI);
35 GetAndUnregisterInternal(request_token);
36 }
37
38 ScopedSurfaceRequestManager::ScopedSurfaceRequestCB
39 ScopedSurfaceRequestManager::GetAndUnregisterInternal(uint64_t request_token) {
40 DCHECK_CURRENTLY_ON(BrowserThread::UI);
41
42 ScopedSurfaceRequestManager::ScopedSurfaceRequestCB request;
43
44 auto it = request_callbacks_.find(request_token);
45 if (it != request_callbacks_.end()) {
46 request = it->second;
47 request_callbacks_.erase(it);
48 }
49
50 return request;
51 }
52
53 void ScopedSurfaceRequestManager::ForwardSurfaceTextureForSurfaceRequest(
54 uint64_t request_token,
55 gl::SurfaceTexture* surface_texture) {
56 gl::ScopedJavaSurface surface(surface_texture);
57 FulfillScopedSurfaceRequest(request_token, std::move(surface));
58 }
59
60 void ScopedSurfaceRequestManager::FulfillScopedSurfaceRequest(
61 uint64_t request_token,
62 gl::ScopedJavaSurface surface) {
63 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
64 // base::Unretained is safe because the lifetime of this object is tied to
65 // the lifetime of the browser process.
66 BrowserThread::PostTask(
67 BrowserThread::UI, FROM_HERE,
68 base::Bind(&ScopedSurfaceRequestManager::FulfillScopedSurfaceRequest,
69 base::Unretained(this), request_token,
70 base::Passed(std::move(surface))));
71 return;
72 }
73
74 ScopedSurfaceRequestManager::ScopedSurfaceRequestCB request =
75 GetAndUnregisterInternal(request_token);
76
77 if (!request.is_null()) {
watk 2016/08/31 00:47:35 remove braces for single line statement
tguilbert 2016/08/31 17:58:56 Done.
78 request.Run(std::move(surface));
79 }
80 }
81
82 ScopedSurfaceRequestManager::ScopedSurfaceRequestManager() {}
83
84 ScopedSurfaceRequestManager::~ScopedSurfaceRequestManager() {}
85
86 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698