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.
36 window_to_id_map_
[window
] = id
;
37 id_to_window_map_
[id
] = window
;
38 window
->AddObserver(this);
42 aura::Window
* GetWindowById(int id
) {
43 std::map
<int, aura::Window
*>::iterator it
= id_to_window_map_
.find(id
);
44 return (it
!= id_to_window_map_
.end()) ? it
->second
: NULL
;
48 friend struct DefaultSingletonTraits
<AuraWindowRegistry
>;
53 virtual ~AuraWindowRegistry() {}
55 // WindowObserver overrides.
56 virtual void OnWindowDestroying(aura::Window
* window
) OVERRIDE
{
57 std::map
<aura::Window
*, int>::iterator it
= window_to_id_map_
.find(window
);
58 DCHECK(it
!= window_to_id_map_
.end());
59 id_to_window_map_
.erase(it
->second
);
60 window_to_id_map_
.erase(it
);
64 std::map
<aura::Window
*, int> window_to_id_map_
;
65 std::map
<int, aura::Window
*> id_to_window_map_
;
67 DISALLOW_COPY_AND_ASSIGN(AuraWindowRegistry
);
70 #endif // defined(USE_AURA)
76 const char kScreenPrefix
[] = "screen";
77 const char kWindowPrefix
[] = "window";
78 const char kAuraWindowPrefix
[] = "aura_window";
83 DesktopMediaID
DesktopMediaID::RegisterAuraWindow(aura::Window
* window
) {
84 return DesktopMediaID(
86 AuraWindowRegistry::GetInstance()->RegisterWindow(window
));
90 aura::Window
* DesktopMediaID::GetAuraWindowById(const DesktopMediaID
& id
) {
91 DCHECK_EQ(id
.type
, TYPE_AURA_WINDOW
);
92 return AuraWindowRegistry::GetInstance()->GetWindowById(id
.id
);
95 #endif // defined(USE_AURA)
98 DesktopMediaID
DesktopMediaID::Parse(const std::string
& str
) {
99 std::vector
<std::string
> parts
;
100 base::SplitString(str
, ':', &parts
);
102 if (parts
.size() != 2)
103 return DesktopMediaID(TYPE_NONE
, 0);
105 Type type
= TYPE_NONE
;
106 if (parts
[0] == kScreenPrefix
) {
108 } else if (parts
[0] == kWindowPrefix
) {
110 } else if (parts
[0] == kAuraWindowPrefix
) {
111 type
= TYPE_AURA_WINDOW
;
113 return DesktopMediaID(TYPE_NONE
, 0);
117 if (!base::StringToInt64(parts
[1], &id
))
118 return DesktopMediaID(TYPE_NONE
, 0);
120 return DesktopMediaID(type
, id
);
123 std::string
DesktopMediaID::ToString() {
128 return std::string();
130 prefix
= kScreenPrefix
;
133 prefix
= kWindowPrefix
;
135 case TYPE_AURA_WINDOW
:
136 prefix
= kAuraWindowPrefix
;
139 DCHECK(!prefix
.empty());
142 prefix
.append(base::Int64ToString(id
));
147 } // namespace content