OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/common/extension_icon_set.h" | 5 #include "extensions/common/extension_icon_set.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/strings/string_util.h" |
8 | 9 |
9 ExtensionIconSet::ExtensionIconSet() {} | 10 ExtensionIconSet::ExtensionIconSet() {} |
10 | 11 |
11 ExtensionIconSet::~ExtensionIconSet() {} | 12 ExtensionIconSet::~ExtensionIconSet() {} |
12 | 13 |
13 void ExtensionIconSet::Clear() { | 14 void ExtensionIconSet::Clear() { |
14 map_.clear(); | 15 map_.clear(); |
15 } | 16 } |
16 | 17 |
17 void ExtensionIconSet::Add(int size, const std::string& path) { | 18 void ExtensionIconSet::Add(int size, const std::string& path) { |
18 DCHECK(!path.empty() && path[0] != '/'); | 19 DCHECK(!path.empty() && path[0] != '/'); |
19 map_[size] = path; | 20 map_[size] = path; |
20 } | 21 } |
21 | 22 |
22 std::string ExtensionIconSet::Get(int size, MatchType match_type) const { | 23 const std::string& ExtensionIconSet::Get(int size, MatchType match_type) const { |
23 // The searches for MATCH_BIGGER and MATCH_SMALLER below rely on the fact that | 24 // The searches for MATCH_BIGGER and MATCH_SMALLER below rely on the fact that |
24 // std::map is sorted. This is per the spec, so it should be safe to rely on. | 25 // std::map is sorted. This is per the spec, so it should be safe to rely on. |
25 if (match_type == MATCH_EXACTLY) { | 26 if (match_type == MATCH_EXACTLY) { |
26 IconMap::const_iterator result = map_.find(size); | 27 IconMap::const_iterator result = map_.find(size); |
27 return result == map_.end() ? std::string() : result->second; | 28 return result == map_.end() ? base::EmptyString() : result->second; |
28 } else if (match_type == MATCH_SMALLER) { | 29 } else if (match_type == MATCH_SMALLER) { |
29 IconMap::const_reverse_iterator result = map_.rend(); | 30 IconMap::const_reverse_iterator result = map_.rend(); |
30 for (IconMap::const_reverse_iterator iter = map_.rbegin(); | 31 for (IconMap::const_reverse_iterator iter = map_.rbegin(); |
31 iter != map_.rend(); ++iter) { | 32 iter != map_.rend(); ++iter) { |
32 if (iter->first <= size) { | 33 if (iter->first <= size) { |
33 result = iter; | 34 result = iter; |
34 break; | 35 break; |
35 } | 36 } |
36 } | 37 } |
37 return result == map_.rend() ? std::string() : result->second; | 38 return result == map_.rend() ? base::EmptyString() : result->second; |
38 } else { | 39 } else { |
39 DCHECK(match_type == MATCH_BIGGER); | 40 DCHECK(match_type == MATCH_BIGGER); |
40 IconMap::const_iterator result = map_.end(); | 41 IconMap::const_iterator result = map_.end(); |
41 for (IconMap::const_iterator iter = map_.begin(); iter != map_.end(); | 42 for (IconMap::const_iterator iter = map_.begin(); iter != map_.end(); |
42 ++iter) { | 43 ++iter) { |
43 if (iter->first >= size) { | 44 if (iter->first >= size) { |
44 result = iter; | 45 result = iter; |
45 break; | 46 break; |
46 } | 47 } |
47 } | 48 } |
48 return result == map_.end() ? std::string() : result->second; | 49 return result == map_.end() ? base::EmptyString() : result->second; |
49 } | 50 } |
50 } | 51 } |
51 | 52 |
52 bool ExtensionIconSet::ContainsPath(const std::string& path) const { | 53 bool ExtensionIconSet::ContainsPath(const std::string& path) const { |
53 return GetIconSizeFromPath(path) != 0; | 54 return GetIconSizeFromPath(path) != 0; |
54 } | 55 } |
55 | 56 |
56 int ExtensionIconSet::GetIconSizeFromPath(const std::string& path) const { | 57 int ExtensionIconSet::GetIconSizeFromPath(const std::string& path) const { |
57 if (path.empty()) | 58 if (path.empty()) |
58 return 0; | 59 return 0; |
59 | 60 |
60 DCHECK_NE(path[0], '/') << | 61 DCHECK_NE(path[0], '/') << |
61 "ExtensionIconSet stores icon paths without leading slash."; | 62 "ExtensionIconSet stores icon paths without leading slash."; |
62 | 63 |
63 for (IconMap::const_iterator iter = map_.begin(); iter != map_.end(); | 64 for (IconMap::const_iterator iter = map_.begin(); iter != map_.end(); |
64 ++iter) { | 65 ++iter) { |
65 if (iter->second == path) | 66 if (iter->second == path) |
66 return iter->first; | 67 return iter->first; |
67 } | 68 } |
68 | 69 |
69 return 0; | 70 return 0; |
70 } | 71 } |
OLD | NEW |