Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(284)

Unified Diff: chrome/browser/automation/automation_util.cc

Issue 8790003: Allow the automation provider to accept an ID for performing render-view (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: ... Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/automation/automation_util.cc
diff --git a/chrome/browser/automation/automation_util.cc b/chrome/browser/automation/automation_util.cc
index 079b9404279e2da11cdef0d85927e2f79b6e7210..7d70826381259bc75ddd03267a6a1ac610f554b0 100644
--- a/chrome/browser/automation/automation_util.cc
+++ b/chrome/browser/automation/automation_util.cc
@@ -8,16 +8,26 @@
#include "base/bind.h"
#include "base/memory/scoped_ptr.h"
+#include "base/stringprintf.h"
+#include "base/string_number_conversions.h"
#include "base/synchronization/waitable_event.h"
#include "base/time.h"
#include "base/values.h"
#include "chrome/browser/automation/automation_provider.h"
#include "chrome/browser/automation/automation_provider_json.h"
+#include "chrome/browser/extensions/extension_host.h"
+#include "chrome/browser/extensions/extension_process_manager.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/profiles/profile.h"
+#include "chrome/browser/sessions/restore_tab_helper.h"
+#include "chrome/browser/sessions/session_id.h"
#include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog_queue.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
+#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
+#include "chrome/common/automation_id.h"
+#include "chrome/common/chrome_view_type.h"
+#include "chrome/common/extensions/extension.h"
#include "content/browser/renderer_host/render_view_host.h"
#include "content/browser/tab_contents/tab_contents.h"
#include "content/public/browser/browser_thread.h"
@@ -135,6 +145,18 @@ TabContents* GetTabContentsAt(int browser_index, int tab_index) {
return browser->GetTabContentsAt(tab_index);
}
+Browser* GetBrowserForTab(TabContents* tab) {
+ BrowserList::const_iterator browser_iter = BrowserList::begin();
+ for (; browser_iter != BrowserList::end(); ++browser_iter) {
+ Browser* browser = *browser_iter;
+ for (int tab_index = 0; tab_index < browser->tab_count(); ++tab_index) {
+ if (browser->GetTabContentsAt(tab_index) == tab)
+ return browser;
+ }
+ }
+ return NULL;
+}
+
net::URLRequestContextGetter* GetRequestContext(TabContents* contents) {
// Since we may be on the UI thread don't call GetURLRequestContext().
// Get the request context specific to the current TabContents and app.
@@ -381,4 +403,155 @@ bool SendErrorIfModalDialogActive(AutomationProvider* provider,
return active;
}
+AutomationId GetIdForTab(const TabContentsWrapper* tab) {
+ return AutomationId(
+ AutomationId::kTypeTab,
+ base::IntToString(tab->restore_tab_helper()->session_id().id()));
+}
+
+AutomationId GetIdForExtensionView(const ExtensionHost* ext_host) {
+ AutomationId::Type type;
+ switch (ext_host->extension_host_type()) {
+ case chrome::VIEW_TYPE_EXTENSION_POPUP:
+ type = AutomationId::kTypeExtensionPopup;
+ break;
+ case chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE:
+ type = AutomationId::kTypeExtensionBgPage;
+ break;
+ case chrome::VIEW_TYPE_EXTENSION_INFOBAR:
+ type = AutomationId::kTypeExtensionInfobar;
+ break;
+ default:
+ type = AutomationId::kTypeInvalid;
+ break;
+ }
+ // Since these extension views do not permit navigation, using the
+ // renderer process and view ID should suffice.
+ std::string id = base::StringPrintf("%d|%d",
+ ext_host->render_view_host()->routing_id(),
+ ext_host->render_process_host()->GetID());
+ return AutomationId(type, id);
+}
+
+AutomationId GetIdForExtension(const Extension* extension) {
+ return AutomationId(AutomationId::kTypeExtension, extension->id());
+}
+
+bool GetTabForId(const AutomationId& id, TabContents** tab) {
+ if (id.type() != AutomationId::kTypeTab)
+ return false;
+
+ BrowserList::const_iterator iter = BrowserList::begin();
+ for (; iter != BrowserList::end(); ++iter) {
+ Browser* browser = *iter;
+ for (int tab_index = 0; tab_index < browser->tab_count(); ++tab_index) {
+ TabContentsWrapper* wrapper = browser->GetTabContentsWrapperAt(tab_index);
+ if (base::IntToString(wrapper->restore_tab_helper()->session_id().id()) ==
+ id.id()) {
+ *tab = wrapper->tab_contents();
+ return true;
+ }
+ }
+ }
+ return false;
+}
+
+namespace {
+
+bool GetExtensionRenderViewForId(
+ const AutomationId& id,
+ Profile* profile,
+ RenderViewHost** rvh) {
+ content::ViewType view_type;
+ switch (id.type()) {
+ case AutomationId::kTypeExtensionPopup:
+ view_type = chrome::VIEW_TYPE_EXTENSION_POPUP;
+ break;
+ case AutomationId::kTypeExtensionBgPage:
+ view_type = chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE;
+ break;
+ case AutomationId::kTypeExtensionInfobar:
+ view_type = chrome::VIEW_TYPE_EXTENSION_INFOBAR;
+ break;
+ default:
+ return false;
+ }
+
+ ExtensionProcessManager* extension_mgr =
+ profile->GetExtensionProcessManager();
+ ExtensionProcessManager::const_iterator iter;
+ for (iter = extension_mgr->begin(); iter != extension_mgr->end();
+ ++iter) {
+ ExtensionHost* host = *iter;
+ AutomationId this_id = GetIdForExtensionView(host);
+ if (id == this_id) {
+ *rvh = host->render_view_host();
+ return true;
+ }
+ }
+ return false;
+}
+
+} // namespace
+
+bool GetRenderViewForId(
+ const AutomationId& id,
+ Profile* profile,
+ RenderViewHost** rvh) {
+ switch (id.type()) {
+ case AutomationId::kTypeTab: {
+ TabContents* tab;
+ if (!GetTabForId(id, &tab))
+ return false;
+ *rvh = tab->render_view_host();
+ break;
+ }
+ case AutomationId::kTypeExtensionPopup:
+ case AutomationId::kTypeExtensionBgPage:
+ case AutomationId::kTypeExtensionInfobar:
+ if (!GetExtensionRenderViewForId(id, profile, rvh))
+ return false;
+ break;
+ default:
+ return false;
+ }
+ return true;
+}
+
+bool GetExtensionForId(
+ const AutomationId& id,
+ Profile* profile,
+ const Extension** extension) {
+ if (id.type() != AutomationId::kTypeExtension)
+ return false;
+ ExtensionService* service = profile->GetExtensionService();
+ const Extension* installed_extension =
+ service->GetInstalledExtension(id.id());
+ if (installed_extension)
+ *extension = installed_extension;
+ return !!installed_extension;
+}
+
+bool DoesObjectWithIdExist(const AutomationId& id, Profile* profile) {
+ switch (id.type()) {
+ case AutomationId::kTypeTab: {
+ TabContents* tab;
+ return GetTabForId(id, &tab);
+ }
+ case AutomationId::kTypeExtensionPopup:
+ case AutomationId::kTypeExtensionBgPage:
+ case AutomationId::kTypeExtensionInfobar: {
+ RenderViewHost* rvh;
+ return GetExtensionRenderViewForId(id, profile, &rvh);
+ }
+ case AutomationId::kTypeExtension: {
+ const Extension* extension;
+ return GetExtensionForId(id, profile, &extension);
+ }
+ default:
+ break;
+ }
+ return false;
+}
+
} // namespace automation_util
« no previous file with comments | « chrome/browser/automation/automation_util.h ('k') | chrome/browser/automation/testing_automation_provider.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698