1// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/android/resource_mapper.h"
6
7#include <map>
8
9#include "base/lazy_instance.h"
10#include "base/logging.h"
11#include "grit/theme_resources.h"
12
13namespace {
14
15typedef std::map<int, int> ResourceMap;
16base::LazyInstance<ResourceMap>::Leaky g_id_map = LAZY_INSTANCE_INITIALIZER;
17
18} // namespace
19
20const int ResourceMapper::kMissingId = -1;
21
22int ResourceMapper::MapFromChromiumId(int resource_id) {
23  if (g_id_map.Get().empty()) {
24    ConstructMap();
25  }
26
27  ResourceMap::iterator iterator = g_id_map.Get().find(resource_id);
28  if (iterator != g_id_map.Get().end()) {
29    return iterator->second;
30  }
31
32  // The resource couldn't be found.
33  NOTREACHED();
34  return kMissingId;
35}
36
37void ResourceMapper::ConstructMap() {
38  DCHECK(g_id_map.Get().empty());
39  int next_id = 0;
40
41#define DEFINE_RESOURCE_ID(c_id,java_id) g_id_map.Get()[c_id] = next_id++;
42#include "chrome/browser/android/resource_id.h"
43#undef DEFINE_RESOURCE_ID
44}
45