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() {
19 void ExtensionIconSet::Add(int size
, const std::string
& path
) {
20 DCHECK(!path
.empty() && path
[0] != '/');
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
) {
39 return result
== map_
.rend() ? base::EmptyString() : result
->second
;
41 DCHECK(match_type
== MATCH_BIGGER
);
42 IconMap::const_iterator result
= map_
.end();
43 for (IconMap::const_iterator iter
= map_
.begin(); iter
!= map_
.end();
45 if (iter
->first
>= size
) {
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 {
62 DCHECK_NE(path
[0], '/') <<
63 "ExtensionIconSet stores icon paths without leading slash.";
65 for (IconMap::const_iterator iter
= map_
.begin(); iter
!= map_
.end();
67 if (iter
->second
== path
)
74 void ExtensionIconSet::GetPaths(std::set
<base::FilePath
>* paths
) const {
76 for (auto iter
: map())
77 paths
->insert(base::FilePath::FromUTF8Unsafe(iter
.second
));