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/common/automation_id.h" |
13 #include "chrome/common/automation_messages.h" | 14 #include "chrome/common/automation_messages.h" |
14 | 15 |
15 namespace { | 16 namespace { |
16 | 17 |
17 // Util for creating a JSON error return string (dict with key | 18 // Util for creating a JSON error return string (dict with key |
18 // 'error' and error string value). No need to quote input. | 19 // 'error' and error string value). No need to quote input. |
19 std::string JSONErrorString(const std::string& err) { | 20 std::string JSONErrorString(const std::string& err) { |
20 std::string prefix = "{\"error\": \""; | 21 std::string prefix = "{\"error\": \""; |
21 std::string no_quote_err; | 22 std::string no_quote_err; |
22 std::string suffix = "\"}"; | 23 std::string suffix = "\"}"; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 AutomationMsg_SendJSONRequest::WriteReplyParams( | 55 AutomationMsg_SendJSONRequest::WriteReplyParams( |
55 message_, json_string, false); | 56 message_, json_string, false); |
56 provider_->Send(message_); | 57 provider_->Send(message_); |
57 message_ = NULL; | 58 message_ = NULL; |
58 } | 59 } |
59 | 60 |
60 bool GetBrowserFromJSONArgs( | 61 bool GetBrowserFromJSONArgs( |
61 DictionaryValue* args, | 62 DictionaryValue* args, |
62 Browser** browser, | 63 Browser** browser, |
63 std::string* error) { | 64 std::string* error) { |
64 int browser_index; | 65 if (args->HasKey("tab_id")) { |
65 if (!args->GetInteger("windex", &browser_index)) { | 66 AutomationId id; |
66 *error = "'windex' missing or invalid"; | 67 if (!GetAutomationIdFromJSONArgs(args, "tab_id", &id, error)) |
67 return false; | 68 return false; |
68 } | 69 TabContents* tab; |
69 *browser = automation_util::GetBrowserAt(browser_index); | 70 if (!automation_util::GetTabForId(id, &tab)) { |
70 if (!*browser) { | 71 *error = "'tab_id' does not refer to an open tab"; |
71 *error = "Cannot locate browser from given index"; | 72 return false; |
72 return false; | 73 } |
| 74 Browser* container = automation_util::GetBrowserForTab(tab); |
| 75 if (!container) { |
| 76 *error = "tab does not belong to an open browser"; |
| 77 return false; |
| 78 } |
| 79 *browser = container; |
| 80 } else { |
| 81 int browser_index; |
| 82 if (!args->GetInteger("windex", &browser_index)) { |
| 83 *error = "'windex' missing or invalid"; |
| 84 return false; |
| 85 } |
| 86 *browser = automation_util::GetBrowserAt(browser_index); |
| 87 if (!*browser) { |
| 88 *error = "Cannot locate browser from given index"; |
| 89 return false; |
| 90 } |
73 } | 91 } |
74 return true; | 92 return true; |
75 } | 93 } |
76 | 94 |
77 bool GetTabFromJSONArgs( | 95 bool GetTabFromJSONArgs( |
78 DictionaryValue* args, | 96 DictionaryValue* args, |
79 TabContents** tab, | 97 TabContents** tab, |
80 std::string* error) { | 98 std::string* error) { |
81 int browser_index, tab_index; | 99 if (args->HasKey("tab_id")) { |
82 if (!args->GetInteger("windex", &browser_index)) { | 100 AutomationId id; |
83 *error = "'windex' missing or invalid"; | 101 if (!GetAutomationIdFromJSONArgs(args, "tab_id", &id, error)) |
84 return false; | 102 return false; |
85 } | 103 if (!automation_util::GetTabForId(id, tab)) { |
86 if (!args->GetInteger("tab_index", &tab_index)) { | 104 *error = "'tab_id' does not refer to an open tab"; |
87 *error = "'tab_index' missing or invalid"; | 105 return false; |
88 return false; | 106 } |
89 } | 107 } else { |
90 *tab = automation_util::GetTabContentsAt(browser_index, tab_index); | 108 int browser_index, tab_index; |
91 if (!*tab) { | 109 if (!args->GetInteger("windex", &browser_index)) { |
92 *error = "Cannot locate tab from given indices"; | 110 *error = "'windex' missing or invalid"; |
93 return false; | 111 return false; |
| 112 } |
| 113 if (!args->GetInteger("tab_index", &tab_index)) { |
| 114 *error = "'tab_index' missing or invalid"; |
| 115 return false; |
| 116 } |
| 117 *tab = automation_util::GetTabContentsAt(browser_index, tab_index); |
| 118 if (!*tab) { |
| 119 *error = "Cannot locate tab from given indices"; |
| 120 return false; |
| 121 } |
94 } | 122 } |
95 return true; | 123 return true; |
96 } | 124 } |
97 | 125 |
98 bool GetBrowserAndTabFromJSONArgs( | 126 bool GetBrowserAndTabFromJSONArgs( |
99 DictionaryValue* args, | 127 DictionaryValue* args, |
100 Browser** browser, | 128 Browser** browser, |
101 TabContents** tab, | 129 TabContents** tab, |
102 std::string* error) { | 130 std::string* error) { |
103 return GetBrowserFromJSONArgs(args, browser, error) && | 131 return GetBrowserFromJSONArgs(args, browser, error) && |
104 GetTabFromJSONArgs(args, tab, error); | 132 GetTabFromJSONArgs(args, tab, error); |
105 } | 133 } |
| 134 |
| 135 bool GetAutomationIdFromJSONArgs( |
| 136 DictionaryValue* args, |
| 137 const std::string& key_name, |
| 138 AutomationId* id, |
| 139 std::string* error) { |
| 140 Value* id_value; |
| 141 if (!args->Get(key_name, &id_value)) { |
| 142 *error = base::StringPrintf("Missing or invalid '%s'", key_name.c_str()); |
| 143 return false; |
| 144 } |
| 145 return AutomationId::FromValue(id_value, id, error); |
| 146 } |
| 147 |
| 148 bool GetRenderViewFromJSONArgs( |
| 149 DictionaryValue* args, |
| 150 Profile* profile, |
| 151 RenderViewHost** rvh, |
| 152 std::string* error) { |
| 153 Value* id_value; |
| 154 if (args->Get("view_id", &id_value)) { |
| 155 AutomationId id; |
| 156 if (!AutomationId::FromValue(id_value, &id, error)) |
| 157 return false; |
| 158 if (!automation_util::GetRenderViewForId(id, profile, rvh)) { |
| 159 *error = "ID does not correspond to an open view"; |
| 160 return false; |
| 161 } |
| 162 } else { |
| 163 // If the render view id is not specified, check for browser/tab indices. |
| 164 TabContents* tab = NULL; |
| 165 if (!GetTabFromJSONArgs(args, &tab, error)) |
| 166 return false; |
| 167 *rvh = tab->render_view_host(); |
| 168 } |
| 169 return true; |
| 170 } |
OLD | NEW |