OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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/wake_lock/wake_lock_context_host.h" | |
6 | |
7 #include "base/atomic_sequence_num.h" | |
8 #include "base/lazy_instance.h" | |
9 #include "content/public/common/service_manager_connection.h" | |
10 #include "device/wake_lock/public/interfaces/wake_lock_context_provider.mojom.h" | |
11 #include "mojo/public/cpp/bindings/strong_binding.h" | |
12 #include "services/device/public/interfaces/constants.mojom.h" | |
13 #include "services/service_manager/public/cpp/connector.h" | |
14 | |
15 namespace content { | |
16 | |
17 namespace { | |
18 | |
19 base::StaticAtomicSequenceNumber g_unique_id; | |
20 | |
21 base::LazyInstance<std::map<int, WakeLockContextHost*>>::DestructorAtExit | |
dcheng
2017/03/17 06:55:25
Does this need to be DestructorAtExit?
blundell
2017/03/17 12:28:21
I cargo-culted from here: https://cs.chromium.org/
| |
22 g_id_to_context_host = LAZY_INSTANCE_INITIALIZER; | |
23 | |
24 WakeLockContextHost* ContextHostFromId(int id) { | |
25 auto it = g_id_to_context_host.Get().find(id); | |
26 return it != g_id_to_context_host.Get().end() ? it->second : nullptr; | |
27 } | |
28 | |
29 } // namespace | |
30 | |
31 WakeLockContextHost::WakeLockContextHost(WebContents* web_contents) | |
32 : id_(g_unique_id.GetNext()), web_contents_(web_contents) { | |
33 g_id_to_context_host.Get()[id_] = this; | |
34 | |
35 // Connect to a WakeLockContext, associating it with |id_|. | |
36 if (ServiceManagerConnection::GetForProcess()) { | |
dcheng
2017/03/17 06:55:25
Out of curiosity, why is this check needed at all?
blundell
2017/03/17 12:28:21
In unittests the service manager connection is not
dcheng
2017/03/22 10:09:55
Out of curiosity, is there any chance to add some
blundell
2017/03/22 13:03:40
Filed crbug.com/704098 to track this.
| |
37 service_manager::Connector* connector = | |
38 ServiceManagerConnection::GetForProcess()->GetConnector(); | |
39 DCHECK(connector); | |
40 device::mojom::WakeLockContextProviderPtr context_provider; | |
41 connector->BindInterface(device::mojom::kServiceName, | |
42 mojo::MakeRequest(&context_provider)); | |
43 context_provider->GetContext(id_, mojo::MakeRequest(&wake_lock_context_)); | |
44 } | |
45 } | |
46 | |
47 WakeLockContextHost::~WakeLockContextHost() { | |
48 g_id_to_context_host.Get()[id_] = nullptr; | |
dcheng
2017/03/17 06:55:25
Nit: erase()?
blundell
2017/03/17 12:28:21
Done.
| |
49 } | |
50 | |
51 // static | |
52 gfx::NativeView WakeLockContextHost::GetNativeViewForContext(int context_id) { | |
53 WakeLockContextHost* context_host = ContextHostFromId(context_id); | |
54 if (context_host) | |
55 return context_host->web_contents_->GetNativeView(); | |
56 return nullptr; | |
57 } | |
58 | |
59 } // namespace content | |
OLD | NEW |