1 // Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that
3 // can be found in the LICENSE file.
4
5 #include "tests/shared/browser/resource_util.h"
6
7 #include "include/base/cef_logging.h"
8 #include "include/cef_stream.h"
9 #include "include/wrapper/cef_byte_read_handler.h"
10 #include "include/wrapper/cef_stream_resource_handler.h"
11
12 namespace client {
13
14 namespace {
15
LoadBinaryResource(int binaryId,DWORD & dwSize,LPBYTE & pBytes)16 bool LoadBinaryResource(int binaryId, DWORD& dwSize, LPBYTE& pBytes) {
17 HINSTANCE hInst = GetModuleHandle(nullptr);
18 HRSRC hRes =
19 FindResource(hInst, MAKEINTRESOURCE(binaryId), MAKEINTRESOURCE(256));
20 if (hRes) {
21 HGLOBAL hGlob = LoadResource(hInst, hRes);
22 if (hGlob) {
23 dwSize = SizeofResource(hInst, hRes);
24 pBytes = (LPBYTE)LockResource(hGlob);
25 if (dwSize > 0 && pBytes)
26 return true;
27 }
28 }
29
30 return false;
31 }
32
33 // Provider of binary resources.
34 class BinaryResourceProvider : public CefResourceManager::Provider {
35 public:
BinaryResourceProvider(const std::string & url_path,const std::string & resource_path_prefix)36 BinaryResourceProvider(const std::string& url_path,
37 const std::string& resource_path_prefix)
38 : url_path_(url_path), resource_path_prefix_(resource_path_prefix) {
39 DCHECK(!url_path.empty());
40 if (!resource_path_prefix_.empty() &&
41 resource_path_prefix_[resource_path_prefix_.length() - 1] != '/') {
42 resource_path_prefix_ += "/";
43 }
44 }
45
OnRequest(scoped_refptr<CefResourceManager::Request> request)46 bool OnRequest(scoped_refptr<CefResourceManager::Request> request) override {
47 CEF_REQUIRE_IO_THREAD();
48
49 const std::string& url = request->url();
50 if (url.find(url_path_) != 0L) {
51 // Not handled by this provider.
52 return false;
53 }
54
55 CefRefPtr<CefResourceHandler> handler;
56
57 std::string relative_path = url.substr(url_path_.length());
58 if (!relative_path.empty()) {
59 if (!resource_path_prefix_.empty())
60 relative_path = resource_path_prefix_ + relative_path;
61
62 CefRefPtr<CefStreamReader> stream =
63 GetBinaryResourceReader(relative_path.data());
64 if (stream.get()) {
65 handler = new CefStreamResourceHandler(
66 request->mime_type_resolver().Run(url), stream);
67 }
68 }
69
70 request->Continue(handler);
71 return true;
72 }
73
74 private:
75 std::string url_path_;
76 std::string resource_path_prefix_;
77
78 DISALLOW_COPY_AND_ASSIGN(BinaryResourceProvider);
79 };
80
81 } // namespace
82
83 // Implemented in resource_util_win_idmap.cc.
84 extern int GetResourceId(const char* resource_name);
85
LoadBinaryResource(const char * resource_name,std::string & resource_data)86 bool LoadBinaryResource(const char* resource_name, std::string& resource_data) {
87 int resource_id = GetResourceId(resource_name);
88 if (resource_id == 0)
89 return false;
90
91 DWORD dwSize;
92 LPBYTE pBytes;
93
94 if (LoadBinaryResource(resource_id, dwSize, pBytes)) {
95 resource_data = std::string(reinterpret_cast<char*>(pBytes), dwSize);
96 return true;
97 }
98
99 NOTREACHED(); // The resource should be found.
100 return false;
101 }
102
GetBinaryResourceReader(const char * resource_name)103 CefRefPtr<CefStreamReader> GetBinaryResourceReader(const char* resource_name) {
104 int resource_id = GetResourceId(resource_name);
105 if (resource_id == 0)
106 return nullptr;
107
108 DWORD dwSize;
109 LPBYTE pBytes;
110
111 if (LoadBinaryResource(resource_id, dwSize, pBytes)) {
112 return CefStreamReader::CreateForHandler(
113 new CefByteReadHandler(pBytes, dwSize, nullptr));
114 }
115
116 NOTREACHED(); // The resource should be found.
117 return nullptr;
118 }
119
CreateBinaryResourceProvider(const std::string & url_path,const std::string & resource_path_prefix)120 CefResourceManager::Provider* CreateBinaryResourceProvider(
121 const std::string& url_path,
122 const std::string& resource_path_prefix) {
123 return new BinaryResourceProvider(url_path, resource_path_prefix);
124 }
125
126 } // namespace client
127