Popular sites on the NTP: Favicon improvements
[chromium-blink-merge.git] / base / posix / global_descriptors.cc
blob6c187838ad69d63051a1155d64c26623de8a2db0
1 // Copyright (c) 2012 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 "base/posix/global_descriptors.h"
7 #include <vector>
8 #include <utility>
10 #include "base/logging.h"
12 namespace base {
14 GlobalDescriptors::Descriptor::Descriptor(Key key, int fd)
15 : key(key), fd(fd), region(base::MemoryMappedFile::Region::kWholeFile) {
18 GlobalDescriptors::Descriptor::Descriptor(Key key,
19 int fd,
20 base::MemoryMappedFile::Region region)
21 : key(key), fd(fd), region(region) {
24 // static
25 GlobalDescriptors* GlobalDescriptors::GetInstance() {
26 typedef Singleton<base::GlobalDescriptors,
27 LeakySingletonTraits<base::GlobalDescriptors> >
28 GlobalDescriptorsSingleton;
29 return GlobalDescriptorsSingleton::get();
32 int GlobalDescriptors::Get(Key key) const {
33 const int ret = MaybeGet(key);
35 if (ret == -1)
36 DLOG(FATAL) << "Unknown global descriptor: " << key;
37 return ret;
40 int GlobalDescriptors::MaybeGet(Key key) const {
41 for (Mapping::const_iterator
42 i = descriptors_.begin(); i != descriptors_.end(); ++i) {
43 if (i->key == key)
44 return i->fd;
47 return -1;
50 void GlobalDescriptors::Set(Key key, int fd) {
51 Set(key, fd, base::MemoryMappedFile::Region::kWholeFile);
54 void GlobalDescriptors::Set(Key key,
55 int fd,
56 base::MemoryMappedFile::Region region) {
57 for (auto& i : descriptors_) {
58 if (i.key == key) {
59 i.fd = fd;
60 i.region = region;
61 return;
65 descriptors_.push_back(Descriptor(key, fd, region));
68 base::MemoryMappedFile::Region GlobalDescriptors::GetRegion(Key key) const {
69 for (const auto& i : descriptors_) {
70 if (i.key == key)
71 return i.region;
73 DLOG(FATAL) << "Unknown global descriptor: " << key;
74 return base::MemoryMappedFile::Region::kWholeFile;
77 void GlobalDescriptors::Reset(const Mapping& mapping) {
78 descriptors_ = mapping;
81 GlobalDescriptors::GlobalDescriptors() {}
83 GlobalDescriptors::~GlobalDescriptors() {}
85 } // namespace base