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 12 #if defined(USE_AURA) 13 #include "ui/aura/window.h" 14 #include "ui/aura/window_observer.h" 15 #endif // defined(USE_AURA) 16 17 namespace { 18 19 #if defined(USE_AURA) 20 21 class AuraWindowRegistry : public aura::WindowObserver { 22 public: 23 static AuraWindowRegistry* GetInstance() { 24 return Singleton<AuraWindowRegistry>::get(); 25 } 26 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; 32 } 33 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; 40 } 41 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; 45 } 46 47 private: 48 friend struct DefaultSingletonTraits<AuraWindowRegistry>; 49 50 AuraWindowRegistry() 51 : next_id_(0) { 52 } 53 virtual ~AuraWindowRegistry() {} 54 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); 61 } 62 63 int next_id_; 64 std::map<aura::Window*, int> window_to_id_map_; 65 std::map<int, aura::Window*> id_to_window_map_; 66 67 DISALLOW_COPY_AND_ASSIGN(AuraWindowRegistry); 68 }; 69 70 #endif // defined(USE_AURA) 71 72 } // namespace 73 74 namespace content { 75 76 const char kScreenPrefix[] = "screen"; 77 const char kWindowPrefix[] = "window"; 78 const char kAuraWindowPrefix[] = "aura_window"; 79 80 #if defined(USE_AURA) 81 82 // static 83 DesktopMediaID DesktopMediaID::RegisterAuraWindow(aura::Window* window) { 84 return DesktopMediaID( 85 TYPE_AURA_WINDOW, 86 AuraWindowRegistry::GetInstance()->RegisterWindow(window)); 87 } 88 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); 93 } 94 95 #endif // defined(USE_AURA) 96 97 // static 98 DesktopMediaID DesktopMediaID::Parse(const std::string& str) { 99 std::vector<std::string> parts; 100 base::SplitString(str, ':', &parts); 101 102 if (parts.size() != 2) 103 return DesktopMediaID(TYPE_NONE, 0); 104 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); 114 } 115 116 int64 id; 117 if (!base::StringToInt64(parts[1], &id)) 118 return DesktopMediaID(TYPE_NONE, 0); 119 120 return DesktopMediaID(type, id); 121 } 122 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; 138 } 139 DCHECK(!prefix.empty()); 140 141 prefix.append(":"); 142 prefix.append(base::Int64ToString(id)); 143 144 return prefix; 145 } 146 147 } // namespace content 148