Skip to content

Commit

Permalink
Leverage DataPack::IsGzipped() from SharedResourcesDataSource.
Browse files Browse the repository at this point in the history
The data sources that serve chrome://resources URLs were previously
relying on Grit's gzipped_resource_map_source output format. This is
no longer necessary since DataPack::IsGzipped() is exposed via the
content client API (or web client API on iOS).

Also reverting various grd files to use a normal resource_map_source
Grit output instead. The goal is to eventually
remove gzipped_resource_map_source output completely, since it should not be
necessary anymore (but there is more work needed to get there).

Bug: 738243
Change-Id: Idff5f91375417a89107ae4fe9314ab8ccbe50de1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1574631
Reviewed-by: Ken Rockot <rockot@google.com>
Reviewed-by: Nasko Oskov <nasko@chromium.org>
Reviewed-by: Dan Beam <dbeam@chromium.org>
Reviewed-by: Eugene But <eugenebut@chromium.org>
Reviewed-by: Steven Bennetts <stevenjb@chromium.org>
Auto-Submit: Demetrios Papadopoulos <dpapad@chromium.org>
Commit-Queue: Demetrios Papadopoulos <dpapad@chromium.org>
Cr-Commit-Position: refs/heads/master@{#656994}
  • Loading branch information
freshp86 authored and Commit Bot committed May 6, 2019
1 parent f9485f3 commit 516800b
Show file tree
Hide file tree
Showing 16 changed files with 51 additions and 28 deletions.
4 changes: 2 additions & 2 deletions chromeos/resources/chromeos_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<emit emit_type='prepend'></emit>
</output>
<output filename="grit/chromeos_resources_map.cc"
type="gzipped_resource_map_source" />
type="resource_map_source" />
<output filename="grit/chromeos_resources_map.h"
type="gzipped_resource_map_header" />
type="resource_map_header" />
<output filename="chromeos_resources.pak" type="data_package" />
</outputs>
<release seq="1">
Expand Down
24 changes: 8 additions & 16 deletions content/browser/webui/shared_resources_data_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ namespace content {

namespace {

struct IdrGzipped {
int idr;
bool gzipped;
};
using ResourcesMap = std::unordered_map<std::string, IdrGzipped>;
using ResourcesMap = std::unordered_map<std::string, int>;

#if defined(OS_CHROMEOS)
const char kPolymerHtml[] = "polymer/v1_0/polymer/polymer.html";
Expand Down Expand Up @@ -191,10 +187,8 @@ bool ShouldIgnore(std::string resource) {

void AddResource(const std::string& path,
int resource_id,
bool gzipped,
ResourcesMap* resources_map) {
IdrGzipped idr_gzipped = {resource_id, gzipped};
if (!resources_map->insert(std::make_pair(path, idr_gzipped)).second)
if (!resources_map->insert(std::make_pair(path, resource_id)).second)
NOTREACHED() << "Redefinition of '" << path << "'";
}

Expand All @@ -210,14 +204,14 @@ void AddResourcesToMap(ResourcesMap* resources_map) {
continue;
#endif // !defined(OS_ANDROID)

AddResource(resource.name, resource.value, resource.gzipped, resources_map);
AddResource(resource.name, resource.value, resources_map);

for (auto it = aliases.begin(); it != aliases.end(); ++it) {
if (base::StartsWith(resource.name, it->first,
base::CompareCase::SENSITIVE)) {
std::string resource_name(resource.name);
AddResource(it->second + resource_name.substr(it->first.length()),
resource.value, resource.gzipped, resources_map);
resource.value, resources_map);
}
}
}
Expand All @@ -228,7 +222,7 @@ void AddResourcesToMap(ResourcesMap* resources_map) {
// alias. Note that resources which do not have an alias will not be added.
void AddAliasedResourcesToMap(
const std::map<int, std::string>& resource_aliases,
const GzippedGritResourceMap resources[],
const GritResourceMap resources[],
size_t resources_size,
ResourcesMap* resources_map) {
for (size_t i = 0; i < resources_size; ++i) {
Expand All @@ -238,7 +232,7 @@ void AddAliasedResourcesToMap(
if (it == resource_aliases.end())
continue;

AddResource(it->second, resource.value, resource.gzipped, resources_map);
AddResource(it->second, resource.value, resources_map);
}
}

Expand Down Expand Up @@ -266,7 +260,7 @@ const ResourcesMap& GetResourcesMap() {
int GetIdrForPath(const std::string& path) {
const ResourcesMap& resources_map = GetResourcesMap();
auto it = resources_map.find(path);
return it != resources_map.end() ? it->second.idr : -1;
return it != resources_map.end() ? it->second : -1;
}

} // namespace
Expand Down Expand Up @@ -409,9 +403,7 @@ SharedResourcesDataSource::GetAccessControlAllowOriginForOrigin(
}

bool SharedResourcesDataSource::IsGzipped(const std::string& path) const {
auto it = GetResourcesMap().find(path);
DCHECK(it != GetResourcesMap().end()) << "missing shared resource: " << path;
return it != GetResourcesMap().end() ? it->second.gzipped : false;
return GetContentClient()->IsDataResourceGzipped(GetIdrForPath(path));
}

#if defined(OS_CHROMEOS)
Expand Down
4 changes: 2 additions & 2 deletions content/content_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
<emit emit_type='prepend'></emit>
</output>
<output filename="grit/content_resources_map.h"
type="gzipped_resource_map_header" />
type="resource_map_header" />
<output filename="grit/content_resources_map.cc"
type="gzipped_resource_map_source" />
type="resource_map_source" />
<output filename="content_resources.pak" type="data_package" />
</outputs>
<translations />
Expand Down
1 change: 1 addition & 0 deletions ios/chrome/browser/web/chrome_web_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class ChromeWebClient : public web::WebClient {
int resource_id,
ui::ScaleFactor scale_factor) const override;
base::RefCountedMemory* GetDataResourceBytes(int resource_id) const override;
bool IsDataResourceGzipped(int resource_id) const override;
base::Optional<service_manager::Manifest> GetServiceManifestOverlay(
base::StringPiece name) override;
void GetAdditionalWebUISchemes(
Expand Down
4 changes: 4 additions & 0 deletions ios/chrome/browser/web/chrome_web_client.mm
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@
resource_id);
}

bool ChromeWebClient::IsDataResourceGzipped(int resource_id) const {
return ui::ResourceBundle::GetSharedInstance().IsGzipped(resource_id);
}

base::Optional<service_manager::Manifest>
ChromeWebClient::GetServiceManifestOverlay(base::StringPiece name) {
if (name == web::mojom::kBrowserServiceName)
Expand Down
2 changes: 2 additions & 0 deletions ios/web/public/test/fakes/test_web_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class TestWebClient : public web::WebClient {
base::string16 GetPluginNotSupportedText() const override;

base::RefCountedMemory* GetDataResourceBytes(int id) const override;
bool IsDataResourceGzipped(int resource_id) const override;

NSString* GetDocumentStartScriptForMainFrame(
BrowserState* browser_state) const override;
void AllowCertificateError(WebState*,
Expand Down
6 changes: 6 additions & 0 deletions ios/web/public/test/fakes/test_web_client.mm
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@
resource_id);
}

bool TestWebClient::IsDataResourceGzipped(int resource_id) const {
if (!ui::ResourceBundle::HasSharedInstance())
return false;
return ui::ResourceBundle::GetSharedInstance().IsGzipped(resource_id);
}

NSString* TestWebClient::GetDocumentStartScriptForMainFrame(
BrowserState* browser_state) const {
return early_page_script_ ? early_page_script_ : @"";
Expand Down
3 changes: 3 additions & 0 deletions ios/web/public/web_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ class WebClient {
// Returns the raw bytes of a scale independent data resource.
virtual base::RefCountedMemory* GetDataResourceBytes(int resource_id) const;

// Returns whether the contents of a resource are compressed (with gzip).
virtual bool IsDataResourceGzipped(int resource_id) const;

// Returns a list of additional WebUI schemes, if any. These additional
// schemes act as aliases to the about: scheme. The additional schemes may or
// may not serve specific WebUI pages depending on the particular
Expand Down
1 change: 1 addition & 0 deletions ios/web/shell/shell_web_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class ShellWebClient : public WebClient {
int resource_id,
ui::ScaleFactor scale_factor) const override;
base::RefCountedMemory* GetDataResourceBytes(int resource_id) const override;
bool IsDataResourceGzipped(int resource_id) const override;
std::unique_ptr<service_manager::Service> HandleServiceRequest(
const std::string& service_name,
service_manager::mojom::ServiceRequest request) override;
Expand Down
4 changes: 4 additions & 0 deletions ios/web/shell/shell_web_client.mm
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ void SetWebUsageEnabled(bool enabled,
resource_id);
}

bool ShellWebClient::IsDataResourceGzipped(int resource_id) const {
return ui::ResourceBundle::GetSharedInstance().IsGzipped(resource_id);
}

std::unique_ptr<service_manager::Service> ShellWebClient::HandleServiceRequest(
const std::string& service_name,
service_manager::mojom::ServiceRequest request) {
Expand Down
4 changes: 4 additions & 0 deletions ios/web/web_client.mm
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ void SetWebClient(WebClient* client) {
return nullptr;
}

bool WebClient::IsDataResourceGzipped(int resource_id) const {
return false;
}

NSString* WebClient::GetDocumentStartScriptForAllFrames(
BrowserState* browser_state) const {
return @"";
Expand Down
9 changes: 5 additions & 4 deletions ios/web/webui/shared_resources_data_source_ios.mm
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

// Maps a path name (i.e. "/js/path.js") to a resource map entry. Returns
// nullptr if not found.
const GzippedGritResourceMap* PathToResource(const std::string& path) {
const GritResourceMap* PathToResource(const std::string& path) {
for (size_t i = 0; i < kWebuiResourcesSize; ++i) {
if (path == kWebuiResources[i].name)
return &kWebuiResources[i];
Expand All @@ -50,7 +50,7 @@
void SharedResourcesDataSourceIOS::StartDataRequest(
const std::string& path,
const URLDataSourceIOS::GotDataCallback& callback) {
const GzippedGritResourceMap* resource = PathToResource(path);
const GritResourceMap* resource = PathToResource(path);
DCHECK(resource) << " path: " << path;
scoped_refptr<base::RefCountedMemory> bytes;

Expand All @@ -75,8 +75,9 @@
}

bool SharedResourcesDataSourceIOS::IsGzipped(const std::string& path) const {
const GzippedGritResourceMap* resource = PathToResource(path);
return resource && resource->gzipped;
const GritResourceMap* resource = PathToResource(path);
int idr = resource ? resource->value : -1;
return idr == -1 ? false : GetWebClient()->IsDataResourceGzipped(idr);
}

} // namespace web
1 change: 1 addition & 0 deletions ios/web_view/internal/web_view_web_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class WebViewWebClient : public web::WebClient {
int resource_id,
ui::ScaleFactor scale_factor) const override;
base::RefCountedMemory* GetDataResourceBytes(int resource_id) const override;
bool IsDataResourceGzipped(int resource_id) const override;
NSString* GetDocumentStartScriptForMainFrame(
web::BrowserState* browser_state) const override;
base::string16 GetPluginNotSupportedText() const override;
Expand Down
4 changes: 4 additions & 0 deletions ios/web_view/internal/web_view_web_client.mm
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@
resource_id);
}

bool WebViewWebClient::IsDataResourceGzipped(int resource_id) const {
return ui::ResourceBundle::GetSharedInstance().IsGzipped(resource_id);
}

NSString* WebViewWebClient::GetDocumentStartScriptForMainFrame(
web::BrowserState* browser_state) const {
NSMutableArray* scripts = [NSMutableArray array];
Expand Down
4 changes: 2 additions & 2 deletions mojo/public/js/mojo_bindings_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<emit emit_type='prepend'></emit>
</output>
<output filename="grit/mojo_bindings_resources_map.h"
type="gzipped_resource_map_header" />
type="resource_map_header" />
<output filename="grit/mojo_bindings_resources_map.cc"
type="gzipped_resource_map_source" />
type="resource_map_source" />
<output filename="mojo_bindings_resources.pak" type="data_package" />
</outputs>
<translations />
Expand Down
4 changes: 2 additions & 2 deletions ui/webui/resources/webui_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ without changes to the corresponding grd file. -->
<emit emit_type='prepend'></emit>
</output>
<output filename="grit/webui_resources_map.cc"
type="gzipped_resource_file_map_source" />
<output filename="grit/webui_resources_map.h" type="gzipped_resource_map_header" />
type="resource_file_map_source" />
<output filename="grit/webui_resources_map.h" type="resource_map_header" />
<output filename="webui_resources.pak" type="data_package" />
</outputs>
<release seq="1">
Expand Down

0 comments on commit 516800b

Please sign in to comment.