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/browser/extensions/extension_service.h" |
| 14 #include "chrome/browser/profiles/profile.h" |
13 #include "chrome/common/automation_id.h" | 15 #include "chrome/common/automation_id.h" |
14 #include "chrome/common/automation_messages.h" | 16 #include "chrome/common/automation_messages.h" |
15 #include "content/browser/tab_contents/tab_contents.h" | 17 #include "content/browser/tab_contents/tab_contents.h" |
16 | 18 |
17 namespace { | 19 namespace { |
18 | 20 |
19 // Util for creating a JSON error return string (dict with key | 21 // Util for creating a JSON error return string (dict with key |
20 // 'error' and error string value). No need to quote input. | 22 // 'error' and error string value). No need to quote input. |
21 std::string JSONErrorString(const std::string& err) { | 23 std::string JSONErrorString(const std::string& err) { |
22 std::string prefix = "{\"error\": \""; | 24 std::string prefix = "{\"error\": \""; |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 DictionaryValue* args, | 130 DictionaryValue* args, |
129 Browser** browser, | 131 Browser** browser, |
130 TabContents** tab, | 132 TabContents** tab, |
131 std::string* error) { | 133 std::string* error) { |
132 return GetBrowserFromJSONArgs(args, browser, error) && | 134 return GetBrowserFromJSONArgs(args, browser, error) && |
133 GetTabFromJSONArgs(args, tab, error); | 135 GetTabFromJSONArgs(args, tab, error); |
134 } | 136 } |
135 | 137 |
136 bool GetAutomationIdFromJSONArgs( | 138 bool GetAutomationIdFromJSONArgs( |
137 DictionaryValue* args, | 139 DictionaryValue* args, |
138 const std::string& key_name, | 140 const std::string& key, |
139 AutomationId* id, | 141 AutomationId* id, |
140 std::string* error) { | 142 std::string* error) { |
141 Value* id_value; | 143 Value* id_value; |
142 if (!args->Get(key_name, &id_value)) { | 144 if (!args->Get(key, &id_value)) { |
143 *error = base::StringPrintf("Missing parameter '%s'", key_name.c_str()); | 145 *error = base::StringPrintf("Missing parameter '%s'", key.c_str()); |
144 return false; | 146 return false; |
145 } | 147 } |
146 return AutomationId::FromValue(id_value, id, error); | 148 return AutomationId::FromValue(id_value, id, error); |
147 } | 149 } |
148 | 150 |
149 bool GetRenderViewFromJSONArgs( | 151 bool GetRenderViewFromJSONArgs( |
150 DictionaryValue* args, | 152 DictionaryValue* args, |
151 Profile* profile, | 153 Profile* profile, |
152 RenderViewHost** rvh, | 154 RenderViewHost** rvh, |
153 std::string* error) { | 155 std::string* error) { |
154 Value* id_value; | 156 Value* id_value; |
155 if (args->Get("auto_id", &id_value)) { | 157 if (args->Get("auto_id", &id_value)) { |
156 AutomationId id; | 158 AutomationId id; |
157 if (!AutomationId::FromValue(id_value, &id, error)) | 159 if (!AutomationId::FromValue(id_value, &id, error)) |
158 return false; | 160 return false; |
159 if (!automation_util::GetRenderViewForId(id, profile, rvh)) { | 161 if (!automation_util::GetRenderViewForId(id, profile, rvh)) { |
160 *error = "ID does not correspond to an open view"; | 162 *error = "ID does not correspond to an open view"; |
161 return false; | 163 return false; |
162 } | 164 } |
163 } else { | 165 } else { |
164 // If the render view id is not specified, check for browser/tab indices. | 166 // If the render view id is not specified, check for browser/tab indices. |
165 TabContents* tab = NULL; | 167 TabContents* tab = NULL; |
166 if (!GetTabFromJSONArgs(args, &tab, error)) | 168 if (!GetTabFromJSONArgs(args, &tab, error)) |
167 return false; | 169 return false; |
168 *rvh = tab->render_view_host(); | 170 *rvh = tab->render_view_host(); |
169 } | 171 } |
170 return true; | 172 return true; |
171 } | 173 } |
| 174 |
| 175 namespace { |
| 176 |
| 177 bool GetExtensionFromJSONArgsHelper( |
| 178 base::DictionaryValue* args, |
| 179 const std::string& key, |
| 180 Profile* profile, |
| 181 bool include_disabled, |
| 182 const Extension** extension, |
| 183 std::string* error) { |
| 184 std::string id; |
| 185 if (!args->GetString(key, &id)) { |
| 186 *error = base::StringPrintf("Missing or invalid key: %s", key.c_str()); |
| 187 return false; |
| 188 } |
| 189 ExtensionService* service = profile->GetExtensionService(); |
| 190 if (!service) { |
| 191 *error = "No extensions service."; |
| 192 return false; |
| 193 } |
| 194 if (!service->GetInstalledExtension(id)) { |
| 195 // The extension ID does not correspond to any extension, whether crashed |
| 196 // or not. |
| 197 *error = base::StringPrintf("Extension %s is not installed.", |
| 198 id.c_str()); |
| 199 return false; |
| 200 } |
| 201 const Extension* installed_extension = |
| 202 service->GetExtensionById(id, include_disabled); |
| 203 if (!installed_extension) { |
| 204 *error = "Extension is disabled or has crashed."; |
| 205 return false; |
| 206 } |
| 207 *extension = installed_extension; |
| 208 return true; |
| 209 } |
| 210 |
| 211 } // namespace |
| 212 |
| 213 bool GetExtensionFromJSONArgs( |
| 214 base::DictionaryValue* args, |
| 215 const std::string& key, |
| 216 Profile* profile, |
| 217 const Extension** extension, |
| 218 std::string* error) { |
| 219 return GetExtensionFromJSONArgsHelper( |
| 220 args, key, profile, true /* include_disabled */, extension, error); |
| 221 } |
| 222 |
| 223 bool GetEnabledExtensionFromJSONArgs( |
| 224 base::DictionaryValue* args, |
| 225 const std::string& key, |
| 226 Profile* profile, |
| 227 const Extension** extension, |
| 228 std::string* error) { |
| 229 return GetExtensionFromJSONArgsHelper( |
| 230 args, key, profile, false /* include_disabled */, extension, error); |
| 231 } |
OLD | NEW |