summaryrefslogtreecommitdiff
path: root/chromium/extensions/common/extension_icon_set.cc
blob: 50992479181a223e302848dc489931c63b7fdd7b (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
// Copyright 2014 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 "extensions/common/extension_icon_set.h"

#include <ostream>

#include "base/check_op.h"
#include "base/files/file_path.h"
#include "base/strings/string_util.h"

ExtensionIconSet::ExtensionIconSet() {}

ExtensionIconSet::ExtensionIconSet(const ExtensionIconSet& other) = default;

ExtensionIconSet::~ExtensionIconSet() {}

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

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

const std::string& ExtensionIconSet::Get(int size_in_px,
                                         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) {
    auto result = map_.find(size_in_px);
    return result == map_.end() ? base::EmptyString() : result->second;
  }
  if (match_type == MATCH_SMALLER) {
    auto result = map_.rend();
    for (auto iter = map_.rbegin(); iter != map_.rend(); ++iter) {
      if (iter->first <= size_in_px) {
        result = iter;
        break;
      }
    }
    return result == map_.rend() ? base::EmptyString() : result->second;
  }

  DCHECK(match_type == MATCH_BIGGER);
  auto result = map_.cend();
  for (auto iter = map_.cbegin(); iter != map_.cend(); ++iter) {
    if (iter->first >= size_in_px) {
      result = iter;
      break;
    }
  }
  return result == map_.cend() ? base::EmptyString() : result->second;
}

bool ExtensionIconSet::ContainsPath(base::StringPiece path) const {
  return GetIconSizeFromPath(path) != 0;
}

int ExtensionIconSet::GetIconSizeFromPath(base::StringPiece path) const {
  if (path.empty())
    return 0;

  DCHECK_NE(path[0], '/') <<
      "ExtensionIconSet stores icon paths without leading slash.";

  for (auto iter = map_.cbegin(); iter != map_.cend(); ++iter) {
    if (iter->second == path)
      return iter->first;
  }

  return 0;
}

void ExtensionIconSet::GetPaths(std::set<base::FilePath>* paths) const {
  CHECK(paths);
  for (const auto& iter : map())
    paths->insert(base::FilePath::FromUTF8Unsafe(iter.second));
}