| 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 ExtensionIconSet::ExtensionIconSet() {} |
| 10 | 10 |
| 11 ExtensionIconSet::~ExtensionIconSet() {} | 11 ExtensionIconSet::~ExtensionIconSet() {} |
| 12 | 12 |
| 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() { | 13 void ExtensionIconSet::Clear() { |
| 27 map_.clear(); | 14 map_.clear(); |
| 28 } | 15 } |
| 29 | 16 |
| 30 void ExtensionIconSet::Add(Icons size, const std::string& path) { | 17 void ExtensionIconSet::Add(int size, const std::string& path) { |
| 31 DCHECK(!path.empty() && path[0] != '/'); | 18 DCHECK(!path.empty() && path[0] != '/'); |
| 32 map_[size] = path; | 19 map_[size] = path; |
| 33 } | 20 } |
| 34 | 21 |
| 35 std::string ExtensionIconSet::Get(int size, MatchType match_type) const { | 22 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 | 23 // 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. | 24 // std::map is sorted. This is per the spec, so it should be safe to rely on. |
| 38 if (match_type == MATCH_EXACTLY) { | 25 if (match_type == MATCH_EXACTLY) { |
| 39 IconMap::const_iterator result = map_.find(static_cast<Icons>(size)); | 26 IconMap::const_iterator result = map_.find(size); |
| 40 return result == map_.end() ? std::string() : result->second; | 27 return result == map_.end() ? std::string() : result->second; |
| 41 } else if (match_type == MATCH_SMALLER) { | 28 } else if (match_type == MATCH_SMALLER) { |
| 42 IconMap::const_reverse_iterator result = map_.rend(); | 29 IconMap::const_reverse_iterator result = map_.rend(); |
| 43 for (IconMap::const_reverse_iterator iter = map_.rbegin(); | 30 for (IconMap::const_reverse_iterator iter = map_.rbegin(); |
| 44 iter != map_.rend(); ++iter) { | 31 iter != map_.rend(); ++iter) { |
| 45 if (iter->first <= size) { | 32 if (iter->first <= size) { |
| 46 result = iter; | 33 result = iter; |
| 47 break; | 34 break; |
| 48 } | 35 } |
| 49 } | 36 } |
| 50 return result == map_.rend() ? std::string() : result->second; | 37 return result == map_.rend() ? std::string() : result->second; |
| 51 } else { | 38 } else { |
| 52 DCHECK(match_type == MATCH_BIGGER); | 39 DCHECK(match_type == MATCH_BIGGER); |
| 53 IconMap::const_iterator result = map_.end(); | 40 IconMap::const_iterator result = map_.end(); |
| 54 for (IconMap::const_iterator iter = map_.begin(); iter != map_.end(); | 41 for (IconMap::const_iterator iter = map_.begin(); iter != map_.end(); |
| 55 ++iter) { | 42 ++iter) { |
| 56 if (iter->first >= size) { | 43 if (iter->first >= size) { |
| 57 result = iter; | 44 result = iter; |
| 58 break; | 45 break; |
| 59 } | 46 } |
| 60 } | 47 } |
| 61 return result == map_.end() ? std::string() : result->second; | 48 return result == map_.end() ? std::string() : result->second; |
| 62 } | 49 } |
| 63 } | 50 } |
| 64 | 51 |
| 65 bool ExtensionIconSet::ContainsPath(const std::string& path) const { | 52 bool ExtensionIconSet::ContainsPath(const std::string& path) const { |
| 66 return GetIconSizeFromPath(path) != EXTENSION_ICON_INVALID; | 53 return GetIconSizeFromPath(path) != 0; |
| 67 } | 54 } |
| 68 | 55 |
| 69 ExtensionIconSet::Icons ExtensionIconSet::GetIconSizeFromPath( | 56 int ExtensionIconSet::GetIconSizeFromPath(const std::string& path) const { |
| 70 const std::string& path) const { | |
| 71 if (path.empty()) | 57 if (path.empty()) |
| 72 return EXTENSION_ICON_INVALID; | 58 return 0; |
| 73 | 59 |
| 74 DCHECK(path[0] != '/') << | 60 DCHECK(path[0] != '/') << |
| 75 "ExtensionIconSet stores icon paths without leading slash."; | 61 "ExtensionIconSet stores icon paths without leading slash."; |
| 76 | 62 |
| 77 for (IconMap::const_iterator iter = map_.begin(); iter != map_.end(); | 63 for (IconMap::const_iterator iter = map_.begin(); iter != map_.end(); |
| 78 ++iter) { | 64 ++iter) { |
| 79 if (iter->second == path) | 65 if (iter->second == path) |
| 80 return iter->first; | 66 return iter->first; |
| 81 } | 67 } |
| 82 | 68 |
| 83 return EXTENSION_ICON_INVALID; | 69 return 0; |
| 84 } | 70 } |
| OLD | NEW |