1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/automation/automation_provider_json.h"
#include "base/json/json_writer.h"
#include "base/json/string_escape.h"
#include "base/values.h"
#include "chrome/browser/autocomplete/autocomplete_match.h"
#include "chrome/browser/automation/automation_provider.h"
#include "chrome/browser/automation/automation_util.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_system.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/automation_id.h"
#include "chrome/common/automation_messages.h"
#include "chrome/common/extensions/extension.h"
#include "content/public/browser/web_contents.h"
using automation::Error;
using automation::ErrorCode;
using content::WebContents;
AutomationJSONReply::AutomationJSONReply(AutomationProvider* provider,
IPC::Message* reply_message)
: provider_(provider),
message_(reply_message) {
}
AutomationJSONReply::~AutomationJSONReply() {
DCHECK(!message_) << "JSON automation request not replied!";
}
void AutomationJSONReply::SendSuccess(const Value* value) {
DCHECK(message_) << "Resending reply for JSON automation request";
std::string json_string = "{}";
if (value)
base::JSONWriter::Write(value, &json_string);
AutomationMsg_SendJSONRequest::WriteReplyParams(
message_, json_string, true);
provider_->Send(message_);
message_ = NULL;
}
void AutomationJSONReply::SendError(const std::string& error_message) {
SendError(Error(error_message));
}
void AutomationJSONReply::SendErrorCode(ErrorCode code) {
SendError(Error(code));
}
void AutomationJSONReply::SendError(const Error& error) {
DCHECK(message_) << "Resending reply for JSON automation request";
base::DictionaryValue dict;
dict.SetString("error", error.message());
dict.SetInteger("code", error.code());
std::string json;
base::JSONWriter::Write(&dict, &json);
AutomationMsg_SendJSONRequest::WriteReplyParams(message_, json, false);
provider_->Send(message_);
message_ = NULL;
}
bool GetBrowserFromJSONArgs(
DictionaryValue* args,
Browser** browser,
std::string* error) {
if (args->HasKey("auto_id")) {
AutomationId id;
if (!GetAutomationIdFromJSONArgs(args, "auto_id", &id, error))
return false;
WebContents* tab;
if (!automation_util::GetTabForId(id, &tab)) {
*error = "'auto_id' does not refer to an open tab";
return false;
}
Browser* container = automation_util::GetBrowserForTab(tab);
if (!container) {
*error = "tab does not belong to an open browser";
return false;
}
*browser = container;
} else {
int browser_index;
if (!args->GetInteger("windex", &browser_index)) {
*error = "'windex' missing or invalid";
return false;
}
*browser = automation_util::GetBrowserAt(browser_index);
if (!*browser) {
*error = "Cannot locate browser from given index";
return false;
}
}
return true;
}
bool GetTabFromJSONArgs(
DictionaryValue* args,
WebContents** tab,
std::string* error) {
if (args->HasKey("auto_id")) {
AutomationId id;
if (!GetAutomationIdFromJSONArgs(args, "auto_id", &id, error))
return false;
if (!automation_util::GetTabForId(id, tab)) {
*error = "'auto_id' does not refer to an open tab";
return false;
}
} else {
int browser_index, tab_index;
if (!args->GetInteger("windex", &browser_index)) {
*error = "'windex' missing or invalid";
return false;
}
if (!args->GetInteger("tab_index", &tab_index)) {
*error = "'tab_index' missing or invalid";
return false;
}
*tab = automation_util::GetWebContentsAt(browser_index, tab_index);
if (!*tab) {
*error = "Cannot locate tab from given indices";
return false;
}
}
return true;
}
bool GetBrowserAndTabFromJSONArgs(
DictionaryValue* args,
Browser** browser,
WebContents** tab,
std::string* error) {
return GetBrowserFromJSONArgs(args, browser, error) &&
GetTabFromJSONArgs(args, tab, error);
}
bool GetAutomationIdFromJSONArgs(
DictionaryValue* args,
const std::string& key,
AutomationId* id,
std::string* error) {
Value* id_value;
if (!args->Get(key, &id_value)) {
*error = base::StringPrintf("Missing parameter '%s'", key.c_str());
return false;
}
return AutomationId::FromValue(id_value, id, error);
}
bool GetRenderViewFromJSONArgs(
DictionaryValue* args,
Profile* profile,
content::RenderViewHost** rvh,
std::string* error) {
Value* id_value;
if (args->Get("auto_id", &id_value)) {
AutomationId id;
if (!AutomationId::FromValue(id_value, &id, error))
return false;
if (!automation_util::GetRenderViewForId(id, profile, rvh)) {
*error = "ID does not correspond to an open view";
return false;
}
} else {
// If the render view id is not specified, check for browser/tab indices.
WebContents* tab = NULL;
if (!GetTabFromJSONArgs(args, &tab, error))
return false;
*rvh = tab->GetRenderViewHost();
}
return true;
}
namespace {
bool GetExtensionFromJSONArgsHelper(
base::DictionaryValue* args,
const std::string& key,
Profile* profile,
bool include_disabled,
const extensions::Extension** extension,
std::string* error) {
std::string id;
if (!args->GetString(key, &id)) {
*error = base::StringPrintf("Missing or invalid key: %s", key.c_str());
return false;
}
ExtensionService* service = extensions::ExtensionSystem::Get(profile)->
extension_service();
if (!service) {
*error = "No extensions service.";
return false;
}
if (!service->GetInstalledExtension(id)) {
// The extension ID does not correspond to any extension, whether crashed
// or not.
*error = base::StringPrintf("Extension %s is not installed.",
id.c_str());
return false;
}
const extensions::Extension* installed_extension =
service->GetExtensionById(id, include_disabled);
if (!installed_extension) {
*error = "Extension is disabled or has crashed.";
return false;
}
*extension = installed_extension;
return true;
}
} // namespace
bool GetExtensionFromJSONArgs(
base::DictionaryValue* args,
const std::string& key,
Profile* profile,
const extensions::Extension** extension,
std::string* error) {
return GetExtensionFromJSONArgsHelper(
args, key, profile, true /* include_disabled */, extension, error);
}
bool GetEnabledExtensionFromJSONArgs(
base::DictionaryValue* args,
const std::string& key,
Profile* profile,
const extensions::Extension** extension,
std::string* error) {
return GetExtensionFromJSONArgsHelper(
args, key, profile, false /* include_disabled */, extension, error);
}
|