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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_util.h" 5 #include "chrome/browser/automation/automation_util.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/stringprintf.h"
12 #include "base/string_number_conversions.h"
11 #include "base/synchronization/waitable_event.h" 13 #include "base/synchronization/waitable_event.h"
12 #include "base/time.h" 14 #include "base/time.h"
13 #include "base/values.h" 15 #include "base/values.h"
14 #include "chrome/browser/automation/automation_provider.h" 16 #include "chrome/browser/automation/automation_provider.h"
15 #include "chrome/browser/automation/automation_provider_json.h" 17 #include "chrome/browser/automation/automation_provider_json.h"
18 #include "chrome/browser/extensions/extension_host.h"
19 #include "chrome/browser/extensions/extension_process_manager.h"
16 #include "chrome/browser/extensions/extension_service.h" 20 #include "chrome/browser/extensions/extension_service.h"
17 #include "chrome/browser/profiles/profile.h" 21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/sessions/restore_tab_helper.h"
23 #include "chrome/browser/sessions/session_id.h"
18 #include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog_queue.h" 24 #include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog_queue.h"
19 #include "chrome/browser/ui/browser.h" 25 #include "chrome/browser/ui/browser.h"
20 #include "chrome/browser/ui/browser_list.h" 26 #include "chrome/browser/ui/browser_list.h"
27 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
28 #include "chrome/common/chrome_view_type.h"
29 #include "chrome/common/extensions/extension.h"
21 #include "content/browser/renderer_host/render_view_host.h" 30 #include "content/browser/renderer_host/render_view_host.h"
22 #include "content/browser/tab_contents/tab_contents.h" 31 #include "content/browser/tab_contents/tab_contents.h"
23 #include "content/public/browser/browser_thread.h" 32 #include "content/public/browser/browser_thread.h"
24 #include "net/base/cookie_monster.h" 33 #include "net/base/cookie_monster.h"
25 #include "net/base/cookie_store.h" 34 #include "net/base/cookie_store.h"
26 #include "net/url_request/url_request_context.h" 35 #include "net/url_request/url_request_context.h"
27 #include "net/url_request/url_request_context_getter.h" 36 #include "net/url_request/url_request_context_getter.h"
28 37
29 using content::BrowserThread; 38 using content::BrowserThread;
30 39
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after
374 bool SendErrorIfModalDialogActive(AutomationProvider* provider, 383 bool SendErrorIfModalDialogActive(AutomationProvider* provider,
375 IPC::Message* message) { 384 IPC::Message* message) {
376 bool active = AppModalDialogQueue::GetInstance()->HasActiveDialog(); 385 bool active = AppModalDialogQueue::GetInstance()->HasActiveDialog();
377 if (active) { 386 if (active) {
378 AutomationJSONReply(provider, message).SendError( 387 AutomationJSONReply(provider, message).SendError(
379 "Command cannot be performed because a modal dialog is active"); 388 "Command cannot be performed because a modal dialog is active");
380 } 389 }
381 return active; 390 return active;
382 } 391 }
383 392
393 AutomationId GetIdForTab(const TabContentsWrapper* tab) {
394 return AutomationId(
395 AutomationId::kTypeTab,
396 base::IntToString(tab->restore_tab_helper()->session_id().id()));
397 }
398
399 bool GetTabForId(const AutomationId& id, TabContents** tab) {
dennis_jeffrey 2011/12/03 00:19:37 Would it make sense to move this function down to
kkania 2011/12/03 00:34:31 Done.
400 if (id.type() != AutomationId::kTypeTab)
401 return false;
402
403 BrowserList::const_iterator iter = BrowserList::begin();
404 for (; iter != BrowserList::end(); ++iter) {
405 Browser* browser = *iter;
406 for (int tab_index = 0; tab_index < browser->tab_count(); ++tab_index) {
407 TabContentsWrapper* wrapper = browser->GetTabContentsWrapperAt(tab_index);
408 if (base::IntToString(wrapper->restore_tab_helper()->session_id().id()) ==
409 id.id()) {
410 *tab = wrapper->tab_contents();
411 return true;
412 }
413 }
414 }
415 return false;
416 }
417
418 AutomationId GetIdForExtensionView(const ExtensionHost* ext_host) {
419 AutomationId::Type type;
420 switch (ext_host->extension_host_type()) {
421 case chrome::VIEW_TYPE_EXTENSION_POPUP:
422 type = AutomationId::kTypeExtensionPopup;
423 break;
424 case chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE:
425 type = AutomationId::kTypeExtensionBgPage;
426 break;
427 case chrome::VIEW_TYPE_EXTENSION_INFOBAR:
428 type = AutomationId::kTypeExtensionInfobar;
429 break;
430 default:
431 type = AutomationId::kInvalidType;
432 break;
433 }
434 // Since these extension views do not permit navigation, using the
435 // renderer process and view ID should suffice.
436 std::string id = base::StringPrintf("%d|%d",
437 ext_host->render_view_host()->routing_id(),
438 ext_host->render_process_host()->GetID());
439 return AutomationId(type, id);
440 }
441
442 AutomationId GetIdForExtension(const Extension* extension) {
443 return AutomationId(AutomationId::kTypeExtension, extension->id());
444 }
445
446 namespace {
447
448 bool GetExtensionRenderViewForId(
449 const AutomationId& id,
450 Profile* profile,
451 RenderViewHost** rvh) {
452 content::ViewType view_type;
453 switch (id.type()) {
454 case AutomationId::kTypeExtensionPopup:
455 view_type = chrome::VIEW_TYPE_EXTENSION_POPUP;
456 break;
457 case AutomationId::kTypeExtensionBgPage:
458 view_type = chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE;
459 break;
460 case AutomationId::kTypeExtensionInfobar:
461 view_type = chrome::VIEW_TYPE_EXTENSION_INFOBAR;
462 break;
463 default:
464 return false;
465 }
466
467 ExtensionProcessManager* extension_mgr =
468 profile->GetExtensionProcessManager();
469 ExtensionProcessManager::const_iterator iter;
470 for (iter = extension_mgr->begin(); iter != extension_mgr->end();
471 ++iter) {
472 ExtensionHost* host = *iter;
473 AutomationId this_id = GetIdForExtensionView(host);
474 if (id == this_id) {
475 *rvh = host->render_view_host();
476 return true;
477 }
478 }
479 return false;
480 }
481
482 } // namespace
483
484 bool GetRenderViewForId(
485 const AutomationId& id,
486 Profile* profile,
487 RenderViewHost** rvh) {
488 switch (id.type()) {
489 case AutomationId::kTypeTab: {
490 TabContents* tab;
491 if (!GetTabForId(id, &tab))
492 return false;
493 *rvh = tab->render_view_host();
494 break;
495 }
496 case AutomationId::kTypeExtensionPopup:
497 case AutomationId::kTypeExtensionBgPage:
498 case AutomationId::kTypeExtensionInfobar:
499 if (!GetExtensionRenderViewForId(id, profile, rvh))
500 return false;
501 break;
502 default:
503 return false;
504 }
505 return true;
506 }
507
508 bool DoesObjectWithIdExist(const AutomationId& id, Profile* profile) {
509 switch(id.type()) {
510 case AutomationId::kTypeTab: {
511 TabContents* tab;
512 return GetTabForId(id, &tab);
513 }
514 case AutomationId::kTypeExtensionPopup:
515 case AutomationId::kTypeExtensionBgPage:
516 case AutomationId::kTypeExtensionInfobar: {
517 RenderViewHost* rvh;
518 return GetExtensionRenderViewForId(id, profile, &rvh);
519 }
520 default:
521 break;
522 }
523 return false;
524 }
525
384 } // namespace automation_util 526 } // namespace automation_util
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698