Make sure webrtc::VideoSourceInterface is released on the main render thread.
[chromium-blink-merge.git] / content / public / browser / desktop_media_id.cc
bloba2adfdab5e604ccaa5e89ca39e29b378d7a5e7cc
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"
7 #include <vector>
9 #include "base/memory/singleton.h"
10 #include "base/strings/string_split.h"
12 #if defined(USE_AURA)
13 #include "ui/aura/window.h"
14 #include "ui/aura/window_observer.h"
15 #endif // defined(USE_AURA)
17 namespace {
19 #if defined(USE_AURA)
21 class AuraWindowRegistry : public aura::WindowObserver {
22 public:
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()) {
31 return it->second;
34 // If the windows doesn't have an Id yet assign it.
35 int id = next_id_++;
36 window_to_id_map_[window] = id;
37 id_to_window_map_[id] = window;
38 window->AddObserver(this);
39 return id;
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;
47 private:
48 friend struct DefaultSingletonTraits<AuraWindowRegistry>;
50 AuraWindowRegistry()
51 : next_id_(0) {
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);
63 int next_id_;
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)
72 } // namespace
74 namespace content {
76 const char kScreenPrefix[] = "screen";
77 const char kWindowPrefix[] = "window";
78 const char kAuraWindowPrefix[] = "aura_window";
80 #if defined(USE_AURA)
82 // static
83 DesktopMediaID DesktopMediaID::RegisterAuraWindow(aura::Window* window) {
84 return DesktopMediaID(
85 TYPE_AURA_WINDOW,
86 AuraWindowRegistry::GetInstance()->RegisterWindow(window));
89 // static
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)
97 // static
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) {
107 type = TYPE_SCREEN;
108 } else if (parts[0] == kWindowPrefix) {
109 type = TYPE_WINDOW;
110 } else if (parts[0] == kAuraWindowPrefix) {
111 type = TYPE_AURA_WINDOW;
112 } else {
113 return DesktopMediaID(TYPE_NONE, 0);
116 int64 id;
117 if (!base::StringToInt64(parts[1], &id))
118 return DesktopMediaID(TYPE_NONE, 0);
120 return DesktopMediaID(type, id);
123 std::string DesktopMediaID::ToString() {
124 std::string prefix;
125 switch (type) {
126 case TYPE_NONE:
127 NOTREACHED();
128 return std::string();
129 case TYPE_SCREEN:
130 prefix = kScreenPrefix;
131 break;
132 case TYPE_WINDOW:
133 prefix = kWindowPrefix;
134 break;
135 case TYPE_AURA_WINDOW:
136 prefix = kAuraWindowPrefix;
137 break;
139 DCHECK(!prefix.empty());
141 prefix.append(":");
142 prefix.append(base::Int64ToString(id));
144 return prefix;
147 } // namespace content