Index: chrome/browser/automation/automation_provider_json.cc |
diff --git a/chrome/browser/automation/automation_provider_json.cc b/chrome/browser/automation/automation_provider_json.cc |
index 16fd8d41f810838c055c3a23e4c89255244ab65c..2c99580a8e0623f49b8fe39c9caac07bd9db066e 100644 |
--- a/chrome/browser/automation/automation_provider_json.cc |
+++ b/chrome/browser/automation/automation_provider_json.cc |
@@ -6,8 +6,11 @@ |
#include "base/json/json_writer.h" |
#include "base/json/string_escape.h" |
+#include "base/values.h" |
#include "chrome/browser/autocomplete/autocomplete_match.h" |
#include "chrome/browser/automation/automation_provider.h" |
+#include "chrome/browser/ui/browser.h" |
+#include "chrome/browser/ui/browser_list.h" |
#include "chrome/common/automation_messages.h" |
namespace { |
@@ -23,6 +26,21 @@ std::string JSONErrorString(const std::string& err) { |
return prefix + no_quote_err + suffix; |
} |
+Browser* GetBrowserAt(int index) { |
+ if (index < 0 || index >= static_cast<int>(BrowserList::size())) |
+ return NULL; |
+ return *(BrowserList::begin() + index); |
+} |
+ |
+TabContents* GetTabContentsAt(int browser_index, int tab_index) { |
+ if (tab_index < 0) |
+ return NULL; |
+ Browser* browser = GetBrowserAt(browser_index); |
+ if (!browser || tab_index >= browser->tab_count()) |
+ return NULL; |
+ return browser->GetTabContentsAt(tab_index); |
+} |
+ |
} // namespace |
AutomationJSONReply::AutomationJSONReply(AutomationProvider* provider, |
@@ -54,3 +72,50 @@ void AutomationJSONReply::SendError(const std::string& error_message) { |
provider_->Send(message_); |
message_ = NULL; |
} |
+ |
+bool GetBrowserFromJSONArgs( |
+ DictionaryValue* args, |
+ Browser** browser, |
+ std::string* error) { |
+ int browser_index; |
+ if (!args->GetInteger("windex", &browser_index)) { |
+ *error = "'windex' missing or invalid"; |
+ return false; |
+ } |
+ *browser = GetBrowserAt(browser_index); |
+ if (!*browser) { |
+ *error = "Cannot locate browser from given index"; |
+ return false; |
+ } |
+ return true; |
+} |
+ |
+bool GetTabFromJSONArgs( |
+ DictionaryValue* args, |
+ TabContents** tab, |
+ std::string* error) { |
+ int browser_index, tab_index; |
+ if (!args->GetInteger("windex", &browser_index)) { |
+ *error = "'windex' missing or invalid"; |
+ return false; |
+ } |
+ if (!args->GetInteger("tab_index", &tab_index)) { |
+ *error = "'tab_index' missing or invalid"; |
+ return false; |
+ } |
+ *tab = GetTabContentsAt(browser_index, tab_index); |
+ if (!*tab) { |
+ *error = "Cannot locate tab from given indices"; |
+ return false; |
+ } |
+ return true; |
+} |
+ |
+bool GetBrowserAndTabFromJSONArgs( |
+ DictionaryValue* args, |
+ Browser** browser, |
+ TabContents** tab, |
+ std::string* error) { |
+ return GetBrowserFromJSONArgs(args, browser, error) && |
+ GetTabFromJSONArgs(args, tab, error); |
+} |