Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / extensions / common / extension_icon_set.cc
blob221f32f5d5b7e8d0b99e13809d68f2617e5bab13
1 // Copyright 2014 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 "extensions/common/extension_icon_set.h"
7 #include "base/files/file_path.h"
8 #include "base/logging.h"
9 #include "base/strings/string_util.h"
11 ExtensionIconSet::ExtensionIconSet() {}
13 ExtensionIconSet::~ExtensionIconSet() {}
15 void ExtensionIconSet::Clear() {
16 map_.clear();
19 void ExtensionIconSet::Add(int size, const std::string& path) {
20 DCHECK(!path.empty() && path[0] != '/');
21 map_[size] = path;
24 const std::string& ExtensionIconSet::Get(int size, MatchType match_type) const {
25 // The searches for MATCH_BIGGER and MATCH_SMALLER below rely on the fact that
26 // std::map is sorted. This is per the spec, so it should be safe to rely on.
27 if (match_type == MATCH_EXACTLY) {
28 IconMap::const_iterator result = map_.find(size);
29 return result == map_.end() ? base::EmptyString() : result->second;
30 } else if (match_type == MATCH_SMALLER) {
31 IconMap::const_reverse_iterator result = map_.rend();
32 for (IconMap::const_reverse_iterator iter = map_.rbegin();
33 iter != map_.rend(); ++iter) {
34 if (iter->first <= size) {
35 result = iter;
36 break;
39 return result == map_.rend() ? base::EmptyString() : result->second;
40 } else {
41 DCHECK(match_type == MATCH_BIGGER);
42 IconMap::const_iterator result = map_.end();
43 for (IconMap::const_iterator iter = map_.begin(); iter != map_.end();
44 ++iter) {
45 if (iter->first >= size) {
46 result = iter;
47 break;
50 return result == map_.end() ? base::EmptyString() : result->second;
54 bool ExtensionIconSet::ContainsPath(const std::string& path) const {
55 return GetIconSizeFromPath(path) != 0;
58 int ExtensionIconSet::GetIconSizeFromPath(const std::string& path) const {
59 if (path.empty())
60 return 0;
62 DCHECK_NE(path[0], '/') <<
63 "ExtensionIconSet stores icon paths without leading slash.";
65 for (IconMap::const_iterator iter = map_.begin(); iter != map_.end();
66 ++iter) {
67 if (iter->second == path)
68 return iter->first;
71 return 0;
74 void ExtensionIconSet::GetPaths(std::set<base::FilePath>* paths) const {
75 CHECK(paths);
76 for (auto iter : map())
77 paths->insert(base::FilePath::FromUTF8Unsafe(iter.second));