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.
5 #include "chrome/browser/automation/automation_provider_json.h"
7 #include "base/json/json_writer.h"
8 #include "base/json/string_escape.h"
9 #include "base/values.h"
10 #include "chrome/browser/autocomplete/autocomplete_match.h"
11 #include "chrome/browser/automation/automation_provider.h"
12 #include "chrome/browser/automation/automation_util.h"
13 #include "chrome/browser/extensions/extension_service.h"
14 #include "chrome/browser/extensions/extension_system.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/common/automation_messages.h"
17 #include "content/public/browser/web_contents.h"
18 #include "extensions/common/extension.h"
20 using content::WebContents
;
22 AutomationJSONReply::AutomationJSONReply(AutomationProvider
* provider
,
23 IPC::Message
* reply_message
)
24 : provider_(provider
),
25 message_(reply_message
) {
28 AutomationJSONReply::~AutomationJSONReply() {
29 DCHECK(!message_
) << "JSON automation request not replied!";
32 void AutomationJSONReply::SendSuccess(const base::Value
* value
) {
33 DCHECK(message_
) << "Resending reply for JSON automation request";
34 std::string json_string
= "{}";
36 base::JSONWriter::Write(value
, &json_string
);
37 AutomationMsg_SendJSONRequest::WriteReplyParams(
38 message_
, json_string
, true);
39 provider_
->Send(message_
);
43 void AutomationJSONReply::SendError(const std::string
& error_message
) {
44 DCHECK(message_
) << "Resending reply for JSON automation request";
46 base::DictionaryValue dict
;
47 dict
.SetString("error", error_message
);
49 base::JSONWriter::Write(&dict
, &json
);
51 AutomationMsg_SendJSONRequest::WriteReplyParams(message_
, json
, false);
52 provider_
->Send(message_
);
56 bool GetBrowserFromJSONArgs(
57 base::DictionaryValue
* args
,
61 if (!args
->GetInteger("windex", &browser_index
)) {
62 *error
= "'windex' missing or invalid";
65 *browser
= automation_util::GetBrowserAt(browser_index
);
67 *error
= "Cannot locate browser from given index";
73 bool GetTabFromJSONArgs(
74 base::DictionaryValue
* args
,
77 int browser_index
, tab_index
;
78 if (!args
->GetInteger("windex", &browser_index
)) {
79 *error
= "'windex' missing or invalid";
82 if (!args
->GetInteger("tab_index", &tab_index
)) {
83 *error
= "'tab_index' missing or invalid";
86 *tab
= automation_util::GetWebContentsAt(browser_index
, tab_index
);
88 *error
= "Cannot locate tab from given indices";
94 bool GetBrowserAndTabFromJSONArgs(
95 base::DictionaryValue
* args
,
99 return GetBrowserFromJSONArgs(args
, browser
, error
) &&
100 GetTabFromJSONArgs(args
, tab
, error
);
103 bool GetRenderViewFromJSONArgs(
104 base::DictionaryValue
* args
,
106 content::RenderViewHost
** rvh
,
107 std::string
* error
) {
108 WebContents
* tab
= NULL
;
109 if (!GetTabFromJSONArgs(args
, &tab
, error
))
111 *rvh
= tab
->GetRenderViewHost();
117 bool GetExtensionFromJSONArgsHelper(
118 base::DictionaryValue
* args
,
119 const std::string
& key
,
121 bool include_disabled
,
122 const extensions::Extension
** extension
,
123 std::string
* error
) {
125 if (!args
->GetString(key
, &id
)) {
126 *error
= base::StringPrintf("Missing or invalid key: %s", key
.c_str());
129 ExtensionService
* service
= extensions::ExtensionSystem::Get(profile
)->
132 *error
= "No extensions service.";
135 if (!service
->GetInstalledExtension(id
)) {
136 // The extension ID does not correspond to any extension, whether crashed
138 *error
= base::StringPrintf("Extension %s is not installed.",
142 const extensions::Extension
* installed_extension
=
143 service
->GetExtensionById(id
, include_disabled
);
144 if (!installed_extension
) {
145 *error
= "Extension is disabled or has crashed.";
148 *extension
= installed_extension
;
154 bool GetExtensionFromJSONArgs(
155 base::DictionaryValue
* args
,
156 const std::string
& key
,
158 const extensions::Extension
** extension
,
159 std::string
* error
) {
160 return GetExtensionFromJSONArgsHelper(
161 args
, key
, profile
, true /* include_disabled */, extension
, error
);
164 bool GetEnabledExtensionFromJSONArgs(
165 base::DictionaryValue
* args
,
166 const std::string
& key
,
168 const extensions::Extension
** extension
,
169 std::string
* error
) {
170 return GetExtensionFromJSONArgsHelper(
171 args
, key
, profile
, false /* include_disabled */, extension
, error
);