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/test/webdriver/commands/chrome_commands.h" | 5 #include "chrome/test/webdriver/commands/chrome_commands.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
| 11 #include "base/stringprintf.h" |
11 #include "chrome/test/automation/value_conversion_util.h" | 12 #include "chrome/test/automation/value_conversion_util.h" |
12 #include "chrome/test/webdriver/commands/response.h" | 13 #include "chrome/test/webdriver/commands/response.h" |
13 #include "chrome/test/webdriver/webdriver_error.h" | 14 #include "chrome/test/webdriver/webdriver_error.h" |
14 #include "chrome/test/webdriver/webdriver_session.h" | 15 #include "chrome/test/webdriver/webdriver_session.h" |
| 16 #include "chrome/test/webdriver/webdriver_util.h" |
| 17 |
| 18 using base::DictionaryValue; |
| 19 using base::ListValue; |
| 20 using base::Value; |
15 | 21 |
16 namespace webdriver { | 22 namespace webdriver { |
17 | 23 |
18 ExtensionsCommand::ExtensionsCommand( | 24 ExtensionsCommand::ExtensionsCommand( |
19 const std::vector<std::string>& path_segments, | 25 const std::vector<std::string>& path_segments, |
20 const base::DictionaryValue* const parameters) | 26 const DictionaryValue* const parameters) |
21 : WebDriverCommand(path_segments, parameters) {} | 27 : WebDriverCommand(path_segments, parameters) {} |
22 | 28 |
23 ExtensionsCommand::~ExtensionsCommand() {} | 29 ExtensionsCommand::~ExtensionsCommand() {} |
24 | 30 |
25 bool ExtensionsCommand::DoesGet() { | 31 bool ExtensionsCommand::DoesGet() { |
26 return true; | 32 return true; |
27 } | 33 } |
28 | 34 |
29 bool ExtensionsCommand::DoesPost() { | 35 bool ExtensionsCommand::DoesPost() { |
30 return true; | 36 return true; |
31 } | 37 } |
32 | 38 |
33 void ExtensionsCommand::ExecuteGet(Response* const response) { | 39 void ExtensionsCommand::ExecuteGet(Response* const response) { |
34 std::vector<std::string> extension_ids; | 40 ListValue extensions_list; |
35 Error* error = session_->GetInstalledExtensions(&extension_ids); | 41 Error* error = session_->GetExtensionsInfo(&extensions_list); |
36 if (error) { | 42 if (error) { |
37 response->SetError(error); | 43 response->SetError(error); |
38 return; | 44 return; |
39 } | 45 } |
40 base::ListValue* extensions = new base::ListValue(); | 46 |
41 for (size_t i = 0; i < extension_ids.size(); ++i) | 47 ListValue id_list; |
42 extensions->Append(CreateValueFrom(extension_ids[i])); | 48 for (size_t i = 0; i < extensions_list.GetSize(); ++i) { |
43 response->SetValue(extensions); | 49 DictionaryValue* extension_dict; |
| 50 if (!extensions_list.GetDictionary(i, &extension_dict)) { |
| 51 response->SetError( |
| 52 new Error(kUnknownError, "Invalid extension dictionary")); |
| 53 return; |
| 54 } |
| 55 bool is_component; |
| 56 if (!extension_dict->GetBoolean("is_component", &is_component)) { |
| 57 response->SetError( |
| 58 new Error(kUnknownError, "Missing or invalid 'is_component'")); |
| 59 return; |
| 60 } |
| 61 if (is_component) |
| 62 continue; |
| 63 |
| 64 std::string extension_id; |
| 65 if (!extension_dict->GetString("id", &extension_id)) { |
| 66 response->SetError(new Error(kUnknownError, "Missing or invalid 'id'")); |
| 67 return; |
| 68 } |
| 69 |
| 70 id_list.Append(Value::CreateStringValue(extension_id)); |
| 71 } |
| 72 |
| 73 response->SetValue(id_list.DeepCopy()); |
44 } | 74 } |
45 | 75 |
46 void ExtensionsCommand::ExecutePost(Response* const response) { | 76 void ExtensionsCommand::ExecutePost(Response* const response) { |
47 FilePath::StringType path_string; | 77 FilePath::StringType path_string; |
48 if (!GetStringParameter("path", &path_string)) { | 78 if (!GetStringParameter("path", &path_string)) { |
49 response->SetError(new Error(kUnknownError, "'path' missing or invalid")); | 79 response->SetError(new Error(kBadRequest, "'path' missing or invalid")); |
50 return; | 80 return; |
51 } | 81 } |
52 | 82 |
53 std::string extension_id; | 83 std::string extension_id; |
54 Error* error = session_->InstallExtension( | 84 Error* error = session_->InstallExtension( |
55 FilePath(path_string), &extension_id); | 85 FilePath(path_string), &extension_id); |
56 if (error) { | 86 if (error) { |
57 response->SetError(error); | 87 response->SetError(error); |
58 return; | 88 return; |
59 } | 89 } |
60 response->SetValue(CreateValueFrom(extension_id)); | 90 response->SetValue(Value::CreateStringValue(extension_id)); |
| 91 } |
| 92 |
| 93 ExtensionCommand::ExtensionCommand( |
| 94 const std::vector<std::string>& path_segments, |
| 95 const DictionaryValue* const parameters) |
| 96 : WebDriverCommand(path_segments, parameters) {} |
| 97 |
| 98 ExtensionCommand::~ExtensionCommand() {} |
| 99 |
| 100 bool ExtensionCommand::Init(Response* const response) { |
| 101 if (!WebDriverCommand::Init(response)) |
| 102 return false; |
| 103 |
| 104 // Path: "/session/$sessionId/chrome/extension/$id". |
| 105 extension_id_ = GetPathVariable(5); |
| 106 if (extension_id_.empty()) { |
| 107 response->SetError(new Error(kBadRequest, "Invalid extension ID")); |
| 108 return false; |
| 109 } |
| 110 return true; |
| 111 } |
| 112 |
| 113 bool ExtensionCommand::DoesGet() { |
| 114 return true; |
| 115 } |
| 116 |
| 117 bool ExtensionCommand::DoesPost() { |
| 118 return true; |
| 119 } |
| 120 |
| 121 bool ExtensionCommand::DoesDelete() { |
| 122 return true; |
| 123 } |
| 124 |
| 125 void ExtensionCommand::ExecuteGet(Response* const response) { |
| 126 ListValue extensions_list; |
| 127 Error* error = session_->GetExtensionsInfo(&extensions_list); |
| 128 if (error) { |
| 129 response->SetError(error); |
| 130 return; |
| 131 } |
| 132 |
| 133 bool found = false; |
| 134 DictionaryValue extension; |
| 135 for (size_t i = 0; i < extensions_list.GetSize(); ++i) { |
| 136 DictionaryValue* extension_dict; |
| 137 if (!extensions_list.GetDictionary(i, &extension_dict)) { |
| 138 response->SetError( |
| 139 new Error(kUnknownError, "Invalid extension dictionary")); |
| 140 return; |
| 141 } |
| 142 std::string id; |
| 143 if (!extension_dict->GetString("id", &id)) { |
| 144 response->SetError( |
| 145 new Error(kUnknownError, "Missing extension ID")); |
| 146 return; |
| 147 } |
| 148 if (id == extension_id_) { |
| 149 found = true; |
| 150 extension.Swap(extension_dict); |
| 151 break; |
| 152 } |
| 153 } |
| 154 |
| 155 if (!found) { |
| 156 response->SetError( |
| 157 new Error(kUnknownError, "Extension is not installed")); |
| 158 return; |
| 159 } |
| 160 |
| 161 bool is_enabled; |
| 162 if (!extension.GetBoolean("is_enabled", &is_enabled)) { |
| 163 response->SetError( |
| 164 new Error(kUnknownError, "Missing or invalid 'is_enabled'")); |
| 165 return; |
| 166 } |
| 167 bool has_page_action; |
| 168 if (!extension.GetBoolean("has_page_action", &has_page_action)) { |
| 169 response->SetError( |
| 170 new Error(kUnknownError, "Missing or invalid 'is_enabled'")); |
| 171 return; |
| 172 } |
| 173 |
| 174 bool is_visible = false; |
| 175 if (is_enabled && has_page_action) { |
| 176 // Only check page action visibility if we are enabled with a page action. |
| 177 // Otherwise Chrome will throw an error saying the extension does not have |
| 178 // a page action. |
| 179 error = session_->IsPageActionVisible( |
| 180 session_->current_target().view_id, extension_id_, &is_visible); |
| 181 if (error) { |
| 182 response->SetError(error); |
| 183 return; |
| 184 } |
| 185 } |
| 186 |
| 187 extension.SetBoolean("is_page_action_visible", is_visible); |
| 188 response->SetValue(extension.DeepCopy()); |
| 189 } |
| 190 |
| 191 void ExtensionCommand::ExecutePost(Response* const response) { |
| 192 Error* error = NULL; |
| 193 if (HasParameter("enable")) { |
| 194 bool enable; |
| 195 if (!GetBooleanParameter("enable", &enable)) { |
| 196 response->SetError(new Error(kBadRequest, "'enable' must be a bool")); |
| 197 return; |
| 198 } |
| 199 error = session_->SetExtensionState(extension_id_, enable); |
| 200 } else if (HasParameter("click_button")) { |
| 201 std::string button; |
| 202 if (!GetStringParameter("click_button", &button)) { |
| 203 response->SetError( |
| 204 new Error(kBadRequest, "'click_button' must be a string")); |
| 205 return; |
| 206 } |
| 207 error = session_->ClickExtensionButton(extension_id_, |
| 208 button == "browser_action"); |
| 209 } else { |
| 210 error = new Error(kBadRequest, "Missing action parameter"); |
| 211 } |
| 212 |
| 213 if (error) { |
| 214 response->SetError(error); |
| 215 return; |
| 216 } |
| 217 } |
| 218 |
| 219 void ExtensionCommand::ExecuteDelete(Response* const response) { |
| 220 Error* error = session_->UninstallExtension(extension_id_); |
| 221 if (error) { |
| 222 response->SetError(error); |
| 223 return; |
| 224 } |
| 225 } |
| 226 |
| 227 ViewsCommand::ViewsCommand( |
| 228 const std::vector<std::string>& path_segments, |
| 229 const DictionaryValue* const parameters) |
| 230 : WebDriverCommand(path_segments, parameters) {} |
| 231 |
| 232 ViewsCommand::~ViewsCommand() {} |
| 233 |
| 234 bool ViewsCommand::DoesGet() { |
| 235 return true; |
| 236 } |
| 237 |
| 238 void ViewsCommand::ExecuteGet(Response* const response) { |
| 239 std::vector<WebViewInfo> views; |
| 240 Error* error = session_->GetViews(&views); |
| 241 if (error) { |
| 242 response->SetError(error); |
| 243 return; |
| 244 } |
| 245 ListValue* views_list = new ListValue(); |
| 246 for (size_t i = 0; i < views.size(); ++i) { |
| 247 DictionaryValue* dict = new DictionaryValue(); |
| 248 AutomationId id = views[i].view_id.GetId(); |
| 249 dict->SetString("handle", JsonStringify(id.ToValue())); |
| 250 dict->SetInteger("type", id.type()); |
| 251 if (!views[i].extension_id.empty()) |
| 252 dict->SetString("extension_id", views[i].extension_id); |
| 253 views_list->Append(dict); |
| 254 } |
| 255 response->SetValue(views_list); |
61 } | 256 } |
62 | 257 |
63 } // namespace webdriver | 258 } // namespace webdriver |
OLD | NEW |