From 044f5a95bef0ef8a672a9ce163d7a45bbc96dc5d Mon Sep 17 00:00:00 2001 From: "erg@chromium.org" Date: Fri, 18 Jun 2010 17:36:39 +0000 Subject: Reland r50057. This is exactly the same except with two lines cherrypicked from tony's CL 2814009 that tell make about the net_internals_resources_map.{cc,h} files. Load net-internals from the newly created resources.pak file. This patch also forces the loading of the optional DataPack for const correctness reasons Original Review: http://codereview.chromium.org/2855003 BUG=35793,42770 TEST=none TBR=tony Review URL: http://codereview.chromium.org/2800014 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@50257 0039d316-1c4b-4281-b951-d872f2087c98 --- app/resource_bundle.cc | 28 +++++++---- app/resource_bundle.h | 5 +- base/data_pack.cc | 4 +- base/data_pack.h | 4 +- chrome/browser/dom_ui/net_internals_ui.cc | 78 +++++++++++++++++-------------- chrome/chrome.gyp | 2 + chrome/chrome_browser.gypi | 37 +-------------- chrome/common/chrome_paths.cc | 5 -- chrome/common/chrome_paths.h | 1 - 9 files changed, 73 insertions(+), 91 deletions(-) diff --git a/app/resource_bundle.cc b/app/resource_bundle.cc index dcf7fa8..d61e83d 100644 --- a/app/resource_bundle.cc +++ b/app/resource_bundle.cc @@ -105,7 +105,17 @@ SkBitmap* ResourceBundle::LoadBitmap(DataHandle data_handle, int resource_id) { RefCountedStaticMemory* ResourceBundle::LoadDataResourceBytes( int resource_id) const { - return LoadResourceBytes(resources_data_, resource_id); + RefCountedStaticMemory* bytes = + LoadResourceBytes(resources_data_, resource_id); + + // Check all our additional data packs for the resources if it wasn't loaded + // from our main source. + for (std::vector::const_iterator it = data_packs_.begin(); + !bytes && it != data_packs_.end(); ++it) { + bytes = (*it)->GetStaticMemory(resource_id); + } + + return bytes; } SkBitmap* ResourceBundle::GetBitmapNamed(int resource_id) { @@ -198,11 +208,8 @@ const gfx::Font& ResourceBundle::GetFont(FontStyle style) { // 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) + // Always preload the data packs so we can maintain constness. Load(); -#endif } void ResourceBundle::LoadedDataPack::Load() { @@ -212,9 +219,12 @@ void ResourceBundle::LoadedDataPack::Load() { CHECK(success) << "Failed to load " << path_.value(); } -bool ResourceBundle::LoadedDataPack::GetStringPiece(int resource_id, - base::StringPiece* data) { - if (!data_pack_.get()) - Load(); +bool ResourceBundle::LoadedDataPack::GetStringPiece( + int resource_id, base::StringPiece* data) const { return data_pack_->GetStringPiece(static_cast(resource_id), data); } + +RefCountedStaticMemory* ResourceBundle::LoadedDataPack::GetStaticMemory( + int resource_id) const { + return data_pack_->GetStaticMemory(resource_id); +} diff --git a/app/resource_bundle.h b/app/resource_bundle.h index 3a3e617..1d328a3 100644 --- a/app/resource_bundle.h +++ b/app/resource_bundle.h @@ -159,8 +159,9 @@ class ResourceBundle { // Helper class for managing data packs. class LoadedDataPack { public: - LoadedDataPack(const FilePath& path); - bool GetStringPiece(int resource_id, base::StringPiece* data); + explicit LoadedDataPack(const FilePath& path); + bool GetStringPiece(int resource_id, base::StringPiece* data) const; + RefCountedStaticMemory* GetStaticMemory(int resource_id) const; private: void Load(); diff --git a/base/data_pack.cc b/base/data_pack.cc index 4e5a569..06f2308 100644 --- a/base/data_pack.cc +++ b/base/data_pack.cc @@ -96,7 +96,7 @@ bool DataPack::Load(const FilePath& path) { return true; } -bool DataPack::GetStringPiece(uint32 resource_id, StringPiece* data) { +bool DataPack::GetStringPiece(uint32 resource_id, StringPiece* data) const { // It won't be hard to make this endian-agnostic, but it's not worth // bothering to do right now. #if defined(__BYTE_ORDER) @@ -119,7 +119,7 @@ bool DataPack::GetStringPiece(uint32 resource_id, StringPiece* data) { return true; } -RefCountedStaticMemory* DataPack::GetStaticMemory(uint32 resource_id) { +RefCountedStaticMemory* DataPack::GetStaticMemory(uint32 resource_id) const { base::StringPiece piece; if (!GetStringPiece(resource_id, &piece)) return NULL; diff --git a/base/data_pack.h b/base/data_pack.h index 8938491..f3f8481 100644 --- a/base/data_pack.h +++ b/base/data_pack.h @@ -35,12 +35,12 @@ class DataPack { // Get resource by id |resource_id|, filling in |data|. // The data is owned by the DataPack object and should not be modified. // Returns false if the resource id isn't found. - bool GetStringPiece(uint32 resource_id, StringPiece* data); + bool GetStringPiece(uint32 resource_id, StringPiece* data) const; // Like GetStringPiece(), but returns a reference to memory. This interface // is used for image data, while the StringPiece interface is usually used // for localization strings. - RefCountedStaticMemory* GetStaticMemory(uint32 resource_id); + RefCountedStaticMemory* GetStaticMemory(uint32 resource_id) const; // Writes a pack file containing |resources| to |path|. static bool WritePack(const FilePath& path, diff --git a/chrome/browser/dom_ui/net_internals_ui.cc b/chrome/browser/dom_ui/net_internals_ui.cc index 553d197..96ee59e 100644 --- a/chrome/browser/dom_ui/net_internals_ui.cc +++ b/chrome/browser/dom_ui/net_internals_ui.cc @@ -36,6 +36,8 @@ #include "chrome/common/net/url_request_context_getter.h" #include "chrome/common/url_constants.h" #include "grit/generated_resources.h" +#include "grit/net_internals_resources.h" +#include "grit/net_internals_resources_map.h" #include "net/base/escape.h" #include "net/base/host_resolver_impl.h" #include "net/base/net_errors.h" @@ -316,51 +318,57 @@ NetInternalsHTMLSource::NetInternalsHTMLSource() void NetInternalsHTMLSource::StartDataRequest(const std::string& path, bool is_off_the_record, int request_id) { - // The provided |path| identifies a file in resources/net_internals/. - std::string data_string; - FilePath file_path; - PathService::Get(chrome::DIR_NET_INTERNALS, &file_path); - std::string filename; - // The provided "path" may contain a fragment, or query section. We only // care about the path itself, and will disregard anything else. - filename = GURL(std::string("chrome://net/") + path).path().substr(1); - + std::string filename = + GURL(std::string("chrome://net/") + path).path().substr(1); if (filename.empty()) filename = "index.html"; - file_path = file_path.AppendASCII(filename); - - if (!file_util::ReadFileToString(file_path, &data_string)) { - LOG(WARNING) << "Could not read resource: " << file_path.value(); - data_string = StringPrintf("

Failed to read file " - "RESOURCES/net_internals/%s

", - EscapeForHTML(filename).c_str()); - - // During the transition from old implementation to new implementation, - // users may be entering the URLs for the old frontend. - data_string.append( - "

Note that the URL scheme for net-internals has changed because of " - "its new implementation (bug 37421):

" - ""); + // The name of the files in the grd list are prefixed with the following + // directory: + std::string key("net_internals/"); + key += filename; + + const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); + for (size_t i = 0; i < kNetInternalsResourcesSize; ++i) { + if (kNetInternalsResources[i].name == key) { + scoped_refptr bytes( + rb.LoadDataResourceBytes(kNetInternalsResources[i].value)); + if (bytes && bytes->front()) { + SendResponse(request_id, bytes); + return; + } + } } + LOG(WARNING) << "Could not read resource: " << path; + std::string data_string = "

Failed to read resource"; + data_string.append(EscapeForHTML(filename)); + data_string.append("

"); + + // During the transition from old implementation to new implementation, + // users may be entering the URLs for the old frontend. + data_string.append( + "

Note that the URL scheme for net-internals has changed because of " + "its new implementation (bug 37421):

" + ""); + scoped_refptr bytes(new RefCountedBytes); bytes->data.resize(data_string.size()); std::copy(data_string.begin(), data_string.end(), bytes->data.begin()); - SendResponse(request_id, bytes); } diff --git a/chrome/chrome.gyp b/chrome/chrome.gyp index 671e9cf..354dc10 100644 --- a/chrome/chrome.gyp +++ b/chrome/chrome.gyp @@ -448,6 +448,8 @@ 'outputs': [ '<(grit_out_dir)/grit/<(RULE_INPUT_ROOT).h', '<(grit_out_dir)/<(RULE_INPUT_ROOT).pak', + '<(grit_out_dir)/grit/<(RULE_INPUT_ROOT)_map.cc', + '<(grit_out_dir)/grit/<(RULE_INPUT_ROOT)_map.h', # TODO(bradnelson): move to something like this instead #'