summaryrefslogtreecommitdiffstats
path: root/app/resource_bundle_mac.mm
blob: 6696617cd1c5b66aa2fe8b979072949a17b2348f (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright (c) 2006-2008 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 "app/resource_bundle.h"

#import <Foundation/Foundation.h>

#include "app/gfx/font.h"
#include "app/l10n_util.h"
#include "base/base_paths.h"
#include "base/data_pack.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/logging.h"
#include "base/mac_util.h"
#include "base/path_service.h"
#include "base/string_piece.h"
#include "base/string_util.h"
#include "skia/ext/skia_utils_mac.h"

ResourceBundle::~ResourceBundle() {
  FreeImages();

  delete locale_resources_data_;
  locale_resources_data_ = NULL;
  delete theme_data_;
  theme_data_ = NULL;
  delete resources_data_;
  resources_data_ = NULL;
}

namespace {

base::DataPack *LoadResourceDataPack(NSString *name) {
  base::DataPack *resource_pack = NULL;

  NSString *resource_path = [mac_util::MainAppBundle() pathForResource:name
                                                                ofType:@"pak"];
  if (resource_path) {
    FilePath resources_pak_path([resource_path fileSystemRepresentation]);
    resource_pack = new base::DataPack;
    bool success = resource_pack->Load(resources_pak_path);
    if (!success) {
      delete resource_pack;
      resource_pack = NULL;
    }
  }

  return resource_pack;
}

}  // namespace

void ResourceBundle::LoadResources(const std::wstring& pref_locale) {
  DLOG_IF(WARNING, pref_locale.size() != 0)
      << "ignoring requested locale in favor of NSBundle's selection";

  DCHECK(resources_data_ == NULL) << "resource data already loaded!";
  resources_data_ = LoadResourceDataPack(@"chrome");
  DCHECK(resources_data_) << "failed to load chrome.pak";

  DCHECK(locale_resources_data_ == NULL) << "locale data already loaded!";
  locale_resources_data_ = LoadResourceDataPack(@"locale");
  DCHECK(locale_resources_data_) << "failed to load locale.pak";
}

void ResourceBundle::LoadThemeResources() {
  DCHECK(theme_data_ == NULL) << "theme data already loaded!";
  theme_data_ = LoadResourceDataPack(@"theme");
  DCHECK(theme_data_) << "failed to load theme.pak";
}

/* static */
bool ResourceBundle::LoadResourceBytes(DataHandle module, int resource_id,
                                       std::vector<unsigned char>* bytes) {
  DCHECK(module);
  base::StringPiece data;
  if (!module->Get(resource_id, &data))
    return false;

  bytes->resize(data.length());
  memcpy(&(bytes->front()), data.data(), data.length());

  return true;
}

base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) {
  DCHECK(resources_data_);
  base::StringPiece data;
  if (!resources_data_->Get(resource_id, &data))
    return base::StringPiece();
  return data;
}

string16 ResourceBundle::GetLocalizedString(int message_id) {
  // If for some reason we were unable to load a resource dll, return an empty
  // string (better than crashing).
  if (!locale_resources_data_) {
    LOG(WARNING) << "locale resources are not loaded";
    return string16();
  }

  base::StringPiece data;
  if (!locale_resources_data_->Get(message_id, &data)) {
    // Fall back on the main data pack (shouldn't be any strings here except in
    // unittests).
    data = GetRawDataResource(message_id);
    if (data.empty()) {
      NOTREACHED() << "unable to find resource: " << message_id;
      return string16();
    }
  }

  // Data pack encodes strings as UTF16.
  string16 msg(reinterpret_cast<const char16*>(data.data()),
               data.length() / 2);
  return msg;
}

NSImage* ResourceBundle::GetNSImageNamed(int resource_id) {
  // Currently this doesn't make a cache holding these as NSImages because
  // GetBitmapNamed has a cache, and we don't want to double cache.
  SkBitmap* bitmap = GetBitmapNamed(resource_id);
  if (!bitmap)
    return nil;

  NSImage* nsimage = gfx::SkBitmapToNSImage(*bitmap);
  return nsimage;
}