OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/common/extensions/extension_icon_set.h" | 5 #include "chrome/common/extensions/extension_icon_set.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 | 8 |
9 ExtensionIconSet::ExtensionIconSet() {} | 9 namespace { |
10 | |
11 const int kExtensionIconSizes[] = { | |
Aaron Boodman
2012/08/10 23:01:57
Now that this class is more general purpose, move
tbarzic
2012/08/11 00:58:45
Done.
| |
12 ExtensionIconSet::EXTENSION_ICON_GIGANTOR, // 512 | |
13 ExtensionIconSet::EXTENSION_ICON_EXTRA_LARGE, // 256 | |
14 ExtensionIconSet::EXTENSION_ICON_LARGE, // 128 | |
15 ExtensionIconSet::EXTENSION_ICON_MEDIUM, // 48 | |
16 ExtensionIconSet::EXTENSION_ICON_SMALL, // 32 | |
17 ExtensionIconSet::EXTENSION_ICON_SMALLISH, // 24 | |
18 ExtensionIconSet::EXTENSION_ICON_BITTY // 16 | |
19 }; | |
20 | |
21 bool IsInArray(const int* const array, size_t array_size, int value) { | |
22 for (size_t i = 0; i < array_size; i++) { | |
23 if (array[i] == value) | |
24 return true; | |
25 } | |
26 return false; | |
27 } | |
28 | |
29 } // namespace | |
30 | |
31 ExtensionIconSet::ExtensionIconSet(IconSetType type) | |
32 : allowed_sizes_(NULL), | |
33 num_allowed_sizes_(0){ | |
34 switch (type) { | |
35 case ICON_SET_MANIFEST_ICONS: | |
36 allowed_sizes_ = kExtensionIconSizes; | |
Aaron Boodman
2012/08/10 23:01:57
I do not think it is necessary to validate that th
tbarzic
2012/08/11 00:58:45
Done.
| |
37 num_allowed_sizes_ = arraysize(kExtensionIconSizes); | |
38 break; | |
39 default: | |
40 NOTREACHED(); | |
41 } | |
42 | |
43 } | |
10 | 44 |
11 ExtensionIconSet::~ExtensionIconSet() {} | 45 ExtensionIconSet::~ExtensionIconSet() {} |
12 | 46 |
13 const ExtensionIconSet::Icons ExtensionIconSet::kIconSizes[] = { | |
14 EXTENSION_ICON_GIGANTOR, | |
15 EXTENSION_ICON_EXTRA_LARGE, | |
16 EXTENSION_ICON_LARGE, | |
17 EXTENSION_ICON_MEDIUM, | |
18 EXTENSION_ICON_SMALL, | |
19 EXTENSION_ICON_SMALLISH, | |
20 EXTENSION_ICON_BITTY | |
21 }; | |
22 | |
23 const size_t ExtensionIconSet::kNumIconSizes = | |
24 arraysize(ExtensionIconSet::kIconSizes); | |
25 | |
26 void ExtensionIconSet::Clear() { | 47 void ExtensionIconSet::Clear() { |
27 map_.clear(); | 48 map_.clear(); |
28 } | 49 } |
29 | 50 |
30 void ExtensionIconSet::Add(Icons size, const std::string& path) { | 51 void ExtensionIconSet::Add(int size, const std::string& path) { |
31 DCHECK(!path.empty() && path[0] != '/'); | 52 DCHECK(!path.empty() && path[0] != '/'); |
53 CHECK(IsInArray(allowed_sizes_, num_allowed_sizes_, size)); | |
54 | |
32 map_[size] = path; | 55 map_[size] = path; |
33 } | 56 } |
34 | 57 |
35 std::string ExtensionIconSet::Get(int size, MatchType match_type) const { | 58 std::string ExtensionIconSet::Get(int size, MatchType match_type) const { |
36 // The searches for MATCH_BIGGER and MATCH_SMALLER below rely on the fact that | 59 // The searches for MATCH_BIGGER and MATCH_SMALLER below rely on the fact that |
37 // std::map is sorted. This is per the spec, so it should be safe to rely on. | 60 // std::map is sorted. This is per the spec, so it should be safe to rely on. |
38 if (match_type == MATCH_EXACTLY) { | 61 if (match_type == MATCH_EXACTLY) { |
39 IconMap::const_iterator result = map_.find(static_cast<Icons>(size)); | 62 IconMap::const_iterator result = map_.find(size); |
40 return result == map_.end() ? std::string() : result->second; | 63 return result == map_.end() ? std::string() : result->second; |
41 } else if (match_type == MATCH_SMALLER) { | 64 } else if (match_type == MATCH_SMALLER) { |
42 IconMap::const_reverse_iterator result = map_.rend(); | 65 IconMap::const_reverse_iterator result = map_.rend(); |
43 for (IconMap::const_reverse_iterator iter = map_.rbegin(); | 66 for (IconMap::const_reverse_iterator iter = map_.rbegin(); |
44 iter != map_.rend(); ++iter) { | 67 iter != map_.rend(); ++iter) { |
45 if (iter->first <= size) { | 68 if (iter->first <= size) { |
46 result = iter; | 69 result = iter; |
47 break; | 70 break; |
48 } | 71 } |
49 } | 72 } |
50 return result == map_.rend() ? std::string() : result->second; | 73 return result == map_.rend() ? std::string() : result->second; |
51 } else { | 74 } else { |
52 DCHECK(match_type == MATCH_BIGGER); | 75 DCHECK(match_type == MATCH_BIGGER); |
53 IconMap::const_iterator result = map_.end(); | 76 IconMap::const_iterator result = map_.end(); |
54 for (IconMap::const_iterator iter = map_.begin(); iter != map_.end(); | 77 for (IconMap::const_iterator iter = map_.begin(); iter != map_.end(); |
55 ++iter) { | 78 ++iter) { |
56 if (iter->first >= size) { | 79 if (iter->first >= size) { |
57 result = iter; | 80 result = iter; |
58 break; | 81 break; |
59 } | 82 } |
60 } | 83 } |
61 return result == map_.end() ? std::string() : result->second; | 84 return result == map_.end() ? std::string() : result->second; |
62 } | 85 } |
63 } | 86 } |
64 | 87 |
65 bool ExtensionIconSet::ContainsPath(const std::string& path) const { | 88 bool ExtensionIconSet::ContainsPath(const std::string& path) const { |
66 return GetIconSizeFromPath(path) != EXTENSION_ICON_INVALID; | 89 return GetIconSizeFromPath(path) != 0; |
67 } | 90 } |
68 | 91 |
69 ExtensionIconSet::Icons ExtensionIconSet::GetIconSizeFromPath( | 92 int ExtensionIconSet::GetIconSizeFromPath(const std::string& path) const { |
70 const std::string& path) const { | |
71 if (path.empty()) | 93 if (path.empty()) |
72 return EXTENSION_ICON_INVALID; | 94 return 0; |
73 | 95 |
74 DCHECK(path[0] != '/') << | 96 DCHECK(path[0] != '/') << |
75 "ExtensionIconSet stores icon paths without leading slash."; | 97 "ExtensionIconSet stores icon paths without leading slash."; |
76 | 98 |
77 for (IconMap::const_iterator iter = map_.begin(); iter != map_.end(); | 99 for (IconMap::const_iterator iter = map_.begin(); iter != map_.end(); |
78 ++iter) { | 100 ++iter) { |
79 if (iter->second == path) | 101 if (iter->second == path) |
80 return iter->first; | 102 return iter->first; |
81 } | 103 } |
82 | 104 |
83 return EXTENSION_ICON_INVALID; | 105 return 0; |
84 } | 106 } |
OLD | NEW |