OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "content/public/browser/desktop_media_id.h" | |
6 | |
7 #include <vector> | |
8 | |
9 #include "base/memory/singleton.h" | |
10 #include "base/strings/string_split.h" | |
11 #include "ui/aura/window.h" | |
12 #include "ui/aura/window_observer.h" | |
13 | |
14 namespace { | |
15 | |
16 class AuraWindowRegistry : public aura::WindowObserver { | |
17 public: | |
18 static AuraWindowRegistry* GetInstance() { | |
19 return Singleton<AuraWindowRegistry>::get(); | |
20 } | |
21 | |
22 int RegisterWindow(aura::Window* window) { | |
23 // First check if an Id is already assigned to the |window|. | |
24 std::map<aura::Window*, int>::iterator it = window_to_id_map_.find(window); | |
25 if (it != window_to_id_map_.end()) { | |
26 return it->second; | |
27 } | |
28 | |
29 // If the windows doesn't have an Id yet assign it. | |
30 int id = next_id_++; | |
31 window_to_id_map_[window] = id; | |
32 id_to_window_map_[id] = window; | |
33 window->AddObserver(this); | |
34 return id; | |
35 } | |
36 | |
37 aura::Window* GetWindowById(int id) { | |
38 std::map<int, aura::Window*>::iterator it = id_to_window_map_.find(id); | |
39 return (it != id_to_window_map_.end()) ? it->second : NULL; | |
40 } | |
41 | |
42 private: | |
43 friend struct DefaultSingletonTraits<AuraWindowRegistry>; | |
44 | |
45 AuraWindowRegistry() | |
46 : next_id_(0) { | |
47 } | |
48 ~AuraWindowRegistry() {} | |
49 | |
50 // WindowObserver overrides. | |
51 virtual void OnWindowDestroying(aura::Window* window) OVERRIDE { | |
52 std::map<aura::Window*, int>::iterator it = window_to_id_map_.find(window); | |
53 DCHECK(it != window_to_id_map_.end()); | |
54 id_to_window_map_.erase(it->second); | |
55 window_to_id_map_.erase(it); | |
56 } | |
57 | |
58 int next_id_; | |
59 std::map<aura::Window*, int> window_to_id_map_; | |
60 std::map<int, aura::Window*> id_to_window_map_; | |
cpu_(ooo_6.6-7.5)
2013/12/07 02:14:37
I am not sure this is the best practice here, we a
Sergey Ulanov
2013/12/07 02:29:34
I don't think there is a place where we store list
| |
61 | |
62 DISALLOW_COPY_AND_ASSIGN(AuraWindowRegistry); | |
63 }; | |
64 | |
65 } // namespace | |
66 | |
67 namespace content { | |
68 | |
69 const char kScreenPrefix[] = "screen"; | |
70 const char kWindowPrefix[] = "window"; | |
71 const char kAuraWindowPrefix[] = "aura_window"; | |
72 | |
73 // static | |
74 DesktopMediaID DesktopMediaID::RegisterAuraWindow(aura::Window* window) { | |
75 return DesktopMediaID( | |
76 TYPE_AURA_WINDOW, | |
77 AuraWindowRegistry::GetInstance()->RegisterWindow(window)); | |
78 } | |
79 | |
80 // static | |
81 aura::Window* DesktopMediaID::GetAuraWindowById(const DesktopMediaID& id) { | |
82 DCHECK_EQ(id.type, TYPE_AURA_WINDOW); | |
83 return AuraWindowRegistry::GetInstance()->GetWindowById(id.id); | |
84 } | |
85 | |
86 // static | |
87 DesktopMediaID DesktopMediaID::Parse(const std::string& str) { | |
88 std::vector<std::string> parts; | |
89 base::SplitString(str, ':', &parts); | |
90 | |
91 if (parts.size() != 2) | |
92 return DesktopMediaID(TYPE_NONE, 0); | |
93 | |
94 Type type = TYPE_NONE; | |
95 if (parts[0] == kScreenPrefix) { | |
96 type = TYPE_SCREEN; | |
97 } else if (parts[0] == kWindowPrefix) { | |
98 type = TYPE_WINDOW; | |
99 } else if (parts[0] == kAuraWindowPrefix) { | |
100 type = TYPE_AURA_WINDOW; | |
101 } else { | |
102 return DesktopMediaID(TYPE_NONE, 0); | |
103 } | |
104 | |
105 int64 id; | |
106 if (!base::StringToInt64(parts[1], &id)) | |
107 return DesktopMediaID(TYPE_NONE, 0); | |
108 | |
109 return DesktopMediaID(type, id); | |
110 } | |
111 | |
112 std::string DesktopMediaID::ToString() { | |
113 std::string prefix; | |
114 switch (type) { | |
115 case TYPE_NONE: | |
116 NOTREACHED(); | |
117 return std::string(); | |
118 case TYPE_SCREEN: | |
119 prefix = kScreenPrefix; | |
120 break; | |
121 case TYPE_WINDOW: | |
122 prefix = kWindowPrefix; | |
123 break; | |
124 case TYPE_AURA_WINDOW: | |
125 prefix = kAuraWindowPrefix; | |
126 break; | |
127 } | |
128 DCHECK(!prefix.empty()); | |
129 | |
130 prefix.append(":"); | |
131 prefix.append(base::Int64ToString(id)); | |
132 | |
133 return prefix; | |
134 } | |
135 | |
136 } // namespace content | |
OLD | NEW |