Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(267)

Side by Side Diff: app/resource_bundle_mac.mm

Issue 442002: Share code between Mac and Linux in ResourceBundle. (Closed)
Patch Set: sync Created 11 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « app/resource_bundle_linux.cc ('k') | app/resource_bundle_posix.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 "app/resource_bundle.h" 5 #include "app/resource_bundle.h"
6 6
7 #import <Foundation/Foundation.h> 7 #import <Foundation/Foundation.h>
8 8
9 #include "app/gfx/font.h"
10 #include "app/l10n_util.h"
11 #include "base/base_paths.h"
12 #include "base/data_pack.h"
13 #include "base/file_path.h" 9 #include "base/file_path.h"
14 #include "base/file_util.h"
15 #include "base/logging.h" 10 #include "base/logging.h"
16 #include "base/mac_util.h" 11 #include "base/mac_util.h"
17 #include "base/path_service.h"
18 #include "base/string_piece.h"
19 #include "base/string_util.h"
20 #include "skia/ext/skia_utils_mac.h" 12 #include "skia/ext/skia_utils_mac.h"
21 13
22 ResourceBundle::~ResourceBundle() {
23 FreeImages();
24
25 delete locale_resources_data_;
26 locale_resources_data_ = NULL;
27 delete resources_data_;
28 resources_data_ = NULL;
29 }
30
31 namespace { 14 namespace {
32 15
33 base::DataPack *LoadResourceDataPack(NSString *name) { 16 FilePath GetResourcesPakFilePath(NSString* name) {
34 base::DataPack *resource_pack = NULL;
35
36 NSString *resource_path = [mac_util::MainAppBundle() pathForResource:name 17 NSString *resource_path = [mac_util::MainAppBundle() pathForResource:name
37 ofType:@"pak"]; 18 ofType:@"pak"];
38 if (resource_path) { 19 if (!resource_path)
39 FilePath resources_pak_path([resource_path fileSystemRepresentation]); 20 return FilePath();
40 resource_pack = new base::DataPack; 21 return FilePath([resource_path fileSystemRepresentation]);
41 bool success = resource_pack->Load(resources_pak_path);
42 if (!success) {
43 delete resource_pack;
44 resource_pack = NULL;
45 }
46 }
47
48 return resource_pack;
49 } 22 }
50 23
51 } // namespace 24 } // namespace
52 25
53 void ResourceBundle::LoadResources(const std::wstring& pref_locale) { 26 // static
54 DLOG_IF(WARNING, pref_locale.size() != 0) 27 FilePath ResourceBundle::GetResourcesFilePath() {
55 << "ignoring requested locale in favor of NSBundle's selection"; 28 return GetResourcesPakFilePath(@"chrome");
56
57 DCHECK(resources_data_ == NULL) << "resource data already loaded!";
58 resources_data_ = LoadResourceDataPack(@"chrome");
59 DCHECK(resources_data_) << "failed to load chrome.pak";
60
61 DCHECK(locale_resources_data_ == NULL) << "locale data already loaded!";
62 locale_resources_data_ = LoadResourceDataPack(@"locale");
63 DCHECK(locale_resources_data_) << "failed to load locale.pak";
64 } 29 }
65 30
66 // static 31 // static
67 RefCountedStaticMemory* ResourceBundle::LoadResourceBytes( 32 FilePath ResourceBundle::GetLocaleFilePath(const std::wstring& pref_locale) {
68 DataHandle module, int resource_id) { 33 DLOG_IF(WARNING, !pref_locale.empty())
69 DCHECK(module); 34 << "ignoring requested locale in favor of NSBundle's selection";
70 return module->GetStaticMemory(resource_id); 35 return GetResourcesPakFilePath(@"locale");
71 }
72
73 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) {
74 DCHECK(resources_data_);
75 base::StringPiece data;
76 if (!resources_data_->GetStringPiece(resource_id, &data)) {
77 if (!locale_resources_data_->GetStringPiece(resource_id, &data)) {
78 return base::StringPiece();
79 }
80 }
81 return data;
82 }
83
84 string16 ResourceBundle::GetLocalizedString(int message_id) {
85 // If for some reason we were unable to load a resource dll, return an empty
86 // string (better than crashing).
87 if (!locale_resources_data_) {
88 LOG(WARNING) << "locale resources are not loaded";
89 return string16();
90 }
91
92 base::StringPiece data;
93 if (!locale_resources_data_->GetStringPiece(message_id, &data)) {
94 // Fall back on the main data pack (shouldn't be any strings here except in
95 // unittests).
96 data = GetRawDataResource(message_id);
97 if (data.empty()) {
98 NOTREACHED() << "unable to find resource: " << message_id;
99 return string16();
100 }
101 }
102
103 // Data pack encodes strings as UTF16.
104 string16 msg(reinterpret_cast<const char16*>(data.data()),
105 data.length() / 2);
106 return msg;
107 } 36 }
108 37
109 NSImage* ResourceBundle::GetNSImageNamed(int resource_id) { 38 NSImage* ResourceBundle::GetNSImageNamed(int resource_id) {
110 // Currently this doesn't make a cache holding these as NSImages because 39 // Currently this doesn't make a cache holding these as NSImages because
111 // GetBitmapNamed has a cache, and we don't want to double cache. 40 // GetBitmapNamed has a cache, and we don't want to double cache.
112 SkBitmap* bitmap = GetBitmapNamed(resource_id); 41 SkBitmap* bitmap = GetBitmapNamed(resource_id);
113 if (!bitmap) 42 if (!bitmap)
114 return nil; 43 return nil;
115 44
116 NSImage* nsimage = gfx::SkBitmapToNSImage(*bitmap); 45 NSImage* nsimage = gfx::SkBitmapToNSImage(*bitmap);
117 return nsimage; 46 return nsimage;
118 } 47 }
OLDNEW
« no previous file with comments | « app/resource_bundle_linux.cc ('k') | app/resource_bundle_posix.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698