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 // Support utilities for the JSON automation interface used by PyAuto. 6 7 #ifndef CHROME_BROWSER_AUTOMATION_AUTOMATION_PROVIDER_JSON_H_ 8 #define CHROME_BROWSER_AUTOMATION_AUTOMATION_PROVIDER_JSON_H_ 9 10 #include <string> 11 12 #include "base/compiler_specific.h" 13 #include "chrome/common/automation_constants.h" 14 15 class AutomationId; 16 class AutomationProvider; 17 class Browser; 18 class Profile; 19 20 namespace base { 21 class DictionaryValue; 22 class Value; 23 } 24 25 namespace content { 26 class RenderViewHost; 27 class WebContents; 28 } 29 30 namespace extensions { 31 class Extension; 32 } 33 34 namespace IPC { 35 class Message; 36 } 37 38 // Helper to ensure we always send a reply message for JSON automation requests. 39 class AutomationJSONReply { 40 public: 41 // Creates a new reply object for the IPC message |reply_message| for 42 // |provider|. The caller is expected to call SendSuccess() or SendError() 43 // before destroying this object. 44 AutomationJSONReply(AutomationProvider* provider, 45 IPC::Message* reply_message); 46 47 ~AutomationJSONReply(); 48 49 // Send a success reply along with data contained in |value|. 50 // An empty message will be sent if |value| is NULL. 51 void SendSuccess(const base::Value* value); 52 53 // Send an error reply along with error message |error_message|. 54 void SendError(const std::string& error_message); 55 56 // Send an error reply along with the specified error code and its 57 // associated error message. 58 void SendErrorCode(automation::ErrorCode code); 59 60 // Send an automation error. 61 void SendError(const automation::Error& error); 62 63 private: 64 AutomationProvider* provider_; 65 IPC::Message* message_; 66 }; 67 68 // Gets the browser specified by the given dictionary |args|. |args| should 69 // contain a key 'windex' which refers to the index of the browser. Returns 70 // true on success and sets |browser|. Otherwise, |error| will be set. 71 bool GetBrowserFromJSONArgs(base::DictionaryValue* args, 72 Browser** browser, 73 std::string* error) WARN_UNUSED_RESULT; 74 75 // Gets the tab specified by the given dictionary |args|. |args| should 76 // contain a key 'windex' which refers to the index of the parent browser, 77 // and a key 'tab_index' which refers to the index of the tab in that browser. 78 // Returns true on success and sets |tab|. Otherwise, |error| will be set. 79 bool GetTabFromJSONArgs(base::DictionaryValue* args, 80 content::WebContents** tab, 81 std::string* error) WARN_UNUSED_RESULT; 82 83 // Gets the browser and tab specified by the given dictionary |args|. |args| 84 // should contain a key 'windex' which refers to the index of the browser and 85 // a key 'tab_index' which refers to the index of the tab in that browser. 86 // Returns true on success and sets |browser| and |tab|. Otherwise, |error| 87 // will be set. 88 bool GetBrowserAndTabFromJSONArgs(base::DictionaryValue* args, 89 Browser** browser, 90 content::WebContents** tab, 91 std::string* error) WARN_UNUSED_RESULT; 92 93 // Gets an automation ID from the given value in the given dicitionary |args|. 94 // Returns true on success and sets |id|. Otherwise, |error| will be set. 95 bool GetAutomationIdFromJSONArgs( 96 base::DictionaryValue* args, 97 const std::string& key, 98 AutomationId* id, 99 std::string* error) WARN_UNUSED_RESULT; 100 101 // Gets the render view specified by the given dictionary |args|. |args| 102 // should contain a key 'view_id' which refers to an automation ID for the 103 // render view. Returns true on success and sets |rvh|. Otherwise, |error| 104 // will be set. 105 bool GetRenderViewFromJSONArgs( 106 base::DictionaryValue* args, 107 Profile* profile, 108 content::RenderViewHost** rvh, 109 std::string* error) WARN_UNUSED_RESULT; 110 111 // Gets the extension specified by the given dictionary |args|. |args| 112 // should contain the given key which refers to an extension ID. Returns 113 // true on success and sets |extension|. Otherwise, |error| will be set. 114 // The retrieved extension may be disabled or crashed. 115 bool GetExtensionFromJSONArgs( 116 base::DictionaryValue* args, 117 const std::string& key, 118 Profile* profile, 119 const extensions::Extension** extension, 120 std::string* error) WARN_UNUSED_RESULT; 121 122 // Gets the enabled extension specified by the given dictionary |args|. |args| 123 // should contain the given key which refers to an extension ID. Returns 124 // true on success and sets |extension|. Otherwise, |error| will be set. 125 // The retrieved extension will not be disabled or crashed. 126 bool GetEnabledExtensionFromJSONArgs( 127 base::DictionaryValue* args, 128 const std::string& key, 129 Profile* profile, 130 const extensions::Extension** extension, 131 std::string* error) WARN_UNUSED_RESULT; 132 133 #endif // CHROME_BROWSER_AUTOMATION_AUTOMATION_PROVIDER_JSON_H_ 134