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)
97 DesktopMediaID
DesktopMediaID::Parse(const std::string
& str
) {
98 std::vector
<std::string
> parts
= base::SplitString(
99 str
, ":", base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
);
101 #if defined(USE_AURA)
102 if (parts
.size() != 3)
103 return DesktopMediaID();
105 if (parts
.size() != 2)
106 return DesktopMediaID();
109 Type type
= TYPE_NONE
;
110 if (parts
[0] == kScreenPrefix
) {
112 } else if (parts
[0] == kWindowPrefix
) {
115 return DesktopMediaID();
119 if (!base::StringToInt64(parts
[1], &id
))
120 return DesktopMediaID();
122 DesktopMediaID
media_id(type
, id
);
124 #if defined(USE_AURA)
126 if (!base::StringToInt64(parts
[2], &aura_id
))
127 return DesktopMediaID();
128 media_id
.aura_id
= aura_id
;
129 #endif // defined(USE_AURA)
134 std::string
DesktopMediaID::ToString() {
139 return std::string();
141 prefix
= kScreenPrefix
;
144 prefix
= kWindowPrefix
;
147 DCHECK(!prefix
.empty());
150 prefix
.append(base::Int64ToString(id
));
152 #if defined(USE_AURA)
154 prefix
.append(base::Int64ToString(aura_id
));
155 #endif // defined(USE_AURA)
160 } // namespace content