OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/browser/autocomplete/autocomplete_match.h" | 10 #include "chrome/browser/autocomplete/autocomplete_match.h" |
10 #include "chrome/browser/automation/automation_provider.h" | 11 #include "chrome/browser/automation/automation_provider.h" |
| 12 #include "chrome/browser/ui/browser.h" |
| 13 #include "chrome/browser/ui/browser_list.h" |
11 #include "chrome/common/automation_messages.h" | 14 #include "chrome/common/automation_messages.h" |
12 | 15 |
13 namespace { | 16 namespace { |
14 | 17 |
15 // Util for creating a JSON error return string (dict with key | 18 // Util for creating a JSON error return string (dict with key |
16 // 'error' and error string value). No need to quote input. | 19 // 'error' and error string value). No need to quote input. |
17 std::string JSONErrorString(const std::string& err) { | 20 std::string JSONErrorString(const std::string& err) { |
18 std::string prefix = "{\"error\": \""; | 21 std::string prefix = "{\"error\": \""; |
19 std::string no_quote_err; | 22 std::string no_quote_err; |
20 std::string suffix = "\"}"; | 23 std::string suffix = "\"}"; |
21 | 24 |
22 base::JsonDoubleQuote(err, false, &no_quote_err); | 25 base::JsonDoubleQuote(err, false, &no_quote_err); |
23 return prefix + no_quote_err + suffix; | 26 return prefix + no_quote_err + suffix; |
24 } | 27 } |
25 | 28 |
| 29 Browser* GetBrowserAt(int index) { |
| 30 if (index < 0) |
| 31 return NULL; |
| 32 BrowserList::const_iterator iter = BrowserList::begin(); |
| 33 for (; (iter != BrowserList::end()) && (index > 0); ++iter, --index) {} |
| 34 if (iter == BrowserList::end()) |
| 35 return NULL; |
| 36 return *iter; |
| 37 } |
| 38 |
| 39 TabContents* GetTabContentsAt(int browser_index, int tab_index) { |
| 40 if (tab_index < 0) |
| 41 return NULL; |
| 42 Browser* browser = GetBrowserAt(browser_index); |
| 43 if (!browser || tab_index >= browser->tab_count()) |
| 44 return NULL; |
| 45 return browser->GetTabContentsAt(tab_index); |
| 46 } |
| 47 |
26 } // namespace | 48 } // namespace |
27 | 49 |
28 AutomationJSONReply::AutomationJSONReply(AutomationProvider* provider, | 50 AutomationJSONReply::AutomationJSONReply(AutomationProvider* provider, |
29 IPC::Message* reply_message) | 51 IPC::Message* reply_message) |
30 : provider_(provider), | 52 : provider_(provider), |
31 message_(reply_message) { | 53 message_(reply_message) { |
32 } | 54 } |
33 | 55 |
34 AutomationJSONReply::~AutomationJSONReply() { | 56 AutomationJSONReply::~AutomationJSONReply() { |
35 DCHECK(!message_) << "JSON automation request not replied!"; | 57 DCHECK(!message_) << "JSON automation request not replied!"; |
(...skipping 11 matching lines...) Expand all Loading... |
47 } | 69 } |
48 | 70 |
49 void AutomationJSONReply::SendError(const std::string& error_message) { | 71 void AutomationJSONReply::SendError(const std::string& error_message) { |
50 DCHECK(message_) << "Resending reply for JSON automation request"; | 72 DCHECK(message_) << "Resending reply for JSON automation request"; |
51 std::string json_string = JSONErrorString(error_message); | 73 std::string json_string = JSONErrorString(error_message); |
52 AutomationMsg_SendJSONRequest::WriteReplyParams( | 74 AutomationMsg_SendJSONRequest::WriteReplyParams( |
53 message_, json_string, false); | 75 message_, json_string, false); |
54 provider_->Send(message_); | 76 provider_->Send(message_); |
55 message_ = NULL; | 77 message_ = NULL; |
56 } | 78 } |
| 79 |
| 80 bool GetBrowserFromJSONArgs( |
| 81 DictionaryValue* args, |
| 82 Browser** browser, |
| 83 std::string* error) { |
| 84 int browser_index; |
| 85 if (!args->GetInteger("windex", &browser_index)) { |
| 86 *error = "'windex' missing or invalid"; |
| 87 return false; |
| 88 } |
| 89 *browser = GetBrowserAt(browser_index); |
| 90 if (!*browser) { |
| 91 *error = "Cannot locate browser from given index"; |
| 92 return false; |
| 93 } |
| 94 return true; |
| 95 } |
| 96 |
| 97 bool GetTabFromJSONArgs( |
| 98 DictionaryValue* args, |
| 99 TabContents** tab, |
| 100 std::string* error) { |
| 101 int browser_index, tab_index; |
| 102 if (!args->GetInteger("windex", &browser_index)) { |
| 103 *error = "'windex' missing or invalid"; |
| 104 return false; |
| 105 } |
| 106 if (!args->GetInteger("tab_index", &tab_index)) { |
| 107 *error = "'tab_index' missing or invalid"; |
| 108 return false; |
| 109 } |
| 110 *tab = GetTabContentsAt(browser_index, tab_index); |
| 111 if (!*tab) { |
| 112 *error = "Cannot locate tab from given indices"; |
| 113 return false; |
| 114 } |
| 115 return true; |
| 116 } |
| 117 |
| 118 bool GetBrowserAndTabFromJSONArgs( |
| 119 DictionaryValue* args, |
| 120 Browser** browser, |
| 121 TabContents** tab, |
| 122 std::string* error) { |
| 123 return GetBrowserFromJSONArgs(args, browser, error) && |
| 124 GetTabFromJSONArgs(args, tab, error); |
| 125 } |
OLD | NEW |