Skip to content

Commit

Permalink
Load resources.pak, which should be located next to the chrome
Browse files Browse the repository at this point in the history
binary.  resources.pak will contain all the files we currently have in the
resources subdirectory (it currently only has net-internals files).

On Linux, this is an eager load (before the zygote), but on Mac
and Win, this is lazily loaded as needed.  This isn't being used
yet, just adding the necessary plumbing for now.

BUG=42770

Review URL: http://codereview.chromium.org/2755006

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@49772 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
tony@chromium.org committed Jun 15, 2010
1 parent 4344db0 commit e1cb0e9
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 2 deletions.
32 changes: 32 additions & 0 deletions app/resource_bundle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

#include "app/resource_bundle.h"

#include "base/data_pack.h"
#include "base/logging.h"
#include "base/string_piece.h"
#include "build/build_config.h"
#include "gfx/codec/png_codec.h"
#include "gfx/font.h"
#include "third_party/skia/include/core/SkBitmap.h"
Expand Down Expand Up @@ -51,6 +53,12 @@ std::string ResourceBundle::ReloadSharedInstance(
return g_shared_instance_->LoadLocaleResources(pref_locale);
}

/* static */
void ResourceBundle::AddDataPackToSharedInstance(const FilePath& path) {
DCHECK(g_shared_instance_ != NULL) << "ResourceBundle not initialized";
g_shared_instance_->data_packs_.push_back(new LoadedDataPack(path));
}

/* static */
void ResourceBundle::CleanupSharedInstance() {
if (g_shared_instance_) {
Expand Down Expand Up @@ -186,3 +194,27 @@ const gfx::Font& ResourceBundle::GetFont(FontStyle style) {
return *base_font_;
}
}

// LoadedDataPack implementation
ResourceBundle::LoadedDataPack::LoadedDataPack(const FilePath& path)
: path_(path) {
// On unicies, we preload data packs so background updates don't cause us to
// load the wrong data.
#if defined(OS_POSIX) && !defined(OS_MACOSX)
Load();
#endif
}

void ResourceBundle::LoadedDataPack::Load() {
DCHECK(!data_pack_.get());
data_pack_.reset(new base::DataPack);
bool success = data_pack_->Load(path_);
CHECK(success) << "Failed to load " << path_.value();
}

bool ResourceBundle::LoadedDataPack::GetStringPiece(int resource_id,
base::StringPiece* data) {
if (!data_pack_.get())
Load();
return data_pack_->GetStringPiece(static_cast<uint32>(resource_id), data);
}
27 changes: 25 additions & 2 deletions app/resource_bundle.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include <map>
#include <string>
#include <vector>

#include "base/basictypes.h"
#include "base/file_path.h"
Expand All @@ -21,11 +22,9 @@
#include "base/scoped_ptr.h"
#include "base/string16.h"

#if defined(USE_BASE_DATA_PACK)
namespace base {
class DataPack;
}
#endif
#if defined(USE_X11)
typedef struct _GdkPixbuf GdkPixbuf;
#endif
Expand Down Expand Up @@ -78,6 +77,12 @@ class ResourceBundle {
// the UI thread.
static std::string ReloadSharedInstance(const std::wstring& pref_locale);

// Registers additional data pack files with the global ResourceBundle. When
// looking for a DataResource, we will search these files after searching the
// main module. This method is not thread safe! You should call it
// immediately after calling InitSharedInstance.
static void AddDataPackToSharedInstance(const FilePath& path);

// Delete the ResourceBundle for this process if it exists.
static void CleanupSharedInstance();

Expand Down Expand Up @@ -151,6 +156,21 @@ class ResourceBundle {
static const SkColor toolbar_separator_color;

private:
// Helper class for managing data packs.
class LoadedDataPack {
public:
LoadedDataPack(const FilePath& path);
bool GetStringPiece(int resource_id, base::StringPiece* data);

private:
void Load();

scoped_ptr<base::DataPack> data_pack_;
FilePath path_;

DISALLOW_COPY_AND_ASSIGN(LoadedDataPack);
};

// We define a DataHandle typedef to abstract across how data is stored
// across platforms.
#if defined(OS_WIN)
Expand Down Expand Up @@ -217,6 +237,9 @@ class ResourceBundle {
DataHandle resources_data_;
DataHandle locale_resources_data_;

// References to extra data packs loaded via AddDataPackToSharedInstance.
std::vector<LoadedDataPack*> data_packs_;

// Cached images. The ResourceBundle caches all retrieved bitmaps and keeps
// ownership of the pointers.
typedef std::map<int, SkBitmap*> SkImageMap;
Expand Down
8 changes: 8 additions & 0 deletions app/resource_bundle_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "app/l10n_util.h"
#include "base/data_pack.h"
#include "base/logging.h"
#include "base/stl_util-inl.h"
#include "base/string16.h"
#include "base/string_piece.h"
#include "gfx/font.h"
Expand All @@ -31,6 +32,8 @@ ResourceBundle::~ResourceBundle() {
FreeGdkPixBufs();
#endif
UnloadLocaleResources();
STLDeleteContainerPointers(data_packs_.begin(),
data_packs_.end());
delete resources_data_;
resources_data_ = NULL;
}
Expand All @@ -52,6 +55,11 @@ base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) {
base::StringPiece data;
if (!resources_data_->GetStringPiece(resource_id, &data)) {
if (!locale_resources_data_->GetStringPiece(resource_id, &data)) {
for (size_t i = 0; i < data_packs_.size(); ++i) {
if (data_packs_[i]->GetStringPiece(resource_id, &data))
return data;
}

return base::StringPiece();
}
}
Expand Down
11 changes: 11 additions & 0 deletions app/resource_bundle_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@

#include "app/app_paths.h"
#include "app/l10n_util.h"
#include "base/data_pack.h"
#include "base/debug_util.h"
#include "base/file_util.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "base/resource_util.h"
#include "base/stl_util-inl.h"
#include "base/string_piece.h"
#include "base/win_util.h"
#include "gfx/font.h"
Expand All @@ -32,6 +34,8 @@ DWORD GetDataDllLoadFlags() {
ResourceBundle::~ResourceBundle() {
FreeImages();
UnloadLocaleResources();
STLDeleteContainerPointers(data_packs_.begin(),
data_packs_.end());
resources_data_ = NULL;
}

Expand Down Expand Up @@ -113,6 +117,13 @@ base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) {
&data_size)) {
return base::StringPiece(static_cast<const char*>(data_ptr), data_size);
}

base::StringPiece data;
for (size_t i = 0; i < data_packs_.size(); ++i) {
if (data_packs_[i]->GetStringPiece(resource_id, &data))
return data;
}

return base::StringPiece();
}

Expand Down
4 changes: 4 additions & 0 deletions chrome/browser/browser_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,10 @@ int BrowserMain(const MainFunctionParams& parameters) {
std::string app_locale = ResourceBundle::InitSharedInstance(
local_state->GetString(prefs::kApplicationLocale));
g_browser_process->SetApplicationLocale(app_locale);

FilePath resources_pack_path;
PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path);
ResourceBundle::AddDataPackToSharedInstance(resources_pack_path);
#endif // !defined(OS_MACOSX)
}

Expand Down
7 changes: 7 additions & 0 deletions chrome/browser/browser_main_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@
#include "app/resource_bundle.h"
#include "base/command_line.h"
#include "base/debug_util.h"
#include "base/file_path.h"
#include "base/mac_util.h"
#include "base/path_service.h"
#include "base/scoped_nsobject.h"
#include "chrome/app/breakpad_mac.h"
#import "chrome/browser/app_controller_mac.h"
#include "chrome/browser/browser_main_win.h"
#import "chrome/browser/chrome_browser_application_mac.h"
#import "chrome/browser/cocoa/keystone_glue.h"
#include "chrome/browser/metrics/metrics_service.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/main_function_params.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/result_codes.h"
Expand All @@ -44,6 +47,10 @@ void WillInitializeMainMessageLoop(const MainFunctionParams& parameters) {
// Before we load the nib, we need to start up the resource bundle so we
// have the strings avaiable for localization.
ResourceBundle::InitSharedInstance(std::wstring());

FilePath resources_pack_path;
PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path);
ResourceBundle::AddDataPackToSharedInstance(resources_pack_path);
}

// Now load the nib (from the right bundle).
Expand Down
5 changes: 5 additions & 0 deletions chrome/common/chrome_paths.cc
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,11 @@ bool PathProvider(int key, FilePath* result) {
if (!file_util::PathExists(cur))
return false;
break;
case chrome::FILE_RESOURCES_PACK:
if (!PathService::Get(base::DIR_EXE, &cur))
return false;
cur = cur.Append(FILE_PATH_LITERAL("resources.pak"));
break;
#if defined(OS_CHROMEOS)
case chrome::FILE_CHROMEOS_API:
if (!PathService::Get(base::DIR_MODULE, &cur))
Expand Down
3 changes: 3 additions & 0 deletions chrome/common/chrome_paths.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ enum {
FILE_LIBAVFORMAT, // Full path to libavformat media parsing
// library.
FILE_LIBAVUTIL, // Full path to libavutil media utility library.
FILE_RESOURCES_PACK, // Full path to the .pak file containing
// binary data (e.g., html files and images
// used by interal pages).
#if defined(OS_CHROMEOS)
FILE_CHROMEOS_API, // Full path to chrome os api shared object.
#endif
Expand Down
1 change: 1 addition & 0 deletions chrome/tools/build/linux/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ libffmpegsumo.so
locales
product_logo_48.png
resources
resources.pak
xdg-settings
libpdf.so
1 change: 1 addition & 0 deletions chrome/tools/build/win/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ locales/vi.dll
locales/zh-CN.dll
locales/zh-TW.dll
resources
resources.pak
wow_helper.exe
gcswf32.dll
plugin.vch
Expand Down

0 comments on commit e1cb0e9

Please sign in to comment.