-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathres_scheme.cpp
168 lines (141 loc) · 4.5 KB
/
res_scheme.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright (c) 2011 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
#include "res_scheme.h"
#include <algorithm>
#include <string>
#include "include/cef_browser.h"
#include "include/cef_frame.h"
#include "include/cef_response.h"
#include "include/cef_request.h"
#include "include/cef_scheme.h"
#include "cefclient/resource_util.h"
#include "cefclient/string_util.h"
#include "cefclient/util.h"
#include "system.h"
#if defined(OS_WIN)
#include "cefclient/resource.h"
#endif
// Implementation of the schema handler for client:// requests.
class ResSchemeHandler : public CefSchemeHandler {
public:
ResSchemeHandler() : offset_(0) {}
virtual bool ProcessRequest(CefRefPtr<CefRequest> request,
CefRefPtr<CefSchemeHandlerCallback> callback)
OVERRIDE {
REQUIRE_IO_THREAD();
extern HINSTANCE hInst;
bool handled = false;
AutoLock lock_scope(this);
std::string url = request->GetURL();
std::string res=url.substr(6);
std::string modulePath="";
string name="";
int index=res.find_last_of("/");
HMODULE hModule=hInst;
if(index!=-1&&index!=(int)res.length()-1){
modulePath=res.substr(0,index);
name=res.substr(index+1);
hModule=GetModuleHandle(CefString(modulePath).ToWString().data());
}
else{
name=res.substr(0,index);
}
std::string ext=GetExt(name);
// Load the response image
#if defined(OS_WIN)
DWORD dwSize;
LPBYTE pBytes;
if (LoadBinaryResourceByName(CefString(name).ToWString().data(), dwSize, pBytes, hModule)) {
data_ = std::string(reinterpret_cast<const char*>(pBytes), dwSize);
handled = true;
// Set the resulting mime type
if(ext=="html"||ext=="js"||ext=="css"){
if(ext=="js"){
mime_type_ = "text/javascript";
}
else{
mime_type_ = "text/"+ext;
}
}
else{
mime_type_ = "image/"+ext;
}
}
#elif defined(OS_MACOSX) || defined(OS_LINUX)
if (LoadBinaryResource("logo.png", data_)) {
handled = true;
// Set the resulting mime type
mime_type_ = "image/png";
}
#else
#error "Unsupported platform"
#endif
if (handled) {
// Indicate the headers are available.
callback->HeadersAvailable();
return true;
}
return false;
}
virtual void GetResponseHeaders(CefRefPtr<CefResponse> response,
int64& response_length,
CefString& redirectUrl) OVERRIDE {
REQUIRE_IO_THREAD();
ASSERT(!data_.empty());
response->SetMimeType(mime_type_);
response->SetStatus(200);
// Set the resulting response length
response_length = data_.length();
}
virtual void Cancel() OVERRIDE {
REQUIRE_IO_THREAD();
}
virtual bool ReadResponse(void* data_out,
int bytes_to_read,
int& bytes_read,
CefRefPtr<CefSchemeHandlerCallback> callback)
OVERRIDE {
REQUIRE_IO_THREAD();
bool has_data = false;
bytes_read = 0;
AutoLock lock_scope(this);
if (offset_ < data_.length()) {
// Copy the next block of data into the buffer.
int transfer_size =
std::min(bytes_to_read, static_cast<int>(data_.length() - offset_));
memcpy(data_out, data_.c_str() + offset_, transfer_size);
offset_ += transfer_size;
bytes_read = transfer_size;
has_data = true;
}
return has_data;
}
private:
std::string data_;
std::string mime_type_;
size_t offset_;
IMPLEMENT_REFCOUNTING(ResSchemeHandler);
IMPLEMENT_LOCKING(ResSchemeHandler);
};
// Implementation of the factory for for creating schema handlers.
class ResSchemeHandlerFactory : public CefSchemeHandlerFactory {
public:
// Return a new scheme handler instance to handle the request.
virtual CefRefPtr<CefSchemeHandler> Create(CefRefPtr<CefBrowser> browser,
const CefString& scheme_name,
CefRefPtr<CefRequest> request)
OVERRIDE {
REQUIRE_IO_THREAD();
return new ResSchemeHandler();
}
IMPLEMENT_REFCOUNTING(ClientSchemeHandlerFactory);
};
void InitResScheme() {
CefRegisterCustomScheme("res", true, false, false);
CefRegisterSchemeHandlerFactory("res", "",
new ResSchemeHandlerFactory());
}
void RunResScheme(CefRefPtr<CefBrowser> browser) {
browser->GetMainFrame()->LoadURL("res://AlloyDesktop/logo.png");
}