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 || index >= static_cast<int>(BrowserList::size())) |
| 31 return NULL; |
| 32 return *(BrowserList::begin() + index); |
| 33 } |
| 34 |
| 35 TabContents* GetTabContentsAt(int browser_index, int tab_index) { |
| 36 if (tab_index < 0) |
| 37 return NULL; |
| 38 Browser* browser = GetBrowserAt(browser_index); |
| 39 if (!browser || tab_index >= browser->tab_count()) |
| 40 return NULL; |
| 41 return browser->GetTabContentsAt(tab_index); |
| 42 } |
| 43 |
26 } // namespace | 44 } // namespace |
27 | 45 |
28 AutomationJSONReply::AutomationJSONReply(AutomationProvider* provider, | 46 AutomationJSONReply::AutomationJSONReply(AutomationProvider* provider, |
29 IPC::Message* reply_message) | 47 IPC::Message* reply_message) |
30 : provider_(provider), | 48 : provider_(provider), |
31 message_(reply_message) { | 49 message_(reply_message) { |
32 } | 50 } |
33 | 51 |
34 AutomationJSONReply::~AutomationJSONReply() { | 52 AutomationJSONReply::~AutomationJSONReply() { |
35 DCHECK(!message_) << "JSON automation request not replied!"; | 53 DCHECK(!message_) << "JSON automation request not replied!"; |
(...skipping 11 matching lines...) Expand all Loading... |
47 } | 65 } |
48 | 66 |
49 void AutomationJSONReply::SendError(const std::string& error_message) { | 67 void AutomationJSONReply::SendError(const std::string& error_message) { |
50 DCHECK(message_) << "Resending reply for JSON automation request"; | 68 DCHECK(message_) << "Resending reply for JSON automation request"; |
51 std::string json_string = JSONErrorString(error_message); | 69 std::string json_string = JSONErrorString(error_message); |
52 AutomationMsg_SendJSONRequest::WriteReplyParams( | 70 AutomationMsg_SendJSONRequest::WriteReplyParams( |
53 message_, json_string, false); | 71 message_, json_string, false); |
54 provider_->Send(message_); | 72 provider_->Send(message_); |
55 message_ = NULL; | 73 message_ = NULL; |
56 } | 74 } |
| 75 |
| 76 bool GetBrowserFromJSONArgs( |
| 77 DictionaryValue* args, |
| 78 Browser** browser, |
| 79 std::string* error) { |
| 80 int browser_index; |
| 81 if (!args->GetInteger("windex", &browser_index)) { |
| 82 *error = "'windex' missing or invalid"; |
| 83 return false; |
| 84 } |
| 85 *browser = GetBrowserAt(browser_index); |
| 86 if (!*browser) { |
| 87 *error = "Cannot locate browser from given index"; |
| 88 return false; |
| 89 } |
| 90 return true; |
| 91 } |
| 92 |
| 93 bool GetTabFromJSONArgs( |
| 94 DictionaryValue* args, |
| 95 TabContents** tab, |
| 96 std::string* error) { |
| 97 int browser_index, tab_index; |
| 98 if (!args->GetInteger("windex", &browser_index)) { |
| 99 *error = "'windex' missing or invalid"; |
| 100 return false; |
| 101 } |
| 102 if (!args->GetInteger("tab_index", &tab_index)) { |
| 103 *error = "'tab_index' missing or invalid"; |
| 104 return false; |
| 105 } |
| 106 *tab = GetTabContentsAt(browser_index, tab_index); |
| 107 if (!*tab) { |
| 108 *error = "Cannot locate tab from given indices"; |
| 109 return false; |
| 110 } |
| 111 return true; |
| 112 } |
| 113 |
| 114 bool GetBrowserAndTabFromJSONArgs( |
| 115 DictionaryValue* args, |
| 116 Browser** browser, |
| 117 TabContents** tab, |
| 118 std::string* error) { |
| 119 return GetBrowserFromJSONArgs(args, browser, error) && |
| 120 GetTabFromJSONArgs(args, tab, error); |
| 121 } |
OLD | NEW |