OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/public/browser/desktop_media_id.h" | 5 #include "content/public/browser/desktop_media_id.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/id_map.h" | 9 #include "base/id_map.h" |
10 #include "base/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 namespace content { | 72 namespace content { |
73 | 73 |
74 const char kScreenPrefix[] = "screen"; | 74 const char kScreenPrefix[] = "screen"; |
75 const char kWindowPrefix[] = "window"; | 75 const char kWindowPrefix[] = "window"; |
76 | 76 |
77 #if defined(USE_AURA) | 77 #if defined(USE_AURA) |
78 | 78 |
79 // static | 79 // static |
80 DesktopMediaID DesktopMediaID::RegisterAuraWindow(DesktopMediaID::Type type, | 80 DesktopMediaID DesktopMediaID::RegisterAuraWindow(DesktopMediaID::Type type, |
81 aura::Window* window) { | 81 aura::Window* window) { |
82 DCHECK(type == TYPE_SCREEN || type == TYPE_WINDOW); | 82 DCHECK(type == TYPE_SCREEN || type == TYPE_WINDOW || type == TYPE_TAB); |
83 DCHECK(window); | 83 DCHECK(window); |
84 DesktopMediaID media_id(type, kNullId); | 84 DesktopMediaID media_id(type, kNullId); |
85 media_id.aura_id = AuraWindowRegistry::GetInstance()->RegisterWindow(window); | 85 media_id.aura_id = AuraWindowRegistry::GetInstance()->RegisterWindow(window); |
86 return media_id; | 86 return media_id; |
87 } | 87 } |
88 | 88 |
89 // static | 89 // static |
90 aura::Window* DesktopMediaID::GetAuraWindowById(const DesktopMediaID& id) { | 90 aura::Window* DesktopMediaID::GetAuraWindowById(const DesktopMediaID& id) { |
91 return AuraWindowRegistry::GetInstance()->GetWindowById(id.aura_id); | 91 return AuraWindowRegistry::GetInstance()->GetWindowById(id.aura_id); |
92 } | 92 } |
93 | 93 |
94 #endif // defined(USE_AURA) | 94 #endif // defined(USE_AURA) |
95 | 95 |
| 96 bool DesktopMediaID::operator<(const DesktopMediaID& other) const { |
| 97 #if defined(USE_AURA) |
| 98 return std::tie(type, id, aura_id, tab_id) < |
| 99 std::tie(other.type, other.id, other.aura_id, other.tab_id); |
| 100 #else |
| 101 return std::tie(type, id, tab_id) < std::tie(other.type, other.id, tab_id); |
| 102 #endif |
| 103 } |
| 104 |
| 105 bool DesktopMediaID::operator==(const DesktopMediaID& other) const { |
| 106 #if defined(USE_AURA) |
| 107 return type == other.type && id == other.id && aura_id == other.aura_id && |
| 108 tab_id == other.tab_id; |
| 109 #else |
| 110 return type == other.type && id == other.id && tab_id == other.tab_id; |
| 111 #endif |
| 112 } |
| 113 |
96 // static | 114 // static |
| 115 // Input string should in format: |
| 116 // for tab: web-contents-media-stream://"render_process_id":"render_process_id" |
| 117 // for no tab and aura: screen:"window_id" or window:"window_id" |
| 118 // for no tab and no aura: screen:"window_id:aura_id" or |
| 119 // window:"window_id:aura_id". |
97 DesktopMediaID DesktopMediaID::Parse(const std::string& str) { | 120 DesktopMediaID DesktopMediaID::Parse(const std::string& str) { |
98 std::vector<std::string> parts = base::SplitString( | 121 std::vector<std::string> parts = base::SplitString( |
99 str, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 122 str, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
100 | 123 |
| 124 if (parts.size() < 2) |
| 125 return DesktopMediaID(); |
| 126 |
| 127 Type type = TYPE_NONE; |
| 128 if (parts[0] == kScreenPrefix) { |
| 129 type = TYPE_SCREEN; |
| 130 } else if (parts[0] == kWindowPrefix) { |
| 131 type = TYPE_WINDOW; |
| 132 } else if (parts[0] == kTabPrefix) { |
| 133 type = TYPE_TAB; |
| 134 } else { |
| 135 return DesktopMediaID(); |
| 136 } |
| 137 |
| 138 // Tab type. |
| 139 if (type == TYPE_TAB) { |
| 140 WebContentsMediaCaptureId temp_id = WebContentsMediaCaptureId::Parse(str); |
| 141 return DesktopMediaID(type, 0, temp_id); |
| 142 } |
| 143 |
| 144 // Screen and window types. |
101 #if defined(USE_AURA) | 145 #if defined(USE_AURA) |
102 if (parts.size() != 3) | 146 if (parts.size() != 3) |
103 return DesktopMediaID(); | 147 return DesktopMediaID(); |
104 #else | 148 #else |
105 if (parts.size() != 2) | 149 if (parts.size() != 2) |
106 return DesktopMediaID(); | 150 return DesktopMediaID(); |
107 #endif | 151 #endif // defined(USE_AURA) |
108 | 152 |
109 Type type = TYPE_NONE; | 153 int64 tempid; |
110 if (parts[0] == kScreenPrefix) { | 154 if (!base::StringToInt64(parts[1], &tempid)) |
111 type = TYPE_SCREEN; | |
112 } else if (parts[0] == kWindowPrefix) { | |
113 type = TYPE_WINDOW; | |
114 } else { | |
115 return DesktopMediaID(); | |
116 } | |
117 | |
118 int64 id; | |
119 if (!base::StringToInt64(parts[1], &id)) | |
120 return DesktopMediaID(); | 155 return DesktopMediaID(); |
121 | 156 |
122 DesktopMediaID media_id(type, id); | 157 DesktopMediaID media_id(type, tempid); |
123 | 158 |
124 #if defined(USE_AURA) | 159 #if defined(USE_AURA) |
125 int64 aura_id; | 160 int64 aura_id; |
126 if (!base::StringToInt64(parts[2], &aura_id)) | 161 if (!base::StringToInt64(parts[2], &aura_id)) |
127 return DesktopMediaID(); | 162 return DesktopMediaID(); |
| 163 |
128 media_id.aura_id = aura_id; | 164 media_id.aura_id = aura_id; |
129 #endif // defined(USE_AURA) | 165 #endif // defined(USE_AURA) |
130 | 166 |
131 return media_id; | 167 return media_id; |
132 } | 168 } |
133 | 169 |
134 std::string DesktopMediaID::ToString() { | 170 std::string DesktopMediaID::ToString() const { |
135 std::string prefix; | 171 std::string prefix; |
136 switch (type) { | 172 switch (type) { |
137 case TYPE_NONE: | 173 case TYPE_NONE: |
138 NOTREACHED(); | 174 NOTREACHED(); |
139 return std::string(); | 175 return std::string(); |
140 case TYPE_SCREEN: | 176 case TYPE_SCREEN: |
141 prefix = kScreenPrefix; | 177 prefix = kScreenPrefix; |
142 break; | 178 break; |
143 case TYPE_WINDOW: | 179 case TYPE_WINDOW: |
144 prefix = kWindowPrefix; | 180 prefix = kWindowPrefix; |
145 break; | 181 break; |
| 182 case TYPE_TAB: |
| 183 return tab_id.ToString(); |
| 184 break; |
146 } | 185 } |
147 DCHECK(!prefix.empty()); | 186 DCHECK(!prefix.empty()); |
148 | 187 |
| 188 // Screen and Window types. |
149 prefix.append(":"); | 189 prefix.append(":"); |
150 prefix.append(base::Int64ToString(id)); | 190 prefix.append(base::Int64ToString(id)); |
151 | 191 |
152 #if defined(USE_AURA) | 192 #if defined(USE_AURA) |
153 prefix.append(":"); | 193 prefix.append(":"); |
154 prefix.append(base::Int64ToString(aura_id)); | 194 prefix.append(base::Int64ToString(aura_id)); |
155 #endif // defined(USE_AURA) | 195 #endif // defined(USE_AURA) |
156 | 196 |
157 return prefix; | 197 return prefix; |
158 } | 198 } |
159 | 199 |
160 } // namespace content | 200 } // namespace content |
OLD | NEW |