Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / extensions / common / extension_icon_set.cc
blob65ccff055e486fbff7191e9d467d0772e97f4368
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/logging.h"
8 #include "base/strings/string_util.h"
10 ExtensionIconSet::ExtensionIconSet() {}
12 ExtensionIconSet::~ExtensionIconSet() {}
14 void ExtensionIconSet::Clear() {
15 map_.clear();
18 void ExtensionIconSet::Add(int size, const std::string& path) {
19 DCHECK(!path.empty() && path[0] != '/');
20 map_[size] = path;
23 const std::string& ExtensionIconSet::Get(int size, MatchType match_type) const {
24 // The searches for MATCH_BIGGER and MATCH_SMALLER below rely on the fact that
25 // std::map is sorted. This is per the spec, so it should be safe to rely on.
26 if (match_type == MATCH_EXACTLY) {
27 IconMap::const_iterator result = map_.find(size);
28 return result == map_.end() ? base::EmptyString() : result->second;
29 } else if (match_type == MATCH_SMALLER) {
30 IconMap::const_reverse_iterator result = map_.rend();
31 for (IconMap::const_reverse_iterator iter = map_.rbegin();
32 iter != map_.rend(); ++iter) {
33 if (iter->first <= size) {
34 result = iter;
35 break;
38 return result == map_.rend() ? base::EmptyString() : result->second;
39 } else {
40 DCHECK(match_type == MATCH_BIGGER);
41 IconMap::const_iterator result = map_.end();
42 for (IconMap::const_iterator iter = map_.begin(); iter != map_.end();
43 ++iter) {
44 if (iter->first >= size) {
45 result = iter;
46 break;
49 return result == map_.end() ? base::EmptyString() : result->second;
53 bool ExtensionIconSet::ContainsPath(const std::string& path) const {
54 return GetIconSizeFromPath(path) != 0;
57 int ExtensionIconSet::GetIconSizeFromPath(const std::string& path) const {
58 if (path.empty())
59 return 0;
61 DCHECK_NE(path[0], '/') <<
62 "ExtensionIconSet stores icon paths without leading slash.";
64 for (IconMap::const_iterator iter = map_.begin(); iter != map_.end();
65 ++iter) {
66 if (iter->second == path)
67 return iter->first;
70 return 0;