1 // Copyright (c) 2014 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/ceftests/routing_test_handler.h"
6 #include "tests/shared/renderer/client_app_renderer.h"
7
8 using client::ClientAppRenderer;
9
10 namespace {
11
SetRouterConfig(CefMessageRouterConfig & config)12 void SetRouterConfig(CefMessageRouterConfig& config) {
13 config.js_query_function = "testQuery";
14 config.js_cancel_function = "testQueryCancel";
15 }
16
17 // Handle the renderer side of the routing implementation.
18 class RoutingRenderDelegate : public ClientAppRenderer::Delegate {
19 public:
RoutingRenderDelegate()20 RoutingRenderDelegate() {}
21
OnWebKitInitialized(CefRefPtr<ClientAppRenderer> app)22 void OnWebKitInitialized(CefRefPtr<ClientAppRenderer> app) override {
23 // Create the renderer-side router for query handling.
24 CefMessageRouterConfig config;
25 SetRouterConfig(config);
26 message_router_ = CefMessageRouterRendererSide::Create(config);
27 }
28
OnContextCreated(CefRefPtr<ClientAppRenderer> app,CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,CefRefPtr<CefV8Context> context)29 void OnContextCreated(CefRefPtr<ClientAppRenderer> app,
30 CefRefPtr<CefBrowser> browser,
31 CefRefPtr<CefFrame> frame,
32 CefRefPtr<CefV8Context> context) override {
33 message_router_->OnContextCreated(browser, frame, context);
34 }
35
OnContextReleased(CefRefPtr<ClientAppRenderer> app,CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,CefRefPtr<CefV8Context> context)36 void OnContextReleased(CefRefPtr<ClientAppRenderer> app,
37 CefRefPtr<CefBrowser> browser,
38 CefRefPtr<CefFrame> frame,
39 CefRefPtr<CefV8Context> context) override {
40 message_router_->OnContextReleased(browser, frame, context);
41 }
42
OnProcessMessageReceived(CefRefPtr<ClientAppRenderer> app,CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,CefProcessId source_process,CefRefPtr<CefProcessMessage> message)43 bool OnProcessMessageReceived(CefRefPtr<ClientAppRenderer> app,
44 CefRefPtr<CefBrowser> browser,
45 CefRefPtr<CefFrame> frame,
46 CefProcessId source_process,
47 CefRefPtr<CefProcessMessage> message) override {
48 return message_router_->OnProcessMessageReceived(browser, frame,
49 source_process, message);
50 }
51
52 private:
53 CefRefPtr<CefMessageRouterRendererSide> message_router_;
54
55 IMPLEMENT_REFCOUNTING(RoutingRenderDelegate);
56 };
57
58 } // namespace
59
RoutingTestHandler(CompletionState * completion_state)60 RoutingTestHandler::RoutingTestHandler(CompletionState* completion_state)
61 : TestHandler(completion_state) {}
62
OnAfterCreated(CefRefPtr<CefBrowser> browser)63 void RoutingTestHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser) {
64 if (!message_router_.get()) {
65 // Create the browser-side router for query handling.
66 CefMessageRouterConfig config;
67 SetRouterConfig(config);
68 message_router_ = CefMessageRouterBrowserSide::Create(config);
69 message_router_->AddHandler(this, false);
70 }
71 TestHandler::OnAfterCreated(browser);
72 }
73
OnBeforeClose(CefRefPtr<CefBrowser> browser)74 void RoutingTestHandler::OnBeforeClose(CefRefPtr<CefBrowser> browser) {
75 message_router_->OnBeforeClose(browser);
76 TestHandler::OnBeforeClose(browser);
77 }
78
OnRenderProcessTerminated(CefRefPtr<CefBrowser> browser,TerminationStatus status)79 void RoutingTestHandler::OnRenderProcessTerminated(
80 CefRefPtr<CefBrowser> browser,
81 TerminationStatus status) {
82 message_router_->OnRenderProcessTerminated(browser);
83 }
84
OnBeforeBrowse(CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,CefRefPtr<CefRequest> request,bool user_gesture,bool is_redirect)85 bool RoutingTestHandler::OnBeforeBrowse(CefRefPtr<CefBrowser> browser,
86 CefRefPtr<CefFrame> frame,
87 CefRefPtr<CefRequest> request,
88 bool user_gesture,
89 bool is_redirect) {
90 message_router_->OnBeforeBrowse(browser, frame);
91 return false;
92 }
93
OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,CefProcessId source_process,CefRefPtr<CefProcessMessage> message)94 bool RoutingTestHandler::OnProcessMessageReceived(
95 CefRefPtr<CefBrowser> browser,
96 CefRefPtr<CefFrame> frame,
97 CefProcessId source_process,
98 CefRefPtr<CefProcessMessage> message) {
99 return message_router_->OnProcessMessageReceived(browser, frame,
100 source_process, message);
101 }
102
103 // Entry point for creating the test delegate.
104 // Called from client_app_delegates.cc.
CreateRoutingTestHandlerDelegate(ClientAppRenderer::DelegateSet & delegates)105 void CreateRoutingTestHandlerDelegate(
106 ClientAppRenderer::DelegateSet& delegates) {
107 delegates.insert(new RoutingRenderDelegate);
108 }
109