- Add support for loading localized strings from locale .pak files (issue #357).

- Add support for loading DevTools resources from chrome.pak via the chrome-devtools scheme (issue #358).
- Add tools_gyp.patch to fix a bug in GYP.

git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@295 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
This commit is contained in:
Marshall Greenblatt
2011-09-30 09:22:54 +00:00
parent ebc4feb095
commit 9842abc0ab
16 changed files with 575 additions and 135 deletions

View File

@ -0,0 +1,125 @@
// Copyright (c) 2011 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that can
// be found in the LICENSE file.
#include "browser_devtools_scheme_handler.h"
#include "include/cef.h"
#include <string>
#include "base/file_util.h"
#include "base/string_util.h"
#include "grit/devtools_resources_map.h"
#include "net/base/mime_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "webkit/glue/webkit_glue.h"
const char kChromeDevToolsScheme[] = "chrome-devtools";
const char kChromeDevToolsHost[] = "devtools";
const char kChromeDevToolsURL[] = "chrome-devtools://devtools/";
namespace {
class DevToolsSchemeHandler : public CefSchemeHandler
{
public:
DevToolsSchemeHandler(const std::string& path,
CefRefPtr<CefStreamReader> reader,
int size)
: path_(path), reader_(reader), size_(size)
{
}
virtual bool ProcessRequest(CefRefPtr<CefRequest> request,
CefString& redirectUrl,
CefRefPtr<CefSchemeHandlerCallback> callback)
OVERRIDE
{
callback->HeadersAvailable();
return true;
}
virtual void GetResponseHeaders(CefRefPtr<CefResponse> response,
int64& response_length) OVERRIDE
{
response_length = size_;
std::string mime_type = "text/plain";
if(net::GetMimeTypeFromFile(FilePath(CefString(path_)), &mime_type))
response->SetMimeType(mime_type);
response->SetStatus(200);
}
virtual bool ReadResponse(void* data_out,
int bytes_to_read,
int& bytes_read,
CefRefPtr<CefSchemeHandlerCallback> callback)
OVERRIDE
{
bytes_read = reader_->Read(data_out, 1, bytes_to_read);
return (bytes_read > 0);
}
virtual void Cancel() OVERRIDE
{
}
private:
std::string path_;
CefRefPtr<CefStreamReader> reader_;
int size_;
IMPLEMENT_REFCOUNTING(DevToolSSchemeHandler);
};
class DevToolsSchemeHandlerFactory : public CefSchemeHandlerFactory
{
public:
DevToolsSchemeHandlerFactory() {}
virtual CefRefPtr<CefSchemeHandler> Create(const CefString& scheme_name,
CefRefPtr<CefRequest> request)
OVERRIDE
{
std::string url = request->GetURL();
const char* path = &url.c_str()[strlen(kChromeDevToolsURL)];
int size = -1;
CefRefPtr<CefStreamReader> reader = GetStreamReader(path, size);
if (!reader.get())
return NULL;
return new DevToolsSchemeHandler(path, reader, size);
}
CefRefPtr<CefStreamReader> GetStreamReader(const char* path, int& size)
{
// Create a stream for the grit resource.
for(size_t i = 0; i < kDevtoolsResourcesSize; ++i) {
if (base::strcasecmp(kDevtoolsResources[i].name, path) == 0) {
base::StringPiece piece =
webkit_glue::GetDataResource(kDevtoolsResources[i].value);
if (!piece.empty()) {
size = piece.size();
return CefStreamReader::CreateForData(const_cast<char*>(piece.data()),
size);
}
}
}
NOTREACHED() << "Missing DevTools resource: "<<path;
return NULL;
}
IMPLEMENT_REFCOUNTING(DevToolSSchemeHandlerFactory);
};
} // namespace
// Register the DevTools scheme handler.
void RegisterDevToolsSchemeHandler()
{
CefRegisterCustomScheme(kChromeDevToolsScheme, true, false, true);
CefRegisterSchemeHandlerFactory(kChromeDevToolsScheme, kChromeDevToolsHost,
new DevToolsSchemeHandlerFactory());
}

View File

@ -0,0 +1,15 @@
// Copyright (c) 2011 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that can
// be found in the LICENSE file.
#ifndef _CEF_BROWSER_DEVTOOLS_SCHEME_HANDLER_H
#define _CEF_BROWSER_DEVTOOLS_SCHEME_HANDLER_H
extern const char kChromeDevToolsScheme[];
extern const char kChromeDevToolsHost[];
extern const char kChromeDevToolsURL[];
// Register the DevTools scheme handler.
void RegisterDevToolsSchemeHandler();
#endif // _CEF_BROWSER_DEVTOOLS_SCHEME_HANDLER_H

View File

@ -3,10 +3,11 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "cef_context.h"
#include "browser_devtools_scheme_handler.h"
#include "browser_impl.h"
#include "browser_webkit_glue.h"
#include "browser_zoom_map.h"
#include "cef_context.h"
#include "dom_document_impl.h"
#include "request_impl.h"
#include "stream_impl.h"
@ -1412,14 +1413,7 @@ void CefBrowserImpl::UIT_ShowDevTools()
BrowserDevToolsClient* client = dev_tools_agent_->client();
if (!client) {
// Create the inspector window.
FilePath dir_exe;
PathService::Get(base::DIR_EXE, &dir_exe);
FilePath devtools_path =
dir_exe.AppendASCII("resources/inspector/devtools.html");
// Mac requires that the URL have a file:// prefix.
CefString path = devtools_path.value();
CefString url(base::StringPrintf("file://%s", path.ToString().c_str()));
CefString url(base::StringPrintf("%sdevtools.html", kChromeDevToolsURL));
CefPopupFeatures features;
CefRefPtr<CefBrowserImpl> browser = UIT_CreatePopupWindow(url, features);

View File

@ -19,6 +19,7 @@ MSVC_POP_WARNING();
#include "browser_webkit_glue.h"
#include "cef_context.h"
#include "base/file_util.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/path_service.h"
@ -27,6 +28,7 @@ MSVC_POP_WARNING();
#include "net/base/mime_util.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h"
#include "ui/base/resource/resource_bundle.h"
#include "webkit/glue/user_agent.h"
#include "webkit/glue/webkit_glue.h"
#include "webkit/plugins/npapi/plugin_list.h"
@ -38,6 +40,28 @@ using WebKit::WebFrameImpl;
namespace webkit_glue {
void InitializeDataPak(const std::string& locale) {
// Load chrome.pak (on Mac) and the appropiate locale pack.
const std::string loaded_locale =
ResourceBundle::InitSharedInstance(locale);
CHECK(!loaded_locale.empty()) << "Locale could not be found for " << locale;
#if defined(OS_WIN)
// Explicitly load chrome.pak on Windows.
FilePath chrome_pack_path;
PathService::Get(base::DIR_EXE, &chrome_pack_path);
chrome_pack_path = chrome_pack_path.AppendASCII("chrome.pak");
if (file_util::PathExists(chrome_pack_path))
ResourceBundle::AddDataPackToSharedInstance(chrome_pack_path);
else
NOTREACHED() << "Could not load chrome.pak";
#endif
}
string16 GetLocalizedString(int message_id) {
return ResourceBundle::GetSharedInstance().GetLocalizedString(message_id);
}
bool IsMediaPlayerAvailable() {
return true;
}

View File

@ -2,6 +2,8 @@
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
#include "include/cef.h"
#if defined(OS_WIN)
#include <windows.h>
#endif
@ -33,17 +35,15 @@ void CaptureWebViewBitmap(HWND mainWnd, WebKit::WebView* webview,
BOOL SaveBitmapToFile(HBITMAP hBmp, HDC hDC, LPCTSTR file, LPBYTE lpBits);
#endif
void InitializeDataPak(const std::string& locale);
#if defined(OS_MACOSX)
void InitializeDataPak();
FilePath GetResourcesFilePath();
#endif
// Text encoding objects must be initialized on the main thread.
void InitializeTextEncoding();
// This is called indirectly by the network layer to access resources.
base::StringPiece NetResourceProvider(int key);
// Retrieve the V8 context associated with the frame.
v8::Handle<v8::Context> GetV8Context(WebKit::WebFrame* frame);

View File

@ -16,25 +16,12 @@
#include "base/path_service.h"
#include "base/utf_string_conversions.h"
#include "grit/webkit_resources.h"
#include "ui/base/resource/data_pack.h"
#include "ui/base/resource/resource_bundle.h"
#include "webkit/glue/webkit_glue.h"
namespace webkit_glue {
// Data pack resource. This is a pointer to the mmapped resources file.
static ui::DataPack* g_resource_data_pack = NULL;
void InitializeDataPak() {
// mmap the data pack which holds strings used by WebCore.
// TODO(port): Allow the embedder to customize the pak name.
g_resource_data_pack = new ui::DataPack;
NSString *resource_path =
[base::mac::MainAppBundle() pathForResource:@"cefclient" ofType:@"pak"];
FilePath resources_pak_path([resource_path fileSystemRepresentation]);
if (!g_resource_data_pack->Load(resources_pak_path))
LOG(FATAL) << "failed to load cefclient.pak";
}
// Helper method for getting the path to the CEF resources directory.
FilePath GetResourcesFilePath() {
FilePath path;
@ -53,37 +40,6 @@ FilePath GetResourcesFilePath() {
return path.AppendASCII("res");
}
}
string16 GetLocalizedString(int message_id) {
base::StringPiece res;
if (!g_resource_data_pack->GetStringPiece(message_id, &res)) {
LOG(FATAL) << "failed to load webkit string with id " << message_id;
}
// Data packs hold strings as either UTF8 or UTF16.
string16 msg;
switch (g_resource_data_pack->GetTextEncodingType()) {
case ui::DataPack::UTF8:
msg = UTF8ToUTF16(res);
break;
case ui::DataPack::UTF16:
msg = string16(reinterpret_cast<const char16*>(res.data()),
res.length() / 2);
break;
case ui::DataPack::BINARY:
default:
break;
}
return msg;
}
base::StringPiece NetResourceProvider(int key) {
base::StringPiece res;
g_resource_data_pack->GetStringPiece(key, &res);
return res;
}
base::StringPiece GetDataResource(int resource_id) {
switch (resource_id) {
@ -116,29 +72,15 @@ base::StringPiece GetDataResource(int resource_id) {
}
return resize_corner_data;
}
case IDR_SEARCH_CANCEL:
case IDR_SEARCH_CANCEL_PRESSED:
case IDR_SEARCH_MAGNIFIER:
case IDR_SEARCH_MAGNIFIER_RESULTS:
case IDR_MEDIA_PAUSE_BUTTON:
case IDR_MEDIA_PLAY_BUTTON:
case IDR_MEDIA_PLAY_BUTTON_DISABLED:
case IDR_MEDIA_SOUND_FULL_BUTTON:
case IDR_MEDIA_SOUND_NONE_BUTTON:
case IDR_MEDIA_SOUND_DISABLED:
case IDR_MEDIA_SLIDER_THUMB:
case IDR_MEDIA_VOLUME_SLIDER_THUMB:
case IDR_INPUT_SPEECH:
case IDR_INPUT_SPEECH_RECORDING:
case IDR_INPUT_SPEECH_WAITING:
return NetResourceProvider(resource_id);
default:
break;
}
return base::StringPiece();
base::StringPiece piece =
ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id);
DCHECK(!piece.empty());
return piece;
}
void DidLoadPlugin(const std::string& filename) {

View File

@ -23,6 +23,7 @@ MSVC_POP_WARNING();
#include "third_party/WebKit/Source/WebKit/chromium/public/WebRect.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/gdi_util.h"
#include "webkit/glue/webkit_glue.h"
@ -32,19 +33,6 @@ using WebKit::WebView;
namespace webkit_glue {
string16 GetLocalizedString(int message_id) {
// Localized resources are provided via webkit_resources.rc and
// webkit_strings_en-US.rc.
const ATLSTRINGRESOURCEIMAGE* image =
AtlGetStringResourceImage(_AtlBaseModule.GetModuleInstance(),
message_id);
if (!image) {
NOTREACHED();
return L"No string for this identifier!";
}
return string16(image->achString, image->nLength);
}
base::StringPiece GetRawDataResource(HMODULE module, int resource_id) {
void* data_ptr;
size_t data_size;
@ -54,15 +42,21 @@ base::StringPiece GetRawDataResource(HMODULE module, int resource_id) {
: base::StringPiece();
}
base::StringPiece NetResourceProvider(int key) {
base::StringPiece GetDataResource(int resource_id) {
base::StringPiece piece;
// Try to load the resource from the DLL.
HMODULE hModule = ::GetModuleHandle(L"libcef.dll");
if(!hModule)
hModule = ::GetModuleHandle(NULL);
return GetRawDataResource(hModule, key);
}
piece = GetRawDataResource(hModule, resource_id);
base::StringPiece GetDataResource(int resource_id) {
return NetResourceProvider(resource_id);
// Try to load the resource from the resource pack.
if (piece.empty())
piece = ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id);
DCHECK(!piece.empty()) << "Resource "<<resource_id<<" could not be loaded";
return piece;
}
bool EnsureFontLoaded(HFONT font) {

View File

@ -8,15 +8,18 @@
#include "browser_webkit_init.h"
#include "cef_context.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/i18n/icu_util.h"
#include "base/metrics/stats_table.h"
#include "base/rand_util.h"
#include "base/string_number_conversions.h"
#include "browser_devtools_scheme_handler.h"
#include "build/build_config.h"
#include "net/base/net_module.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebNetworkStateNotifier.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebScriptController.h"
#include "ui/base/ui_base_paths.h"
#include "ui/gfx/gl/gl_implementation.h"
#include "webkit/extensions/v8/gc_extension.h"
#include "webkit/glue/webkit_glue.h"
@ -45,8 +48,6 @@ CefProcessUIThread::~CefProcessUIThread() {
}
void CefProcessUIThread::Init() {
PlatformInit();
// Initialize the global CommandLine object.
CommandLine::Init(0, NULL);
@ -70,24 +71,34 @@ void CefProcessUIThread::Init() {
logging::DONT_LOCK_LOG_FILE, logging::APPEND_TO_OLD_LOG_FILE,
logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS);
// Load ICU data tables.
bool ret = icu_util::Initialize();
if(!ret) {
#if defined(OS_WIN)
MessageBox(NULL, L"Failed to load the required icudt library",
L"CEF Initialization Error", MB_ICONERROR | MB_OK);
#endif
return;
}
// Provides path resolution required for locating locale pack files.
ui::RegisterPathProvider();
std::string localeStr = CefString(&settings.locale);
if (localeStr.empty())
localeStr = "en-US";
webkit_glue::InitializeDataPak(localeStr);
PlatformInit();
// Initialize WebKit.
webkit_init_ = new BrowserWebKitInit();
// Initialize WebKit encodings
webkit_glue::InitializeTextEncoding();
// Load ICU data tables.
bool ret = icu_util::Initialize();
if(!ret) {
#if defined(OS_WIN)
MessageBox(NULL, L"Failed to load the required icudt38 library",
L"CEF Initialization Error", MB_ICONERROR | MB_OK);
#endif
return;
}
// Config the network module so it has access to a limited set of resources.
net::NetModule::SetResourceProvider(webkit_glue::NetResourceProvider);
net::NetModule::SetResourceProvider(webkit_glue::GetDataResource);
// Load and initialize the stats table. Attempt to construct a somewhat
// unique name to isolate separate instances from each other.
@ -145,6 +156,10 @@ void CefProcessUIThread::Init() {
// Initialize WebKit with the current state.
WebKit::WebNetworkStateNotifier::setOnLine(
!net::NetworkChangeNotifier::IsOffline());
// Perform DevTools scheme registration when CEF initialization is complete.
CefThread::PostTask(CefThread::UI, FROM_HERE,
base::Bind(&RegisterDevToolsSchemeHandler));
}
void CefProcessUIThread::CleanUp() {

View File

@ -9,6 +9,7 @@
#import "include/cef_application_mac.h"
#include "cef_process_ui_thread.h"
#include "browser_webkit_glue.h"
#include "cef_context.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
#include "third_party/WebKit/Source/WebKit/mac/WebCoreSupport/WebSystemInterface.h"
@ -19,8 +20,6 @@ void CefProcessUIThread::PlatformInit() {
InitWebCoreSystemInterface();
webkit_glue::InitializeDataPak();
// On Mac, the select popup menus are rendered by the browser.
WebKit::WebView::setUseExternalPopupMenus(true);
}