1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CONTENT_RENDERER_DOM_AUTOMATION_CONTROLLER_H_ 6 #define CONTENT_RENDERER_DOM_AUTOMATION_CONTROLLER_H_ 7 8 #include "ipc/ipc_sender.h" 9 #include "webkit/renderer/cpp_bound_class.h" 10 11 /* DomAutomationController class: 12 Bound to Javascript window.domAutomationController object. 13 At the very basic, this object makes any native value (string, numbers, 14 boolean) from javascript available to the automation host in Cpp. 15 Any renderer implementation that is built with this binding will allow the 16 above facility. 17 The intended use of this object is to expose the DOM Objects and their 18 attributes to the automation host. 19 20 A typical usage would be like following (JS code): 21 22 var object = document.getElementById('some_id'); 23 window.domAutomationController.send(object.nodeName); // get the tag name 24 25 For the exact mode of usage, 26 refer AutomationProxyTest.*DomAutomationController tests. 27 28 The class provides a single send method that can send variety of native 29 javascript values. (NPString, Number(double), Boolean) 30 31 The actual communication occurs in the following manner: 32 33 TEST MASTER RENDERER 34 (1) (3) 35 |AProxy| ----->|AProvider|----->|RenderView|------| 36 /\ | | | 37 | | | | 38 |(6) |(2) |(0) |(4) 39 | | \/ | 40 | |-------->|DAController|<----| 41 | | 42 | |(5) 43 |-------|WebContentsImpl|<--------| 44 45 46 Legends: 47 - AProxy = AutomationProxy 48 - AProvider = AutomationProvider 49 - DAController = DomAutomationController 50 51 (0) Initialization step where DAController is bound to the renderer 52 and the view_id of the renderer is supplied to the DAController for 53 routing message in (5). (routing_id_) 54 (1) A 'javascript:' url is sent from the test process to master as an IPC 55 message. A unique routing id is generated at this stage (automation_id_) 56 (2) The automation_id_ of step (1) is supplied to DAController by calling 57 the bound method setAutomationId(). This is required for routing message 58 in (6). 59 (3) The 'javascript:' url is sent for execution by calling into 60 Browser::LoadURL() 61 (4) A callback is generated as a result of domAutomationController.send() 62 into Cpp. The supplied value is received as a result of this callback. 63 (5) The value received in (4) is sent to the master along with the 64 stored automation_id_ as an IPC message. routing_id_ is used to route 65 the message. (IPC messages, ViewHostMsg_*DomAutomation* ) 66 (6) The value and the automation_id_ is extracted out of the message received 67 in (5). This value is relayed to AProxy using another IPC message. 68 automation_id_ is used to route the message. 69 (IPC messages, AutomationMsg_Dom*Response) 70 71 */ 72 73 namespace content { 74 75 // TODO(vibhor): Add another method-pair like sendLater() and sendNow() 76 // sendLater() should keep building a json serializer 77 // sendNow() should send the above serializer as a string. 78 class DomAutomationController : public webkit_glue::CppBoundClass { 79 public: 80 DomAutomationController(); 81 82 // Makes the renderer send a javascript value to the app. 83 // The value to be sent can be either of type NPString, 84 // Number (double casted to int32) or boolean. 85 // The function returns true/false based on the result of actual send over 86 // IPC. It sets the return value to null on unexpected errors or arguments. 87 void Send(const webkit_glue::CppArgumentList& args, 88 webkit_glue::CppVariant* result); 89 90 // Makes the renderer send a javascript value to the app. 91 // The value must be a NPString and should be properly formed JSON. 92 // This function does not modify/escape the returned string in any way. 93 void SendJSON(const webkit_glue::CppArgumentList& args, 94 webkit_glue::CppVariant* result); 95 96 // Sends a string with a provided Automation Id. 97 // Expects two javascript values; the first must be a number type and will be 98 // used as the Automation Id, the second must be of type NPString. 99 // The function returns true/false to the javascript based on the success 100 // of the send over IPC. It sets the javascript return value to null on 101 // unexpected errors or arguments. 102 void SendWithId(const webkit_glue::CppArgumentList& args, 103 webkit_glue::CppVariant* result); 104 105 void SetAutomationId(const webkit_glue::CppArgumentList& args, 106 webkit_glue::CppVariant* result); 107 108 // TODO(vibhor): Implement later 109 // static CppBindingObjectMethod sendLater; 110 // static CppBindingObjectMethod sendNow; 111 set_routing_id(int routing_id)112 void set_routing_id(int routing_id) { routing_id_ = routing_id; } 113 set_message_sender(IPC::Sender * sender)114 void set_message_sender(IPC::Sender* sender) { 115 sender_ = sender; 116 } 117 118 private: 119 IPC::Sender* sender_; 120 121 // Refer to the comments at the top of the file for more details. 122 int routing_id_; // routing id to be used by first channel. 123 int automation_id_; // routing id to be used by the next channel. 124 }; 125 126 } // namespace content 127 128 #endif // CONTENT_RENDERER_DOM_AUTOMATION_CONTROLLER_H_ 129