2013-04-03 20:20:59 +02:00
|
|
|
// Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights
|
2012-04-03 03:34:16 +02:00
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
2016-11-18 00:52:42 +01:00
|
|
|
#include "tests/shared/browser/resource_util.h"
|
2015-01-31 01:55:56 +01:00
|
|
|
|
2014-07-11 22:10:05 +02:00
|
|
|
#include "include/base/cef_logging.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "include/cef_stream.h"
|
|
|
|
#include "include/wrapper/cef_byte_read_handler.h"
|
2015-05-23 03:57:31 +02:00
|
|
|
#include "include/wrapper/cef_stream_resource_handler.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2015-01-23 20:09:34 +01:00
|
|
|
namespace client {
|
|
|
|
|
2013-04-03 20:20:59 +02:00
|
|
|
namespace {
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
bool LoadBinaryResource(int binaryId, DWORD& dwSize, LPBYTE& pBytes) {
|
2021-06-17 22:08:01 +02:00
|
|
|
HINSTANCE hInst = GetModuleHandle(nullptr);
|
2017-05-17 11:29:28 +02:00
|
|
|
HRSRC hRes =
|
|
|
|
FindResource(hInst, MAKEINTRESOURCE(binaryId), MAKEINTRESOURCE(256));
|
2012-04-03 03:34:16 +02:00
|
|
|
if (hRes) {
|
|
|
|
HGLOBAL hGlob = LoadResource(hInst, hRes);
|
|
|
|
if (hGlob) {
|
|
|
|
dwSize = SizeofResource(hInst, hRes);
|
|
|
|
pBytes = (LPBYTE)LockResource(hGlob);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (dwSize > 0 && pBytes) {
|
2012-04-03 03:34:16 +02:00
|
|
|
return true;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-05-23 03:57:31 +02:00
|
|
|
// Provider of binary resources.
|
|
|
|
class BinaryResourceProvider : public CefResourceManager::Provider {
|
|
|
|
public:
|
2017-08-04 00:55:19 +02:00
|
|
|
BinaryResourceProvider(const std::string& url_path,
|
|
|
|
const std::string& resource_path_prefix)
|
|
|
|
: url_path_(url_path), resource_path_prefix_(resource_path_prefix) {
|
2015-05-23 03:57:31 +02:00
|
|
|
DCHECK(!url_path.empty());
|
2017-08-04 00:55:19 +02:00
|
|
|
if (!resource_path_prefix_.empty() &&
|
|
|
|
resource_path_prefix_[resource_path_prefix_.length() - 1] != '/') {
|
|
|
|
resource_path_prefix_ += "/";
|
|
|
|
}
|
2015-05-23 03:57:31 +02:00
|
|
|
}
|
|
|
|
|
2021-06-17 21:43:06 +02:00
|
|
|
bool OnRequest(scoped_refptr<CefResourceManager::Request> request) override {
|
2015-05-23 03:57:31 +02:00
|
|
|
CEF_REQUIRE_IO_THREAD();
|
|
|
|
|
|
|
|
const std::string& url = request->url();
|
|
|
|
if (url.find(url_path_) != 0L) {
|
|
|
|
// Not handled by this provider.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefResourceHandler> handler;
|
|
|
|
|
2017-08-04 00:55:19 +02:00
|
|
|
std::string relative_path = url.substr(url_path_.length());
|
2015-05-23 03:57:31 +02:00
|
|
|
if (!relative_path.empty()) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!resource_path_prefix_.empty()) {
|
2017-08-04 00:55:19 +02:00
|
|
|
relative_path = resource_path_prefix_ + relative_path;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-08-04 00:55:19 +02:00
|
|
|
|
2015-05-23 03:57:31 +02:00
|
|
|
CefRefPtr<CefStreamReader> stream =
|
|
|
|
GetBinaryResourceReader(relative_path.data());
|
|
|
|
if (stream.get()) {
|
|
|
|
handler = new CefStreamResourceHandler(
|
2017-05-17 11:29:28 +02:00
|
|
|
request->mime_type_resolver().Run(url), stream);
|
2015-05-23 03:57:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
request->Continue(handler);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string url_path_;
|
2017-08-04 00:55:19 +02:00
|
|
|
std::string resource_path_prefix_;
|
2015-05-23 03:57:31 +02:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(BinaryResourceProvider);
|
|
|
|
};
|
|
|
|
|
2013-04-03 20:20:59 +02:00
|
|
|
} // namespace
|
|
|
|
|
2016-11-18 00:52:42 +01:00
|
|
|
// Implemented in resource_util_win_idmap.cc.
|
|
|
|
extern int GetResourceId(const char* resource_name);
|
|
|
|
|
2013-04-03 20:20:59 +02:00
|
|
|
bool LoadBinaryResource(const char* resource_name, std::string& resource_data) {
|
|
|
|
int resource_id = GetResourceId(resource_name);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (resource_id == 0) {
|
2013-04-03 20:20:59 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2013-04-03 20:20:59 +02:00
|
|
|
|
|
|
|
DWORD dwSize;
|
|
|
|
LPBYTE pBytes;
|
|
|
|
|
|
|
|
if (LoadBinaryResource(resource_id, dwSize, pBytes)) {
|
|
|
|
resource_data = std::string(reinterpret_cast<char*>(pBytes), dwSize);
|
|
|
|
return true;
|
2012-04-12 22:21:50 +02:00
|
|
|
}
|
|
|
|
|
2014-07-11 22:10:05 +02:00
|
|
|
NOTREACHED(); // The resource should be found.
|
2013-04-03 20:20:59 +02:00
|
|
|
return false;
|
2012-04-12 22:21:50 +02:00
|
|
|
}
|
|
|
|
|
2013-04-03 20:20:59 +02:00
|
|
|
CefRefPtr<CefStreamReader> GetBinaryResourceReader(const char* resource_name) {
|
|
|
|
int resource_id = GetResourceId(resource_name);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (resource_id == 0) {
|
2020-01-15 15:28:12 +01:00
|
|
|
return nullptr;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2013-04-03 20:20:59 +02:00
|
|
|
|
|
|
|
DWORD dwSize;
|
|
|
|
LPBYTE pBytes;
|
|
|
|
|
|
|
|
if (LoadBinaryResource(resource_id, dwSize, pBytes)) {
|
|
|
|
return CefStreamReader::CreateForHandler(
|
2020-01-15 15:28:12 +01:00
|
|
|
new CefByteReadHandler(pBytes, dwSize, nullptr));
|
2013-04-03 20:20:59 +02:00
|
|
|
}
|
|
|
|
|
2014-07-11 22:10:05 +02:00
|
|
|
NOTREACHED(); // The resource should be found.
|
2020-01-15 15:28:12 +01:00
|
|
|
return nullptr;
|
2013-04-03 20:20:59 +02:00
|
|
|
}
|
2015-01-23 20:09:34 +01:00
|
|
|
|
2015-05-23 03:57:31 +02:00
|
|
|
CefResourceManager::Provider* CreateBinaryResourceProvider(
|
2017-08-04 00:55:19 +02:00
|
|
|
const std::string& url_path,
|
|
|
|
const std::string& resource_path_prefix) {
|
|
|
|
return new BinaryResourceProvider(url_path, resource_path_prefix);
|
2015-05-23 03:57:31 +02:00
|
|
|
}
|
|
|
|
|
2015-01-23 20:09:34 +01:00
|
|
|
} // namespace client
|