summaryrefslogtreecommitdiffstats
path: root/chrome/common/extensions/extension_icon_set.cc
blob: 588d0546f12f0cd5baffee63fb8c3fce781f9fb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/common/extensions/extension_icon_set.h"

#include "base/logging.h"

ExtensionIconSet::ExtensionIconSet() {}

ExtensionIconSet::~ExtensionIconSet() {}

const ExtensionIconSet::Icons ExtensionIconSet::kIconSizes[] = {
  EXTENSION_ICON_GIGANTOR,
  EXTENSION_ICON_EXTRA_LARGE,
  EXTENSION_ICON_LARGE,
  EXTENSION_ICON_MEDIUM,
  EXTENSION_ICON_SMALL,
  EXTENSION_ICON_SMALLISH,
  EXTENSION_ICON_BITTY
};

const size_t ExtensionIconSet::kNumIconSizes =
    arraysize(ExtensionIconSet::kIconSizes);

void ExtensionIconSet::Clear() {
  map_.clear();
}

void ExtensionIconSet::Add(Icons size, const std::string& path) {
  DCHECK(!path.empty() && path[0] != '/');
  map_[size] = path;
}

std::string ExtensionIconSet::Get(int size, MatchType match_type) const {
  // The searches for MATCH_BIGGER and MATCH_SMALLER below rely on the fact that
  // std::map is sorted. This is per the spec, so it should be safe to rely on.
  if (match_type == MATCH_EXACTLY) {
    IconMap::const_iterator result = map_.find(static_cast<Icons>(size));
    return result == map_.end() ? std::string() : result->second;
  } else if (match_type == MATCH_SMALLER) {
    IconMap::const_reverse_iterator result = map_.rend();
    for (IconMap::const_reverse_iterator iter = map_.rbegin();
         iter != map_.rend(); ++iter) {
      if (iter->first <= size) {
        result = iter;
        break;
      }
    }
    return result == map_.rend() ? std::string() : result->second;
  } else {
    DCHECK(match_type == MATCH_BIGGER);
    IconMap::const_iterator result = map_.end();
    for (IconMap::const_iterator iter = map_.begin(); iter != map_.end();
         ++iter) {
      if (iter->first >= size) {
        result = iter;
        break;
      }
    }
    return result == map_.end() ? std::string() : result->second;
  }
}

bool ExtensionIconSet::ContainsPath(const std::string& path) const {
  return GetIconSizeFromPath(path) != EXTENSION_ICON_INVALID;
}

ExtensionIconSet::Icons ExtensionIconSet::GetIconSizeFromPath(
    const std::string& path) const {
  if (path.empty())
    return EXTENSION_ICON_INVALID;

  DCHECK(path[0] != '/') <<
      "ExtensionIconSet stores icon paths without leading slash.";

  for (IconMap::const_iterator iter = map_.begin(); iter != map_.end();
       ++iter) {
    if (iter->second == path)
      return iter->first;
  }

  return EXTENSION_ICON_INVALID;
}