This source file includes following definitions.
- AppsRelativePathMatch
- PathToIDR
- GetSource
- StartDataRequest
- GetMimeType
#include "content/browser/webui/shared_resources_data_source.h"
#include "base/logging.h"
#include "base/memory/ref_counted_memory.h"
#include "base/strings/string_util.h"
#include "base/threading/thread_restrictions.h"
#include "content/public/common/content_client.h"
#include "content/public/common/url_constants.h"
#include "grit/webui_resources_map.h"
#include "net/base/mime_util.h"
namespace {
const char kAppImagesPath[] = "images/apps/";
const char kAppImagesPath2x[] = "images/2x/apps/";
const char kReplacement[] = "../../resources/default_100_percent/common/";
const char kReplacement2x[] = "../../resources/default_200_percent/common/";
bool AppsRelativePathMatch(const std::string& path,
const std::string& compareto) {
if (StartsWithASCII(path, kAppImagesPath, false)) {
if (compareto ==
(kReplacement + path.substr(arraysize(kAppImagesPath) - 1)))
return true;
} else if (StartsWithASCII(path, kAppImagesPath2x, false)) {
if (compareto ==
(kReplacement2x + path.substr(arraysize(kAppImagesPath2x) - 1)))
return true;
}
return false;
}
int PathToIDR(const std::string& path) {
int idr = -1;
for (size_t i = 0; i < kWebuiResourcesSize; ++i) {
if ((path == kWebuiResources[i].name) ||
AppsRelativePathMatch(path, kWebuiResources[i].name)) {
idr = kWebuiResources[i].value;
break;
}
}
return idr;
}
}
SharedResourcesDataSource::SharedResourcesDataSource() {
}
SharedResourcesDataSource::~SharedResourcesDataSource() {
}
std::string SharedResourcesDataSource::GetSource() const {
return content::kChromeUIResourcesHost;
}
void SharedResourcesDataSource::StartDataRequest(
const std::string& path,
int render_process_id,
int render_frame_id,
const content::URLDataSource::GotDataCallback& callback) {
int idr = PathToIDR(path);
DCHECK_NE(-1, idr) << " path: " << path;
scoped_refptr<base::RefCountedStaticMemory> bytes(
content::GetContentClient()->GetDataResourceBytes(idr));
callback.Run(bytes.get());
}
std::string SharedResourcesDataSource::GetMimeType(
const std::string& path) const {
base::ThreadRestrictions::ScopedAllowIO allow_io;
std::string mime_type;
net::GetMimeTypeFromFile(base::FilePath().AppendASCII(path), &mime_type);
return mime_type;
}