blob: 6fa3a7ebf38337bc5be12eed6619bf252b47d3df [file] [log] [blame]
[email protected]07c00d992009-03-04 20:27:041// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
5#ifndef CHROME_COMMON_RESOURCE_BUNDLE_H__
6#define CHROME_COMMON_RESOURCE_BUNDLE_H__
7
[email protected]11c2fe82009-01-13 01:52:598#include "build/build_config.h"
9
10#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:2911#include <windows.h>
[email protected]11c2fe82009-01-13 01:52:5912#endif
13
initial.commit09911bf2008-07-26 23:55:2914#include <map>
15#include <string>
16
17#include "base/basictypes.h"
[email protected]11c2fe82009-01-13 01:52:5918#include "base/file_path.h"
initial.commit09911bf2008-07-26 23:55:2919#include "base/lock.h"
20#include "base/scoped_ptr.h"
[email protected]07c00d992009-03-04 20:27:0421#include "chrome/browser/extensions/extension.h"
initial.commit09911bf2008-07-26 23:55:2922
[email protected]4b0d2dd2009-02-26 12:41:4023#if defined(OS_LINUX) || defined(OS_MACOSX)
[email protected]1a141882009-01-13 21:35:3224namespace base {
25 class DataPack;
26};
[email protected]4b0d2dd2009-02-26 12:41:4027#endif
28#if defined(OS_LINUX)
[email protected]e6ba5402009-02-20 19:15:0229typedef struct _GdkPixbuf GdkPixbuf;
[email protected]1a141882009-01-13 21:35:3230#endif
initial.commit09911bf2008-07-26 23:55:2931class ChromeFont;
[email protected]07c00d992009-03-04 20:27:0432class Extension;
initial.commit09911bf2008-07-26 23:55:2933class SkBitmap;
34class StringPiece;
35
[email protected]11c2fe82009-01-13 01:52:5936// ResourceBundle is a central facility to load images and other resources,
37// such as theme graphics.
38// Every resource is loaded only once.
initial.commit09911bf2008-07-26 23:55:2939class ResourceBundle {
40 public:
41 // An enumeration of the various font styles used throughout Chrome.
42 // The following holds true for the font sizes:
43 // Small <= Base <= Medium <= MediumBold <= Large.
44 enum FontStyle {
45 SmallFont,
46 BaseFont,
47 MediumFont,
[email protected]11c2fe82009-01-13 01:52:5948 // NOTE: depending upon the locale, this may *not* result in a bold font.
initial.commit09911bf2008-07-26 23:55:2949 MediumBoldFont,
50 LargeFont,
initial.commit09911bf2008-07-26 23:55:2951 };
52
[email protected]11c2fe82009-01-13 01:52:5953 // Initialize the ResourceBundle for this process.
[email protected]4b0d2dd2009-02-26 12:41:4054 // NOTE: Mac ignores this and always loads up resources for the language
55 // defined by the Cocoa UI (ie-NSBundle does the langange work).
initial.commit09911bf2008-07-26 23:55:2956 static void InitSharedInstance(const std::wstring& pref_locale);
57
58 // Delete the ResourceBundle for this process if it exists.
59 static void CleanupSharedInstance();
60
[email protected]11c2fe82009-01-13 01:52:5961 // Return the global resource loader instance.
initial.commit09911bf2008-07-26 23:55:2962 static ResourceBundle& GetSharedInstance();
63
[email protected]11c2fe82009-01-13 01:52:5964 // Load the data file that contains theme resources if present.
initial.commit09911bf2008-07-26 23:55:2965 void LoadThemeResources();
66
67 // Gets the bitmap with the specified resource_id, first by looking into the
[email protected]11c2fe82009-01-13 01:52:5968 // theme data, than in the current module data if applicable.
69 // Returns a pointer to a shared instance of the SkBitmap in the given out
70 // parameter. This shared bitmap is owned by the resource bundle and should
71 // not be freed.
initial.commit09911bf2008-07-26 23:55:2972 //
73 // The bitmap is assumed to exist. This function will log in release, and
74 // assert in debug mode if it does not. On failure, this will return a
75 // pointer to a shared empty placeholder bitmap so it will be visible what
76 // is missing.
77 SkBitmap* GetBitmapNamed(int resource_id);
78
79 // Loads the raw bytes of an image resource into |bytes|,
80 // without doing any processing or interpretation of
81 // the resource. Returns whether we successfully read the resource.
82 bool LoadImageResourceBytes(int resource_id,
83 std::vector<unsigned char>* bytes);
84
85 // Loads the raw bytes of a data resource into |bytes|,
86 // without doing any processing or interpretation of
87 // the resource. Returns whether we successfully read the resource.
88 bool LoadDataResourceBytes(int resource_id,
89 std::vector<unsigned char>* bytes);
90
initial.commit09911bf2008-07-26 23:55:2991 // Return the contents of a file in a string given the resource id.
92 // This will copy the data from the resource and return it as a string.
[email protected]11c2fe82009-01-13 01:52:5993 // TODO(port): deprecate this and replace with GetRawDataResource to avoid
94 // needless copying.
initial.commit09911bf2008-07-26 23:55:2995 std::string GetDataResource(int resource_id);
96
97 // Like GetDataResource(), but avoids copying the resource. Instead, it
98 // returns a StringPiece which points into the actual resource in the image.
99 StringPiece GetRawDataResource(int resource_id);
100
initial.commit09911bf2008-07-26 23:55:29101 // Get a localized string given a message id. Returns an empty
102 // string if the message_id is not found.
[email protected]267497152009-04-14 19:31:09103 string16 GetLocalizedString(int message_id);
initial.commit09911bf2008-07-26 23:55:29104
105 // Returns the font for the specified style.
106 ChromeFont GetFont(FontStyle style);
107
[email protected]11c2fe82009-01-13 01:52:59108#if defined(OS_WIN)
109 // Loads and returns an icon from the theme dll.
110 HICON LoadThemeIcon(int icon_id);
111
112 // Loads and returns the global accelerators.
113 HACCEL GetGlobalAccelerators();
114
115 // Loads and returns a cursor from the app module.
116 HCURSOR LoadCursor(int cursor_id);
[email protected]91462212009-04-10 15:09:39117#elif defined(TOOLKIT_GTK)
[email protected]e6ba5402009-02-20 19:15:02118 // Load a theme image as a GdkPixbuf.
119 GdkPixbuf* LoadPixbuf(int resource_id);
120#endif
initial.commit09911bf2008-07-26 23:55:29121
[email protected]07c00d992009-03-04 20:27:04122 // Sets an Extension object that can handle theme resource requests.
123 void SetThemeExtension(const Extension& e);
124
initial.commit09911bf2008-07-26 23:55:29125 private:
[email protected]11c2fe82009-01-13 01:52:59126 // We define a DataHandle typedef to abstract across how data is stored
127 // across platforms.
128#if defined(OS_WIN)
129 // Windows stores resources in DLLs, which are managed by HINSTANCE.
130 typedef HINSTANCE DataHandle;
[email protected]4b0d2dd2009-02-26 12:41:40131#elif defined(OS_LINUX) || defined(OS_MACOSX)
[email protected]2aad8bb2009-02-04 19:00:50132 // Linux uses base::DataPack.
[email protected]11c2fe82009-01-13 01:52:59133 typedef base::DataPack* DataHandle;
134#endif
135
136 // Ctor/dtor are private, since we're a singleton.
initial.commit09911bf2008-07-26 23:55:29137 ResourceBundle();
138 ~ResourceBundle();
139
[email protected]11c2fe82009-01-13 01:52:59140 // Free skia_images_.
141 void FreeImages();
142
[email protected]2aad8bb2009-02-04 19:00:50143 // Try to load the main resources and the locale specific strings from an
144 // external data module.
145 void LoadResources(const std::wstring& pref_locale);
initial.commit09911bf2008-07-26 23:55:29146
[email protected]11c2fe82009-01-13 01:52:59147 // Initialize all the ChromeFont members if they haven't yet been initialized.
initial.commit09911bf2008-07-26 23:55:29148 void LoadFontsIfNecessary();
149
[email protected]11c2fe82009-01-13 01:52:59150 // Returns the full pathname of the locale file to load. May return an empty
151 // string if no locale data files are found.
152 FilePath GetLocaleFilePath(const std::wstring& pref_locale);
initial.commit09911bf2008-07-26 23:55:29153
154 // Loads the raw bytes of a resource from |module| into |bytes|,
155 // without doing any processing or interpretation of
156 // the resource. Returns whether we successfully read the resource.
[email protected]11c2fe82009-01-13 01:52:59157 static bool LoadResourceBytes(DataHandle module,
158 int resource_id,
159 std::vector<unsigned char>* bytes);
160
161 // Creates and returns a new SkBitmap given the data file to look in and the
162 // resource id. It's up to the caller to free the returned bitmap when
163 // done.
164 static SkBitmap* LoadBitmap(DataHandle dll_inst, int resource_id);
initial.commit09911bf2008-07-26 23:55:29165
166 // Class level lock. Used to protect internal data structures that may be
167 // accessed from other threads (e.g., skia_images_).
168 Lock lock_;
169
[email protected]11c2fe82009-01-13 01:52:59170 // Handles for data sources.
[email protected]2aad8bb2009-02-04 19:00:50171 DataHandle resources_data_;
[email protected]11c2fe82009-01-13 01:52:59172 DataHandle locale_resources_data_;
173 DataHandle theme_data_;
initial.commit09911bf2008-07-26 23:55:29174
175 // Cached images. The ResourceBundle caches all retrieved bitmaps and keeps
176 // ownership of the pointers.
177 typedef std::map<int, SkBitmap*> SkImageMap;
178 SkImageMap skia_images_;
179
180 // The various fonts used. Cached to avoid repeated GDI creation/destruction.
181 scoped_ptr<ChromeFont> base_font_;
182 scoped_ptr<ChromeFont> small_font_;
183 scoped_ptr<ChromeFont> medium_font_;
184 scoped_ptr<ChromeFont> medium_bold_font_;
185 scoped_ptr<ChromeFont> large_font_;
186 scoped_ptr<ChromeFont> web_font_;
187
[email protected]11c2fe82009-01-13 01:52:59188 static ResourceBundle* g_shared_instance_;
initial.commit09911bf2008-07-26 23:55:29189
[email protected]07c00d992009-03-04 20:27:04190 scoped_ptr<Extension> theme_extension_;
191
initial.commit09911bf2008-07-26 23:55:29192 DISALLOW_EVIL_CONSTRUCTORS(ResourceBundle);
193};
194
195#endif // CHROME_COMMON_RESOURCE_BUNDLE_H__