blob: 74fbb3ab89f9310a227472c28eae347f2762b420 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/public/browser/desktop_media_id.h"
#include <vector>
#include "base/id_map.h"
#include "base/memory/singleton.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#if defined(USE_AURA)
#include "ui/aura/window.h"
#include "ui/aura/window_observer.h"
#endif // defined(USE_AURA)
namespace {
#if defined(USE_AURA)
class AuraWindowRegistry : public aura::WindowObserver {
public:
static AuraWindowRegistry* GetInstance() {
return Singleton<AuraWindowRegistry>::get();
}
int RegisterWindow(aura::Window* window) {
IDMap<aura::Window>::const_iterator it(®istered_windows_);
for (; !it.IsAtEnd(); it.Advance()) {
if (it.GetCurrentValue() == window)
return it.GetCurrentKey();
}
window->AddObserver(this);
return registered_windows_.Add(window);
}
aura::Window* GetWindowById(int id) {
return registered_windows_.Lookup(id);
}
private:
friend struct DefaultSingletonTraits<AuraWindowRegistry>;
AuraWindowRegistry() {}
~AuraWindowRegistry() override {}
// WindowObserver overrides.
void OnWindowDestroying(aura::Window* window) override {
IDMap<aura::Window>::iterator it(®istered_windows_);
for (; !it.IsAtEnd(); it.Advance()) {
if (it.GetCurrentValue() == window) {
registered_windows_.Remove(it.GetCurrentKey());
return;
}
}
NOTREACHED();
}
IDMap<aura::Window> registered_windows_;
DISALLOW_COPY_AND_ASSIGN(AuraWindowRegistry);
};
#endif // defined(USE_AURA)
} // namespace
namespace content {
const char kScreenPrefix[] = "screen";
const char kWindowPrefix[] = "window";
#if defined(USE_AURA)
// static
DesktopMediaID DesktopMediaID::RegisterAuraWindow(DesktopMediaID::Type type,
aura::Window* window) {
DCHECK(type == TYPE_SCREEN || type == TYPE_WINDOW);
DCHECK(window);
DesktopMediaID media_id(type, kNullId);
media_id.aura_id = AuraWindowRegistry::GetInstance()->RegisterWindow(window);
return media_id;
}
// static
aura::Window* DesktopMediaID::GetAuraWindowById(const DesktopMediaID& id) {
return AuraWindowRegistry::GetInstance()->GetWindowById(id.aura_id);
}
#endif // defined(USE_AURA)
DesktopMediaID::DesktopMediaID() = default;
// static
DesktopMediaID DesktopMediaID::Parse(const std::string& str) {
std::vector<std::string> parts = base::SplitString(
str, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
#if defined(USE_AURA)
if (parts.size() != 3)
return DesktopMediaID();
#else
if (parts.size() != 2)
return DesktopMediaID();
#endif
Type type = TYPE_NONE;
if (parts[0] == kScreenPrefix) {
type = TYPE_SCREEN;
} else if (parts[0] == kWindowPrefix) {
type = TYPE_WINDOW;
} else {
return DesktopMediaID();
}
int64 id;
if (!base::StringToInt64(parts[1], &id))
return DesktopMediaID();
DesktopMediaID media_id(type, id);
#if defined(USE_AURA)
int64 aura_id;
if (!base::StringToInt64(parts[2], &aura_id))
return DesktopMediaID();
media_id.aura_id = aura_id;
#endif // defined(USE_AURA)
return media_id;
}
std::string DesktopMediaID::ToString() {
std::string prefix;
switch (type) {
case TYPE_NONE:
NOTREACHED();
return std::string();
case TYPE_SCREEN:
prefix = kScreenPrefix;
break;
case TYPE_WINDOW:
prefix = kWindowPrefix;
break;
}
DCHECK(!prefix.empty());
prefix.append(":");
prefix.append(base::Int64ToString(id));
#if defined(USE_AURA)
prefix.append(":");
prefix.append(base::Int64ToString(aura_id));
#endif // defined(USE_AURA)
return prefix;
}
} // namespace content
|