Popular sites on the NTP: Try to keep the ordering constant
[chromium-blink-merge.git] / content / public / browser / desktop_media_id.cc
blob74fbb3ab89f9310a227472c28eae347f2762b420
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 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 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 DesktopMediaID::DesktopMediaID() = default;
98 // static
99 DesktopMediaID DesktopMediaID::Parse(const std::string& str) {
100 std::vector<std::string> parts = base::SplitString(
101 str, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
103 #if defined(USE_AURA)
104 if (parts.size() != 3)
105 return DesktopMediaID();
106 #else
107 if (parts.size() != 2)
108 return DesktopMediaID();
109 #endif
111 Type type = TYPE_NONE;
112 if (parts[0] == kScreenPrefix) {
113 type = TYPE_SCREEN;
114 } else if (parts[0] == kWindowPrefix) {
115 type = TYPE_WINDOW;
116 } else {
117 return DesktopMediaID();
120 int64 id;
121 if (!base::StringToInt64(parts[1], &id))
122 return DesktopMediaID();
124 DesktopMediaID media_id(type, id);
126 #if defined(USE_AURA)
127 int64 aura_id;
128 if (!base::StringToInt64(parts[2], &aura_id))
129 return DesktopMediaID();
130 media_id.aura_id = aura_id;
131 #endif // defined(USE_AURA)
133 return media_id;
136 std::string DesktopMediaID::ToString() {
137 std::string prefix;
138 switch (type) {
139 case TYPE_NONE:
140 NOTREACHED();
141 return std::string();
142 case TYPE_SCREEN:
143 prefix = kScreenPrefix;
144 break;
145 case TYPE_WINDOW:
146 prefix = kWindowPrefix;
147 break;
149 DCHECK(!prefix.empty());
151 prefix.append(":");
152 prefix.append(base::Int64ToString(id));
154 #if defined(USE_AURA)
155 prefix.append(":");
156 prefix.append(base::Int64ToString(aura_id));
157 #endif // defined(USE_AURA)
159 return prefix;
162 } // namespace content