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_host.h" |
| 14 #include "chrome/browser/extensions/extension_process_manager.h" |
| 15 #include "chrome/browser/extensions/extension_service.h" |
| 16 #include "chrome/browser/sessions/restore_tab_helper.h" |
| 17 #include "chrome/browser/sessions/session_id.h" |
| 18 #include "chrome/browser/ui/browser.h" |
| 19 #include "chrome/browser/ui/browser_list.h" |
| 20 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
13 #include "chrome/common/automation_messages.h" | 21 #include "chrome/common/automation_messages.h" |
| 22 #include "chrome/common/chrome_view_types.h" |
| 23 #include "chrome/common/extensions/extension.h" |
| 24 #include "content/browser/tab_contents/tab_contents.h" |
14 | 25 |
15 namespace { | 26 namespace { |
16 | 27 |
17 // Util for creating a JSON error return string (dict with key | 28 // Util for creating a JSON error return string (dict with key |
18 // 'error' and error string value). No need to quote input. | 29 // 'error' and error string value). No need to quote input. |
19 std::string JSONErrorString(const std::string& err) { | 30 std::string JSONErrorString(const std::string& err) { |
20 std::string prefix = "{\"error\": \""; | 31 std::string prefix = "{\"error\": \""; |
21 std::string no_quote_err; | 32 std::string no_quote_err; |
22 std::string suffix = "\"}"; | 33 std::string suffix = "\"}"; |
23 | 34 |
(...skipping 26 matching lines...) Expand all Loading... |
50 | 61 |
51 void AutomationJSONReply::SendError(const std::string& error_message) { | 62 void AutomationJSONReply::SendError(const std::string& error_message) { |
52 DCHECK(message_) << "Resending reply for JSON automation request"; | 63 DCHECK(message_) << "Resending reply for JSON automation request"; |
53 std::string json_string = JSONErrorString(error_message); | 64 std::string json_string = JSONErrorString(error_message); |
54 AutomationMsg_SendJSONRequest::WriteReplyParams( | 65 AutomationMsg_SendJSONRequest::WriteReplyParams( |
55 message_, json_string, false); | 66 message_, json_string, false); |
56 provider_->Send(message_); | 67 provider_->Send(message_); |
57 message_ = NULL; | 68 message_ = NULL; |
58 } | 69 } |
59 | 70 |
| 71 // static |
| 72 bool AutomationId::FromValue( |
| 73 Value* value, AutomationId* id, std::string* error) { |
| 74 DictionaryValue* dict; |
| 75 if (!value->GetAsDictionary(&dict)) { |
| 76 *error = "automation ID must be a dictionary"; |
| 77 return false; |
| 78 } |
| 79 int type; |
| 80 if (!dict->GetInteger("type", &type)) { |
| 81 *error = "automation ID 'type' missing or invalid"; |
| 82 return false; |
| 83 } |
| 84 std::string type_id; |
| 85 if (!dict->GetString("id", &type_id)) { |
| 86 *error = "automation ID 'type_id' missing or invalid"; |
| 87 return false; |
| 88 } |
| 89 *id = AutomationId(static_cast<Type>(type), type_id); |
| 90 return true; |
| 91 } |
| 92 |
| 93 AutomationId::AutomationId() : type_(kInvalidType) { } |
| 94 |
| 95 AutomationId::AutomationId(Type type, const std::string& id) |
| 96 : type_(type), id_(id) { } |
| 97 |
| 98 Value* AutomationId::ToValue() const { |
| 99 DictionaryValue* dict = new DictionaryValue(); |
| 100 dict->SetInteger("type", type_); |
| 101 dict->SetString("id", id_); |
| 102 return dict; |
| 103 } |
| 104 |
| 105 bool AutomationId::is_valid() const { |
| 106 return type_ != kInvalidType; |
| 107 } |
| 108 |
| 109 AutomationId::Type AutomationId::type() const { |
| 110 return type_; |
| 111 } |
| 112 |
| 113 const std::string& AutomationId::id() const { |
| 114 return id_; |
| 115 } |
| 116 |
60 bool GetBrowserFromJSONArgs( | 117 bool GetBrowserFromJSONArgs( |
61 DictionaryValue* args, | 118 DictionaryValue* args, |
62 Browser** browser, | 119 Browser** browser, |
63 std::string* error) { | 120 std::string* error) { |
64 int browser_index; | 121 int browser_index; |
65 if (!args->GetInteger("windex", &browser_index)) { | 122 if (!args->GetInteger("windex", &browser_index)) { |
66 *error = "'windex' missing or invalid"; | 123 *error = "'windex' missing or invalid"; |
67 return false; | 124 return false; |
68 } | 125 } |
69 *browser = automation_util::GetBrowserAt(browser_index); | 126 *browser = automation_util::GetBrowserAt(browser_index); |
(...skipping 26 matching lines...) Expand all Loading... |
96 } | 153 } |
97 | 154 |
98 bool GetBrowserAndTabFromJSONArgs( | 155 bool GetBrowserAndTabFromJSONArgs( |
99 DictionaryValue* args, | 156 DictionaryValue* args, |
100 Browser** browser, | 157 Browser** browser, |
101 TabContents** tab, | 158 TabContents** tab, |
102 std::string* error) { | 159 std::string* error) { |
103 return GetBrowserFromJSONArgs(args, browser, error) && | 160 return GetBrowserFromJSONArgs(args, browser, error) && |
104 GetTabFromJSONArgs(args, tab, error); | 161 GetTabFromJSONArgs(args, tab, error); |
105 } | 162 } |
| 163 |
| 164 bool GetAutomationIdFromJSONArgs( |
| 165 DictionaryValue* args, |
| 166 AutomationId* id, |
| 167 std::string* error) { |
| 168 Value* id_value; |
| 169 if (!args->Get("view_id", &id_value)) { |
| 170 *error = "Missing or invalid 'view_id'"; |
| 171 return false; |
| 172 } |
| 173 return AutomationId::FromValue(id_value, id, error); |
| 174 } |
| 175 |
| 176 bool GetRenderViewFromJSONArgs( |
| 177 DictionaryValue* args, |
| 178 Profile* profile, |
| 179 RenderViewHost** rvh, |
| 180 std::string* error) { |
| 181 Value* id_value; |
| 182 if (args->Get("view_id", &id_value)) { |
| 183 AutomationId id; |
| 184 if (!AutomationId::FromValue(id_value, &id, error)) |
| 185 return false; |
| 186 if (!automation_util::GetRenderViewForId(id, profile, rvh)) { |
| 187 *error = "ID does not correspond to an open view"; |
| 188 return false; |
| 189 } |
| 190 } else { |
| 191 // If the render view id is not specified, check for browser/tab indices. |
| 192 TabContents* tab = NULL; |
| 193 if (!GetTabFromJSONArgs(args, &tab, error)) |
| 194 return false; |
| 195 *rvh = tab->render_view_host(); |
| 196 } |
| 197 return true; |
| 198 } |
OLD | NEW |