1 // Copyright 2013 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.
5 #include "content/public/browser/desktop_media_id.h"
9 #include "base/id_map.h"
10 #include "base/memory/singleton.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_split.h"
13 #include "base/strings/string_util.h"
16 #include "ui/aura/window.h"
17 #include "ui/aura/window_observer.h"
18 #endif // defined(USE_AURA)
24 class AuraWindowRegistry
: public aura::WindowObserver
{
26 static AuraWindowRegistry
* GetInstance() {
27 return Singleton
<AuraWindowRegistry
>::get();
30 int RegisterWindow(aura::Window
* window
) {
31 IDMap
<aura::Window
>::const_iterator
it(®istered_windows_
);
32 for (; !it
.IsAtEnd(); it
.Advance()) {
33 if (it
.GetCurrentValue() == window
)
34 return it
.GetCurrentKey();
37 window
->AddObserver(this);
38 return registered_windows_
.Add(window
);
41 aura::Window
* GetWindowById(int id
) {
42 return registered_windows_
.Lookup(id
);
46 friend struct DefaultSingletonTraits
<AuraWindowRegistry
>;
48 AuraWindowRegistry() {}
49 ~AuraWindowRegistry() override
{}
51 // WindowObserver overrides.
52 void OnWindowDestroying(aura::Window
* window
) override
{
53 IDMap
<aura::Window
>::iterator
it(®istered_windows_
);
54 for (; !it
.IsAtEnd(); it
.Advance()) {
55 if (it
.GetCurrentValue() == window
) {
56 registered_windows_
.Remove(it
.GetCurrentKey());
63 IDMap
<aura::Window
> registered_windows_
;
65 DISALLOW_COPY_AND_ASSIGN(AuraWindowRegistry
);
68 #endif // defined(USE_AURA)
74 const char kScreenPrefix
[] = "screen";
75 const char kWindowPrefix
[] = "window";
80 DesktopMediaID
DesktopMediaID::RegisterAuraWindow(DesktopMediaID::Type type
,
81 aura::Window
* window
) {
82 DCHECK(type
== TYPE_SCREEN
|| type
== TYPE_WINDOW
);
84 DesktopMediaID
media_id(type
, kNullId
);
85 media_id
.aura_id
= AuraWindowRegistry::GetInstance()->RegisterWindow(window
);
90 aura::Window
* DesktopMediaID::GetAuraWindowById(const DesktopMediaID
& id
) {
91 return AuraWindowRegistry::GetInstance()->GetWindowById(id
.aura_id
);
94 #endif // defined(USE_AURA)
96 DesktopMediaID::DesktopMediaID() = default;
99 DesktopMediaID
DesktopMediaID::Parse(const std::string
& str
) {
100 std::vector
<std::string
> parts
= base::SplitString(
101 str
, ":", base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
);
103 #if defined(USE_AURA)
104 if (parts
.size() != 3)
105 return DesktopMediaID();
107 if (parts
.size() != 2)
108 return DesktopMediaID();
111 Type type
= TYPE_NONE
;
112 if (parts
[0] == kScreenPrefix
) {
114 } else if (parts
[0] == kWindowPrefix
) {
117 return DesktopMediaID();
121 if (!base::StringToInt64(parts
[1], &id
))
122 return DesktopMediaID();
124 DesktopMediaID
media_id(type
, id
);
126 #if defined(USE_AURA)
128 if (!base::StringToInt64(parts
[2], &aura_id
))
129 return DesktopMediaID();
130 media_id
.aura_id
= aura_id
;
131 #endif // defined(USE_AURA)
136 std::string
DesktopMediaID::ToString() {
141 return std::string();
143 prefix
= kScreenPrefix
;
146 prefix
= kWindowPrefix
;
149 DCHECK(!prefix
.empty());
152 prefix
.append(base::Int64ToString(id
));
154 #if defined(USE_AURA)
156 prefix
.append(base::Int64ToString(aura_id
));
157 #endif // defined(USE_AURA)
162 } // namespace content