Content settings: remove some plugin-related code/resources when... there are no...
[chromium-blink-merge.git] / content / public / browser / desktop_media_id.cc
blob1ec49b7b4de8594b3788e8b3a250ad3bf9164f6c
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/id_map.h"
10 #include "base/memory/singleton.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_split.h"
13 #include "base/strings/string_util.h"
15 #if defined(USE_AURA)
16 #include "ui/aura/window.h"
17 #include "ui/aura/window_observer.h"
18 #endif // defined(USE_AURA)
20 namespace {
22 #if defined(USE_AURA)
24 class AuraWindowRegistry : public aura::WindowObserver {
25 public:
26 static AuraWindowRegistry* GetInstance() {
27 return base::Singleton<AuraWindowRegistry>::get();
30 int RegisterWindow(aura::Window* window) {
31 IDMap<aura::Window>::const_iterator it(&registered_windows_);
32 for (; !it.IsAtEnd(); it.Advance()) {
33 if (it.GetCurrentValue() == window)
34 return it.GetCurrentKey();
37 window->AddObserver(this);
38 return registered_windows_.Add(window);
41 aura::Window* GetWindowById(int id) {
42 return registered_windows_.Lookup(id);
45 private:
46 friend struct base::DefaultSingletonTraits<AuraWindowRegistry>;
48 AuraWindowRegistry() {}
49 ~AuraWindowRegistry() override {}
51 // WindowObserver overrides.
52 void OnWindowDestroying(aura::Window* window) override {
53 IDMap<aura::Window>::iterator it(&registered_windows_);
54 for (; !it.IsAtEnd(); it.Advance()) {
55 if (it.GetCurrentValue() == window) {
56 registered_windows_.Remove(it.GetCurrentKey());
57 return;
60 NOTREACHED();
63 IDMap<aura::Window> registered_windows_;
65 DISALLOW_COPY_AND_ASSIGN(AuraWindowRegistry);
68 #endif // defined(USE_AURA)
70 } // namespace
72 namespace content {
74 const char kScreenPrefix[] = "screen";
75 const char kWindowPrefix[] = "window";
77 #if defined(USE_AURA)
79 // static
80 DesktopMediaID DesktopMediaID::RegisterAuraWindow(DesktopMediaID::Type type,
81 aura::Window* window) {
82 DCHECK(type == TYPE_SCREEN || type == TYPE_WINDOW);
83 DCHECK(window);
84 DesktopMediaID media_id(type, kNullId);
85 media_id.aura_id = AuraWindowRegistry::GetInstance()->RegisterWindow(window);
86 return media_id;
89 // static
90 aura::Window* DesktopMediaID::GetAuraWindowById(const DesktopMediaID& id) {
91 return AuraWindowRegistry::GetInstance()->GetWindowById(id.aura_id);
94 #endif // defined(USE_AURA)
96 // static
97 DesktopMediaID DesktopMediaID::Parse(const std::string& str) {
98 std::vector<std::string> parts = base::SplitString(
99 str, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
101 #if defined(USE_AURA)
102 if (parts.size() != 3)
103 return DesktopMediaID();
104 #else
105 if (parts.size() != 2)
106 return DesktopMediaID();
107 #endif
109 Type type = TYPE_NONE;
110 if (parts[0] == kScreenPrefix) {
111 type = TYPE_SCREEN;
112 } else if (parts[0] == kWindowPrefix) {
113 type = TYPE_WINDOW;
114 } else {
115 return DesktopMediaID();
118 int64 id;
119 if (!base::StringToInt64(parts[1], &id))
120 return DesktopMediaID();
122 DesktopMediaID media_id(type, id);
124 #if defined(USE_AURA)
125 int64 aura_id;
126 if (!base::StringToInt64(parts[2], &aura_id))
127 return DesktopMediaID();
128 media_id.aura_id = aura_id;
129 #endif // defined(USE_AURA)
131 return media_id;
134 std::string DesktopMediaID::ToString() {
135 std::string prefix;
136 switch (type) {
137 case TYPE_NONE:
138 NOTREACHED();
139 return std::string();
140 case TYPE_SCREEN:
141 prefix = kScreenPrefix;
142 break;
143 case TYPE_WINDOW:
144 prefix = kWindowPrefix;
145 break;
147 DCHECK(!prefix.empty());
149 prefix.append(":");
150 prefix.append(base::Int64ToString(id));
152 #if defined(USE_AURA)
153 prefix.append(":");
154 prefix.append(base::Int64ToString(aura_id));
155 #endif // defined(USE_AURA)
157 return prefix;
160 } // namespace content