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/memory/singleton.h"
10 #include "base/strings/string_split.h"
13 #include "ui/aura/window.h"
14 #include "ui/aura/window_observer.h"
15 #endif // defined(USE_AURA)
21 class AuraWindowRegistry
: public aura::WindowObserver
{
23 static AuraWindowRegistry
* GetInstance() {
24 return Singleton
<AuraWindowRegistry
>::get();
27 int RegisterWindow(aura::Window
* window
) {
28 // First check if an Id is already assigned to the |window|.
29 std::map
<aura::Window
*, int>::iterator it
= window_to_id_map_
.find(window
);
30 if (it
!= window_to_id_map_
.end()) {
34 // If the windows doesn't have an Id yet assign it.
38 next_id_
= (next_id_
== INT_MAX
) ? 1 : (next_id_
+ 1);
39 } while (id_to_window_map_
.find(id
) != id_to_window_map_
.end());
41 window_to_id_map_
[window
] = id
;
42 id_to_window_map_
[id
] = window
;
43 window
->AddObserver(this);
47 aura::Window
* GetWindowById(int id
) {
48 std::map
<int, aura::Window
*>::iterator it
= id_to_window_map_
.find(id
);
49 return (it
!= id_to_window_map_
.end()) ? it
->second
: nullptr;
53 friend struct DefaultSingletonTraits
<AuraWindowRegistry
>;
58 ~AuraWindowRegistry() override
{}
60 // WindowObserver overrides.
61 void OnWindowDestroying(aura::Window
* window
) override
{
62 std::map
<aura::Window
*, int>::iterator it
= window_to_id_map_
.find(window
);
63 DCHECK(it
!= window_to_id_map_
.end());
64 id_to_window_map_
.erase(it
->second
);
65 window_to_id_map_
.erase(it
);
69 std::map
<aura::Window
*, int> window_to_id_map_
;
70 std::map
<int, aura::Window
*> id_to_window_map_
;
72 DISALLOW_COPY_AND_ASSIGN(AuraWindowRegistry
);
75 #endif // defined(USE_AURA)
81 const char kScreenPrefix
[] = "screen";
82 const char kWindowPrefix
[] = "window";
83 const char kAuraWindowPrefix
[] = "aura_window";
88 DesktopMediaID
DesktopMediaID::RegisterAuraWindow(aura::Window
* window
) {
89 return DesktopMediaID(
91 AuraWindowRegistry::GetInstance()->RegisterWindow(window
));
95 aura::Window
* DesktopMediaID::GetAuraWindowById(const DesktopMediaID
& id
) {
96 DCHECK_EQ(id
.type
, TYPE_AURA_WINDOW
);
97 return AuraWindowRegistry::GetInstance()->GetWindowById(id
.id
);
100 #endif // defined(USE_AURA)
103 DesktopMediaID
DesktopMediaID::Parse(const std::string
& str
) {
104 std::vector
<std::string
> parts
;
105 base::SplitString(str
, ':', &parts
);
107 if (parts
.size() != 2)
108 return DesktopMediaID(TYPE_NONE
, 0);
110 Type type
= TYPE_NONE
;
111 if (parts
[0] == kScreenPrefix
) {
113 } else if (parts
[0] == kWindowPrefix
) {
115 } else if (parts
[0] == kAuraWindowPrefix
) {
116 type
= TYPE_AURA_WINDOW
;
118 return DesktopMediaID(TYPE_NONE
, 0);
122 if (!base::StringToInt64(parts
[1], &id
))
123 return DesktopMediaID(TYPE_NONE
, 0);
125 return DesktopMediaID(type
, id
);
128 std::string
DesktopMediaID::ToString() {
133 return std::string();
135 prefix
= kScreenPrefix
;
138 prefix
= kWindowPrefix
;
140 case TYPE_AURA_WINDOW
:
141 prefix
= kAuraWindowPrefix
;
144 DCHECK(!prefix
.empty());
147 prefix
.append(base::Int64ToString(id
));
152 } // namespace content