Initialize resource bundle on browser process separately

This commit is contained in:
Cheng Zhao 2016-01-08 12:04:23 +08:00
parent 2374149b18
commit d6051e9580
5 changed files with 81 additions and 31 deletions

View file

@ -131,6 +131,8 @@
'SK_SUPPORT_LEGACY_SETCONFIG',
'SK_IGNORE_ETC1_SUPPORT',
'SK_IGNORE_GPU_DITHER',
# NACL is not enabled:
'DISABLE_NACL',
],
'conditions': [
['OS!="mac"', {

View file

@ -7,6 +7,7 @@
#include "browser/browser_context.h"
#include "browser/devtools_manager_delegate.h"
#include "browser/web_ui_controller_factory.h"
#include "common/main_delegate.h"
#include "base/command_line.h"
#include "base/strings/string_number_conversions.h"
@ -14,6 +15,7 @@
#include "content/public/browser/browser_thread.h"
#include "content/public/common/content_switches.h"
#include "net/proxy/proxy_resolver_v8.h"
#include "ui/base/l10n/l10n_util.h"
#if defined(USE_AURA)
#include "ui/gfx/screen.h"
@ -39,7 +41,6 @@
#if defined(OS_WIN)
#include "ui/base/cursor/cursor_loader_win.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/l10n/l10n_util_win.h"
#include "ui/gfx/platform_font_win.h"
#endif
@ -169,6 +170,10 @@ void BrowserMainParts::ToolkitInitialized() {
}
void BrowserMainParts::PreMainMessageLoopStart() {
#if defined(OS_MACOSX)
l10n_util::OverrideLocaleWithCocoaLocale();
#endif
InitializeResourceBundle("");
#if defined(OS_MACOSX)
InitializeMainNib();
#endif

View file

@ -10,11 +10,70 @@
#include "base/command_line.h"
#include "base/path_service.h"
#include "content/public/common/content_switches.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/ui_base_switches.h"
namespace brightray {
namespace {
// Returns true if this subprocess type needs the ResourceBundle initialized
// and resources loaded.
bool SubprocessNeedsResourceBundle(const std::string& process_type) {
return
#if defined(OS_WIN) || defined(OS_MACOSX)
// Windows needs resources for the default/null plugin.
// Mac needs them for the plugin process name.
process_type == switches::kPluginProcess ||
#endif
#if defined(OS_POSIX) && !defined(OS_MACOSX)
// The zygote process opens the resources for the renderers.
process_type == switches::kZygoteProcess ||
#endif
#if defined(OS_MACOSX)
// Mac needs them too for scrollbar related images and for sandbox
// profiles.
#if !defined(DISABLE_NACL)
process_type == switches::kNaClLoaderProcess ||
#endif
process_type == switches::kPpapiPluginProcess ||
process_type == switches::kPpapiBrokerProcess ||
process_type == switches::kGpuProcess ||
#endif
process_type == switches::kRendererProcess ||
process_type == switches::kUtilityProcess;
}
} // namespace
void InitializeResourceBundle(const std::string& locale) {
// Load locales.
ui::ResourceBundle::InitSharedInstanceWithLocale(
locale, nullptr, ui::ResourceBundle::DO_NOT_LOAD_COMMON_RESOURCES);
// Load other resource files.
base::FilePath path;
#if defined(OS_MACOSX)
path = GetResourcesPakFilePath();
#else
base::FilePath pak_dir;
PathService::Get(base::DIR_MODULE, &pak_dir);
path = pak_dir.Append(FILE_PATH_LITERAL("content_shell.pak"));
#endif
ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
bundle.AddDataPackFromPath(path, ui::GetSupportedScaleFactors()[0]);
#if defined(OS_WIN)
bundle.AddDataPackFromPath(
pak_dir.Append(FILE_PATH_LITERAL("ui_resources_200_percent.pak")),
ui::SCALE_FACTOR_200P);
bundle.AddDataPackFromPath(
pak_dir.Append(FILE_PATH_LITERAL("content_resources_200_percent.pak")),
ui::SCALE_FACTOR_200P);
#endif
}
MainDelegate::MainDelegate() {
}
@ -36,27 +95,16 @@ bool MainDelegate::BasicStartupComplete(int* exit_code) {
}
void MainDelegate::PreSandboxStartup() {
#if defined(OS_MACOSX)
l10n_util::OverrideLocaleWithCocoaLocale();
#endif
InitializeResourceBundle();
}
auto cmd = *base::CommandLine::ForCurrentProcess();
std::string process_type = cmd.GetSwitchValueASCII(switches::kProcessType);
void MainDelegate::InitializeResourceBundle() {
base::FilePath path;
#if defined(OS_MACOSX)
path = GetResourcesPakFilePath();
#else
base::FilePath pak_dir;
PathService::Get(base::DIR_MODULE, &pak_dir);
path = pak_dir.Append(FILE_PATH_LITERAL("content_shell.pak"));
#endif
ui::ResourceBundle::InitSharedInstanceWithLocale("", nullptr,
ui::ResourceBundle::DO_NOT_LOAD_COMMON_RESOURCES);
ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
path, ui::GetSupportedScaleFactors()[0]);
AddDataPackFromPath(&ui::ResourceBundle::GetSharedInstance(), path.DirName());
// Initialize ResourceBundle which handles files loaded from external
// sources. The language should have been passed in to us from the
// browser process as a command line flag.
if (SubprocessNeedsResourceBundle(process_type)) {
std::string locale = cmd.GetSwitchValueASCII(switches::kLang);
InitializeResourceBundle(locale);
}
}
content::ContentBrowserClient* MainDelegate::CreateContentBrowserClient() {

View file

@ -22,6 +22,9 @@ namespace brightray {
class BrowserClient;
class ContentClient;
void InitializeResourceBundle(const std::string& locale);
base::FilePath GetResourcesPakFilePath();
class MainDelegate : public content::ContentMainDelegate {
public:
MainDelegate();
@ -36,15 +39,9 @@ class MainDelegate : public content::ContentMainDelegate {
// implementation.
virtual scoped_ptr<BrowserClient> CreateBrowserClient();
// Subclasses can override this to provide additional .pak files to be
// included in the ui::ResourceBundle.
virtual void AddDataPackFromPath(
ui::ResourceBundle* bundle, const base::FilePath& pak_dir) {}
#if defined(OS_MACOSX)
// Subclasses can override this to custom the paths of child process and
// framework bundle.
virtual base::FilePath GetResourcesPakFilePath();
virtual void OverrideChildProcessPath();
virtual void OverrideFrameworkBundlePath();
#endif
@ -55,8 +52,6 @@ class MainDelegate : public content::ContentMainDelegate {
private:
content::ContentBrowserClient* CreateContentBrowserClient() override;
void InitializeResourceBundle();
scoped_ptr<ContentClient> content_client_;
scoped_ptr<BrowserClient> browser_client_;

View file

@ -27,7 +27,7 @@ base::FilePath GetFrameworksPath() {
}
base::FilePath MainDelegate::GetResourcesPakFilePath() {
base::FilePath GetResourcesPakFilePath() {
auto path = [base::mac::FrameworkBundle() pathForResource:@"content_shell" ofType:@"pak"];
return base::mac::NSStringToFilePath(path);
}