| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/automation/automation_provider_json.h" | 5 #include "chrome/browser/automation/automation_provider_json.h" |
| 6 | 6 |
| 7 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
| 8 #include "base/json/string_escape.h" | 8 #include "base/json/string_escape.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/browser/autocomplete/autocomplete_match.h" | 10 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 11 #include "chrome/browser/automation/automation_provider.h" | 11 #include "chrome/browser/automation/automation_provider.h" |
| 12 #include "chrome/browser/automation/automation_util.h" | 12 #include "chrome/browser/automation/automation_util.h" |
| 13 #include "chrome/common/automation_id.h" |
| 13 #include "chrome/common/automation_messages.h" | 14 #include "chrome/common/automation_messages.h" |
| 15 #include "content/browser/tab_contents/tab_contents.h" |
| 14 | 16 |
| 15 namespace { | 17 namespace { |
| 16 | 18 |
| 17 // Util for creating a JSON error return string (dict with key | 19 // Util for creating a JSON error return string (dict with key |
| 18 // 'error' and error string value). No need to quote input. | 20 // 'error' and error string value). No need to quote input. |
| 19 std::string JSONErrorString(const std::string& err) { | 21 std::string JSONErrorString(const std::string& err) { |
| 20 std::string prefix = "{\"error\": \""; | 22 std::string prefix = "{\"error\": \""; |
| 21 std::string no_quote_err; | 23 std::string no_quote_err; |
| 22 std::string suffix = "\"}"; | 24 std::string suffix = "\"}"; |
| 23 | 25 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 54 AutomationMsg_SendJSONRequest::WriteReplyParams( | 56 AutomationMsg_SendJSONRequest::WriteReplyParams( |
| 55 message_, json_string, false); | 57 message_, json_string, false); |
| 56 provider_->Send(message_); | 58 provider_->Send(message_); |
| 57 message_ = NULL; | 59 message_ = NULL; |
| 58 } | 60 } |
| 59 | 61 |
| 60 bool GetBrowserFromJSONArgs( | 62 bool GetBrowserFromJSONArgs( |
| 61 DictionaryValue* args, | 63 DictionaryValue* args, |
| 62 Browser** browser, | 64 Browser** browser, |
| 63 std::string* error) { | 65 std::string* error) { |
| 64 int browser_index; | 66 if (args->HasKey("auto_id")) { |
| 65 if (!args->GetInteger("windex", &browser_index)) { | 67 AutomationId id; |
| 66 *error = "'windex' missing or invalid"; | 68 if (!GetAutomationIdFromJSONArgs(args, "auto_id", &id, error)) |
| 67 return false; | 69 return false; |
| 68 } | 70 TabContents* tab; |
| 69 *browser = automation_util::GetBrowserAt(browser_index); | 71 if (!automation_util::GetTabForId(id, &tab)) { |
| 70 if (!*browser) { | 72 *error = "'auto_id' does not refer to an open tab"; |
| 71 *error = "Cannot locate browser from given index"; | 73 return false; |
| 72 return false; | 74 } |
| 75 Browser* container = automation_util::GetBrowserForTab(tab); |
| 76 if (!container) { |
| 77 *error = "tab does not belong to an open browser"; |
| 78 return false; |
| 79 } |
| 80 *browser = container; |
| 81 } else { |
| 82 int browser_index; |
| 83 if (!args->GetInteger("windex", &browser_index)) { |
| 84 *error = "'windex' missing or invalid"; |
| 85 return false; |
| 86 } |
| 87 *browser = automation_util::GetBrowserAt(browser_index); |
| 88 if (!*browser) { |
| 89 *error = "Cannot locate browser from given index"; |
| 90 return false; |
| 91 } |
| 73 } | 92 } |
| 74 return true; | 93 return true; |
| 75 } | 94 } |
| 76 | 95 |
| 77 bool GetTabFromJSONArgs( | 96 bool GetTabFromJSONArgs( |
| 78 DictionaryValue* args, | 97 DictionaryValue* args, |
| 79 TabContents** tab, | 98 TabContents** tab, |
| 80 std::string* error) { | 99 std::string* error) { |
| 81 int browser_index, tab_index; | 100 if (args->HasKey("auto_id")) { |
| 82 if (!args->GetInteger("windex", &browser_index)) { | 101 AutomationId id; |
| 83 *error = "'windex' missing or invalid"; | 102 if (!GetAutomationIdFromJSONArgs(args, "auto_id", &id, error)) |
| 84 return false; | 103 return false; |
| 85 } | 104 if (!automation_util::GetTabForId(id, tab)) { |
| 86 if (!args->GetInteger("tab_index", &tab_index)) { | 105 *error = "'auto_id' does not refer to an open tab"; |
| 87 *error = "'tab_index' missing or invalid"; | 106 return false; |
| 88 return false; | 107 } |
| 89 } | 108 } else { |
| 90 *tab = automation_util::GetTabContentsAt(browser_index, tab_index); | 109 int browser_index, tab_index; |
| 91 if (!*tab) { | 110 if (!args->GetInteger("windex", &browser_index)) { |
| 92 *error = "Cannot locate tab from given indices"; | 111 *error = "'windex' missing or invalid"; |
| 93 return false; | 112 return false; |
| 113 } |
| 114 if (!args->GetInteger("tab_index", &tab_index)) { |
| 115 *error = "'tab_index' missing or invalid"; |
| 116 return false; |
| 117 } |
| 118 *tab = automation_util::GetTabContentsAt(browser_index, tab_index); |
| 119 if (!*tab) { |
| 120 *error = "Cannot locate tab from given indices"; |
| 121 return false; |
| 122 } |
| 94 } | 123 } |
| 95 return true; | 124 return true; |
| 96 } | 125 } |
| 97 | 126 |
| 98 bool GetBrowserAndTabFromJSONArgs( | 127 bool GetBrowserAndTabFromJSONArgs( |
| 99 DictionaryValue* args, | 128 DictionaryValue* args, |
| 100 Browser** browser, | 129 Browser** browser, |
| 101 TabContents** tab, | 130 TabContents** tab, |
| 102 std::string* error) { | 131 std::string* error) { |
| 103 return GetBrowserFromJSONArgs(args, browser, error) && | 132 return GetBrowserFromJSONArgs(args, browser, error) && |
| 104 GetTabFromJSONArgs(args, tab, error); | 133 GetTabFromJSONArgs(args, tab, error); |
| 105 } | 134 } |
| 135 |
| 136 bool GetAutomationIdFromJSONArgs( |
| 137 DictionaryValue* args, |
| 138 const std::string& key_name, |
| 139 AutomationId* id, |
| 140 std::string* error) { |
| 141 Value* id_value; |
| 142 if (!args->Get(key_name, &id_value)) { |
| 143 *error = base::StringPrintf("Missing parameter '%s'", key_name.c_str()); |
| 144 return false; |
| 145 } |
| 146 return AutomationId::FromValue(id_value, id, error); |
| 147 } |
| 148 |
| 149 bool GetRenderViewFromJSONArgs( |
| 150 DictionaryValue* args, |
| 151 Profile* profile, |
| 152 RenderViewHost** rvh, |
| 153 std::string* error) { |
| 154 Value* id_value; |
| 155 if (args->Get("auto_id", &id_value)) { |
| 156 AutomationId id; |
| 157 if (!AutomationId::FromValue(id_value, &id, error)) |
| 158 return false; |
| 159 if (!automation_util::GetRenderViewForId(id, profile, rvh)) { |
| 160 *error = "ID does not correspond to an open view"; |
| 161 return false; |
| 162 } |
| 163 } else { |
| 164 // If the render view id is not specified, check for browser/tab indices. |
| 165 TabContents* tab = NULL; |
| 166 if (!GetTabFromJSONArgs(args, &tab, error)) |
| 167 return false; |
| 168 *rvh = tab->render_view_host(); |
| 169 } |
| 170 return true; |
| 171 } |
| OLD | NEW |