-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathclient_impl.cc
166 lines (129 loc) · 5.07 KB
/
client_impl.cc
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
// Copyright (c) 2017 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 "examples/message_router/client_impl.h"
#include <algorithm>
#include "include/wrapper/cef_helpers.h"
#include "examples/shared/client_util.h"
#include "examples/shared/resource_util.h"
namespace message_router {
namespace {
const char kTestMessageName[] = "MessageRouterTest";
// Handle messages in the browser process.
class MessageHandler : public CefMessageRouterBrowserSide::Handler {
public:
explicit MessageHandler(const CefString& startup_url)
: startup_url_(startup_url) {}
// Called due to cefQuery execution in message_router.html.
bool OnQuery(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
int64_t query_id,
const CefString& request,
bool persistent,
CefRefPtr<Callback> callback) override {
// Only handle messages from the startup URL.
const std::string& url = frame->GetURL();
if (url.find(startup_url_) != 0)
return false;
const std::string& message_name = request;
if (message_name.find(kTestMessageName) == 0) {
// Reverse the string and return.
std::string result = message_name.substr(sizeof(kTestMessageName));
std::reverse(result.begin(), result.end());
callback->Success(result);
return true;
}
return false;
}
private:
const CefString startup_url_;
DISALLOW_COPY_AND_ASSIGN(MessageHandler);
};
} // namespace
Client::Client(const CefString& startup_url)
: startup_url_(startup_url), browser_ct_(0) {}
void Client::OnTitleChange(CefRefPtr<CefBrowser> browser,
const CefString& title) {
// Call the default shared implementation.
shared::OnTitleChange(browser, title);
}
bool Client::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefProcessId source_process,
CefRefPtr<CefProcessMessage> message) {
CEF_REQUIRE_UI_THREAD();
return message_router_->OnProcessMessageReceived(browser, frame,
source_process, message);
}
void Client::OnAfterCreated(CefRefPtr<CefBrowser> browser) {
CEF_REQUIRE_UI_THREAD();
if (!message_router_) {
// Create the browser-side router for query handling.
CefMessageRouterConfig config;
message_router_ = CefMessageRouterBrowserSide::Create(config);
// Register handlers with the router.
message_handler_.reset(new MessageHandler(startup_url_));
message_router_->AddHandler(message_handler_.get(), false);
}
browser_ct_++;
// Call the default shared implementation.
shared::OnAfterCreated(browser);
}
bool Client::DoClose(CefRefPtr<CefBrowser> browser) {
// Call the default shared implementation.
return shared::DoClose(browser);
}
void Client::OnBeforeClose(CefRefPtr<CefBrowser> browser) {
CEF_REQUIRE_UI_THREAD();
if (--browser_ct_ == 0) {
// Free the router when the last browser is closed.
message_router_->RemoveHandler(message_handler_.get());
message_handler_.reset();
message_router_ = nullptr;
}
// Call the default shared implementation.
shared::OnBeforeClose(browser);
}
bool Client::OnBeforeBrowse(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefRequest> request,
bool user_gesture,
bool is_redirect) {
CEF_REQUIRE_UI_THREAD();
message_router_->OnBeforeBrowse(browser, frame);
return false;
}
CefRefPtr<CefResourceRequestHandler> Client::GetResourceRequestHandler(
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefRequest> request,
bool is_navigation,
bool is_download,
const CefString& request_initiator,
bool& disable_default_handling) {
CEF_REQUIRE_IO_THREAD();
return this;
}
void Client::OnRenderProcessTerminated(CefRefPtr<CefBrowser> browser,
TerminationStatus status,
int error_code,
const CefString& error_string) {
CEF_REQUIRE_UI_THREAD();
message_router_->OnRenderProcessTerminated(browser);
}
CefRefPtr<CefResourceHandler> Client::GetResourceHandler(
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefRequest> request) {
CEF_REQUIRE_IO_THREAD();
const std::string& url = request->GetURL();
// This is a minimal implementation of resource loading. For more complex
// usage (multiple files, zip archives, custom handlers, etc.) you might want
// to use CefResourceManager. See the "resource_manager" target for an
// example implementation.
const std::string& resource_path = shared::GetResourcePath(url);
if (!resource_path.empty())
return shared::GetResourceHandler(resource_path);
return nullptr;
}
} // namespace message_router