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

Side by Side Diff: chrome/common/resource_bundle_mac.mm

Issue 28171: Wire up resource bundles on the mac:... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 11 years, 10 months 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 | Annotate | Revision Log
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 "chrome/common/resource_bundle.h" 5 #include "chrome/common/resource_bundle.h"
6 6
7 #include <gtk/gtk.h> 7 #include <Foundation/Foundation.h>
Mark Mentovai 2009/02/26 04:08:42 #import Cocoa headers.
TVL 2009/02/26 12:25:34 done (we don't want the min needed headers?)
8 8
9 #include "base/base_paths.h" 9 #include "base/base_paths.h"
10 #include "base/data_pack.h" 10 #include "base/data_pack.h"
11 #include "base/file_path.h" 11 #include "base/file_path.h"
12 #include "base/file_util.h" 12 #include "base/file_util.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/path_service.h" 14 #include "base/path_service.h"
15 #include "base/string_piece.h" 15 #include "base/string_piece.h"
16 #include "base/string_util.h" 16 #include "base/string_util.h"
17 #include "chrome/common/chrome_paths.h" 17 #include "chrome/common/chrome_paths.h"
18 #include "chrome/common/gfx/chrome_font.h" 18 #include "chrome/common/gfx/chrome_font.h"
19 #include "chrome/common/l10n_util.h" 19 #include "chrome/common/l10n_util.h"
20 20
21 ResourceBundle::~ResourceBundle() { 21 ResourceBundle::~ResourceBundle() {
22 FreeImages(); 22 FreeImages();
23 23
24 delete locale_resources_data_; 24 delete locale_resources_data_;
25 locale_resources_data_ = NULL; 25 locale_resources_data_ = NULL;
26 delete theme_data_; 26 delete theme_data_;
27 theme_data_ = NULL; 27 theme_data_ = NULL;
28 delete resources_data_; 28 delete resources_data_;
29 resources_data_ = NULL; 29 resources_data_ = NULL;
30 } 30 }
31 31
32 void ResourceBundle::LoadResources(const std::wstring& pref_locale) { 32 namespace {
33 FilePath resources_data_path; 33 base::DataPack* LoadResourceDataPack(NSString *name) {
Mark Mentovai 2009/02/26 04:08:42 There's no extra level of indentation in an namesp
TVL 2009/02/26 12:25:34 Done.
34 PathService::Get(base::DIR_EXE, &resources_data_path); 34 base::DataPack *resource_pack = NULL;
Mark Mentovai 2009/02/26 04:08:42 A pet peeve of mine is code that can't decide whet
TVL 2009/02/26 12:25:34 Done.
35 resources_data_path = resources_data_path.Append( 35
36 FILE_PATH_LITERAL("chrome.pak")); 36 NSString *resource_path = [[NSBundle mainBundle] pathForResource:name
37 DCHECK(resources_data_ == NULL) << "resource data already loaded!"; 37 ofType:@"pak"];
38 resources_data_ = new base::DataPack; 38 if (resource_path) {
39 bool success = resources_data_->Load(resources_data_path); 39 FilePath resources_pak_path([resource_path fileSystemRepresentation]);
40 DCHECK(success) << "failed to load chrome.pak"; 40 resource_pack = new base::DataPack;
41 41 bool success = resource_pack->Load(resources_pak_path);
42 FilePath locale_path; 42 DCHECK(success) << "failed to load chrome.pak";
43 PathService::Get(chrome::DIR_LOCALES, &locale_path); 43 if (!success) {
44 // TODO(tc): Handle other locales properly. 44 delete resource_pack;
45 NOTIMPLEMENTED() << " loading en-US strings only"; 45 resource_pack = NULL;
46 locale_path = locale_path.Append(FILE_PATH_LITERAL("en-US.pak")); 46 }
47 DCHECK(locale_resources_data_ == NULL) << "locale data already loaded!"; 47 }
48 locale_resources_data_ = new base::DataPack; 48
49 success = locale_resources_data_->Load(locale_path); 49 return resource_pack;
50 DCHECK(success) << "failed to load locale pak file"; 50 }
51 } 51 }
Mark Mentovai 2009/02/26 04:08:42 At the end of the namespace: } // namespace
TVL 2009/02/26 12:25:34 Done.
52 52
53 FilePath ResourceBundle::GetLocaleFilePath(const std::wstring& pref_locale) { 53 void ResourceBundle::LoadResources(const std::wstring& pref_locale) {
54 FilePath locale_path; 54 DCHECK(pref_locale.size() == 0)
55 PathService::Get(chrome::DIR_LOCALES, &locale_path); 55 << "ignoring resource langage for cocoa's choice";
Mark Mentovai 2009/02/26 04:08:42 I have no idea what this message means or why it h
TVL 2009/02/26 12:25:34 Done.
56
57 DCHECK(resources_data_ == NULL) << "resource data already loaded!";
58 resources_data_ = LoadResourceDataPack(@"chrome");
59 DCHECK(resources_data_) << "failed to load chrome.pak";
56 60
57 const std::wstring app_locale = l10n_util::GetApplicationLocale(pref_locale); 61 DCHECK(locale_resources_data_ == NULL) << "locale data already loaded!";
58 if (app_locale.empty()) 62 locale_resources_data_ = LoadResourceDataPack(@"locale");
59 return FilePath(); 63 DCHECK(locale_resources_data_) << "failed to load locale.pak";
60
61 return locale_path.Append(WideToASCII(app_locale + L".pak"));
62 } 64 }
63 65
64 void ResourceBundle::LoadThemeResources() { 66 void ResourceBundle::LoadThemeResources() {
65 FilePath theme_data_path; 67 DCHECK(theme_data_ == NULL) << "theme data already loaded!";
66 PathService::Get(chrome::DIR_THEMES, &theme_data_path); 68 theme_data_ = LoadResourceDataPack(@"theme");
67 theme_data_path = theme_data_path.Append(FILE_PATH_LITERAL("default.pak")); 69 DCHECK(theme_data_) << "failed to load theme.pak";
68 theme_data_ = new base::DataPack;
69 bool success = theme_data_->Load(theme_data_path);
70 DCHECK(success) << "failed to load theme data";
71 } 70 }
72 71
73 /* static */ 72 /* static */
74 bool ResourceBundle::LoadResourceBytes(DataHandle module, int resource_id, 73 bool ResourceBundle::LoadResourceBytes(DataHandle module, int resource_id,
75 std::vector<unsigned char>* bytes) { 74 std::vector<unsigned char>* bytes) {
76 DCHECK(module); 75 DCHECK(module);
77 StringPiece data; 76 StringPiece data;
78 if (!module->Get(resource_id, &data)) 77 if (!module->Get(resource_id, &data))
79 return false; 78 return false;
80 79
(...skipping 28 matching lines...) Expand all
109 NOTREACHED() << "unable to find resource: " << message_id; 108 NOTREACHED() << "unable to find resource: " << message_id;
110 return std::wstring(); 109 return std::wstring();
111 } 110 }
112 } 111 }
113 112
114 // Data pack encodes strings as UTF16. 113 // Data pack encodes strings as UTF16.
115 string16 msg(reinterpret_cast<const char16*>(data.data()), 114 string16 msg(reinterpret_cast<const char16*>(data.data()),
116 data.length() / 2); 115 data.length() / 2);
117 return UTF16ToWide(msg); 116 return UTF16ToWide(msg);
118 } 117 }
119
120 GdkPixbuf* ResourceBundle::LoadPixbuf(int resource_id) {
121 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
122 std::vector<unsigned char> data;
123 rb.LoadImageResourceBytes(resource_id, &data);
124
125 GdkPixbufLoader* loader = gdk_pixbuf_loader_new();
126 bool ok = gdk_pixbuf_loader_write(loader, static_cast<guint8*>(data.data()),
127 data.size(), NULL);
128 DCHECK(ok) << "failed to write " << resource_id;
129 // Calling gdk_pixbuf_loader_close forces the data to be parsed by the
130 // loader. We must do this before calling gdk_pixbuf_loader_get_pixbuf.
131 ok = gdk_pixbuf_loader_close(loader, NULL);
132 DCHECK(ok) << "close failed " << resource_id;
133 GdkPixbuf* pixbuf = gdk_pixbuf_loader_get_pixbuf(loader);
134 DCHECK(pixbuf) << "failed to load " << resource_id << " " << data.size();
135
136 // The pixbuf is owned by the loader, so add a ref so when we delete the
137 // loader, the pixbuf still exists.
138 g_object_ref(pixbuf);
139 g_object_unref(loader);
140
141 return pixbuf;
142 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698