1 // Copyright (c) 2012 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/cefclient/renderer/client_renderer.h"
6
7 #include <sstream>
8 #include <string>
9
10 #include "include/cef_crash_util.h"
11 #include "include/cef_dom.h"
12 #include "include/wrapper/cef_helpers.h"
13 #include "include/wrapper/cef_message_router.h"
14
15 namespace client {
16 namespace renderer {
17
18 namespace {
19
20 // Must match the value in client_handler.cc.
21 const char kFocusedNodeChangedMessage[] = "ClientRenderer.FocusedNodeChanged";
22
23 class ClientRenderDelegate : public ClientAppRenderer::Delegate {
24 public:
ClientRenderDelegate()25 ClientRenderDelegate() : last_node_is_editable_(false) {}
26
OnWebKitInitialized(CefRefPtr<ClientAppRenderer> app)27 void OnWebKitInitialized(CefRefPtr<ClientAppRenderer> app) override {
28 if (CefCrashReportingEnabled()) {
29 // Set some crash keys for testing purposes. Keys must be defined in the
30 // "crash_reporter.cfg" file. See cef_crash_util.h for details.
31 CefSetCrashKeyValue("testkey_small1", "value1_small_renderer");
32 CefSetCrashKeyValue("testkey_small2", "value2_small_renderer");
33 CefSetCrashKeyValue("testkey_medium1", "value1_medium_renderer");
34 CefSetCrashKeyValue("testkey_medium2", "value2_medium_renderer");
35 CefSetCrashKeyValue("testkey_large1", "value1_large_renderer");
36 CefSetCrashKeyValue("testkey_large2", "value2_large_renderer");
37 }
38
39 // Create the renderer-side router for query handling.
40 CefMessageRouterConfig config;
41 message_router_ = CefMessageRouterRendererSide::Create(config);
42 }
43
OnContextCreated(CefRefPtr<ClientAppRenderer> app,CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,CefRefPtr<CefV8Context> context)44 void OnContextCreated(CefRefPtr<ClientAppRenderer> app,
45 CefRefPtr<CefBrowser> browser,
46 CefRefPtr<CefFrame> frame,
47 CefRefPtr<CefV8Context> context) override {
48 message_router_->OnContextCreated(browser, frame, context);
49 }
50
OnContextReleased(CefRefPtr<ClientAppRenderer> app,CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,CefRefPtr<CefV8Context> context)51 void OnContextReleased(CefRefPtr<ClientAppRenderer> app,
52 CefRefPtr<CefBrowser> browser,
53 CefRefPtr<CefFrame> frame,
54 CefRefPtr<CefV8Context> context) override {
55 message_router_->OnContextReleased(browser, frame, context);
56 }
57
OnFocusedNodeChanged(CefRefPtr<ClientAppRenderer> app,CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,CefRefPtr<CefDOMNode> node)58 void OnFocusedNodeChanged(CefRefPtr<ClientAppRenderer> app,
59 CefRefPtr<CefBrowser> browser,
60 CefRefPtr<CefFrame> frame,
61 CefRefPtr<CefDOMNode> node) override {
62 bool is_editable = (node.get() && node->IsEditable());
63 if (is_editable != last_node_is_editable_) {
64 // Notify the browser of the change in focused element type.
65 last_node_is_editable_ = is_editable;
66 CefRefPtr<CefProcessMessage> message =
67 CefProcessMessage::Create(kFocusedNodeChangedMessage);
68 message->GetArgumentList()->SetBool(0, is_editable);
69 frame->SendProcessMessage(PID_BROWSER, message);
70 }
71 }
72
OnProcessMessageReceived(CefRefPtr<ClientAppRenderer> app,CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,CefProcessId source_process,CefRefPtr<CefProcessMessage> message)73 bool OnProcessMessageReceived(CefRefPtr<ClientAppRenderer> app,
74 CefRefPtr<CefBrowser> browser,
75 CefRefPtr<CefFrame> frame,
76 CefProcessId source_process,
77 CefRefPtr<CefProcessMessage> message) override {
78 return message_router_->OnProcessMessageReceived(browser, frame,
79 source_process, message);
80 }
81
82 private:
83 bool last_node_is_editable_;
84
85 // Handles the renderer side of query routing.
86 CefRefPtr<CefMessageRouterRendererSide> message_router_;
87
88 DISALLOW_COPY_AND_ASSIGN(ClientRenderDelegate);
89 IMPLEMENT_REFCOUNTING(ClientRenderDelegate);
90 };
91
92 } // namespace
93
CreateDelegates(ClientAppRenderer::DelegateSet & delegates)94 void CreateDelegates(ClientAppRenderer::DelegateSet& delegates) {
95 delegates.insert(new ClientRenderDelegate);
96 }
97
98 } // namespace renderer
99 } // namespace client
100