OLD | NEW |
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/automation_id.h" |
| 29 #include "chrome/common/chrome_view_type.h" |
| 30 #include "chrome/common/extensions/extension.h" |
21 #include "content/browser/renderer_host/render_view_host.h" | 31 #include "content/browser/renderer_host/render_view_host.h" |
22 #include "content/browser/tab_contents/tab_contents.h" | 32 #include "content/browser/tab_contents/tab_contents.h" |
23 #include "content/public/browser/browser_thread.h" | 33 #include "content/public/browser/browser_thread.h" |
24 #include "net/base/cookie_monster.h" | 34 #include "net/base/cookie_monster.h" |
25 #include "net/base/cookie_store.h" | 35 #include "net/base/cookie_store.h" |
26 #include "net/url_request/url_request_context.h" | 36 #include "net/url_request/url_request_context.h" |
27 #include "net/url_request/url_request_context_getter.h" | 37 #include "net/url_request/url_request_context_getter.h" |
28 | 38 |
29 using content::BrowserThread; | 39 using content::BrowserThread; |
30 | 40 |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 | 138 |
129 TabContents* GetTabContentsAt(int browser_index, int tab_index) { | 139 TabContents* GetTabContentsAt(int browser_index, int tab_index) { |
130 if (tab_index < 0) | 140 if (tab_index < 0) |
131 return NULL; | 141 return NULL; |
132 Browser* browser = GetBrowserAt(browser_index); | 142 Browser* browser = GetBrowserAt(browser_index); |
133 if (!browser || tab_index >= browser->tab_count()) | 143 if (!browser || tab_index >= browser->tab_count()) |
134 return NULL; | 144 return NULL; |
135 return browser->GetTabContentsAt(tab_index); | 145 return browser->GetTabContentsAt(tab_index); |
136 } | 146 } |
137 | 147 |
| 148 Browser* GetBrowserForTab(TabContents* tab) { |
| 149 BrowserList::const_iterator browser_iter = BrowserList::begin(); |
| 150 for (; browser_iter != BrowserList::end(); ++browser_iter) { |
| 151 Browser* browser = *browser_iter; |
| 152 for (int tab_index = 0; tab_index < browser->tab_count(); ++tab_index) { |
| 153 if (browser->GetTabContentsAt(tab_index) == tab) |
| 154 return browser; |
| 155 } |
| 156 } |
| 157 return NULL; |
| 158 } |
| 159 |
138 net::URLRequestContextGetter* GetRequestContext(TabContents* contents) { | 160 net::URLRequestContextGetter* GetRequestContext(TabContents* contents) { |
139 // Since we may be on the UI thread don't call GetURLRequestContext(). | 161 // Since we may be on the UI thread don't call GetURLRequestContext(). |
140 // Get the request context specific to the current TabContents and app. | 162 // Get the request context specific to the current TabContents and app. |
141 return contents->browser_context()->GetRequestContextForRenderProcess( | 163 return contents->browser_context()->GetRequestContextForRenderProcess( |
142 contents->render_view_host()->process()->GetID()); | 164 contents->render_view_host()->process()->GetID()); |
143 } | 165 } |
144 | 166 |
145 void GetCookies(const GURL& url, | 167 void GetCookies(const GURL& url, |
146 TabContents* contents, | 168 TabContents* contents, |
147 int* value_size, | 169 int* value_size, |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
374 bool SendErrorIfModalDialogActive(AutomationProvider* provider, | 396 bool SendErrorIfModalDialogActive(AutomationProvider* provider, |
375 IPC::Message* message) { | 397 IPC::Message* message) { |
376 bool active = AppModalDialogQueue::GetInstance()->HasActiveDialog(); | 398 bool active = AppModalDialogQueue::GetInstance()->HasActiveDialog(); |
377 if (active) { | 399 if (active) { |
378 AutomationJSONReply(provider, message).SendError( | 400 AutomationJSONReply(provider, message).SendError( |
379 "Command cannot be performed because a modal dialog is active"); | 401 "Command cannot be performed because a modal dialog is active"); |
380 } | 402 } |
381 return active; | 403 return active; |
382 } | 404 } |
383 | 405 |
| 406 AutomationId GetIdForTab(const TabContentsWrapper* tab) { |
| 407 return AutomationId( |
| 408 AutomationId::kTypeTab, |
| 409 base::IntToString(tab->restore_tab_helper()->session_id().id())); |
| 410 } |
| 411 |
| 412 AutomationId GetIdForExtensionView(const ExtensionHost* ext_host) { |
| 413 AutomationId::Type type; |
| 414 switch (ext_host->extension_host_type()) { |
| 415 case chrome::VIEW_TYPE_EXTENSION_POPUP: |
| 416 type = AutomationId::kTypeExtensionPopup; |
| 417 break; |
| 418 case chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE: |
| 419 type = AutomationId::kTypeExtensionBgPage; |
| 420 break; |
| 421 case chrome::VIEW_TYPE_EXTENSION_INFOBAR: |
| 422 type = AutomationId::kTypeExtensionInfobar; |
| 423 break; |
| 424 default: |
| 425 type = AutomationId::kTypeInvalid; |
| 426 break; |
| 427 } |
| 428 // Since these extension views do not permit navigation, using the |
| 429 // renderer process and view ID should suffice. |
| 430 std::string id = base::StringPrintf("%d|%d", |
| 431 ext_host->render_view_host()->routing_id(), |
| 432 ext_host->render_process_host()->GetID()); |
| 433 return AutomationId(type, id); |
| 434 } |
| 435 |
| 436 AutomationId GetIdForExtension(const Extension* extension) { |
| 437 return AutomationId(AutomationId::kTypeExtension, extension->id()); |
| 438 } |
| 439 |
| 440 bool GetTabForId(const AutomationId& id, TabContents** tab) { |
| 441 if (id.type() != AutomationId::kTypeTab) |
| 442 return false; |
| 443 |
| 444 BrowserList::const_iterator iter = BrowserList::begin(); |
| 445 for (; iter != BrowserList::end(); ++iter) { |
| 446 Browser* browser = *iter; |
| 447 for (int tab_index = 0; tab_index < browser->tab_count(); ++tab_index) { |
| 448 TabContentsWrapper* wrapper = browser->GetTabContentsWrapperAt(tab_index); |
| 449 if (base::IntToString(wrapper->restore_tab_helper()->session_id().id()) == |
| 450 id.id()) { |
| 451 *tab = wrapper->tab_contents(); |
| 452 return true; |
| 453 } |
| 454 } |
| 455 } |
| 456 return false; |
| 457 } |
| 458 |
| 459 namespace { |
| 460 |
| 461 bool GetExtensionRenderViewForId( |
| 462 const AutomationId& id, |
| 463 Profile* profile, |
| 464 RenderViewHost** rvh) { |
| 465 content::ViewType view_type; |
| 466 switch (id.type()) { |
| 467 case AutomationId::kTypeExtensionPopup: |
| 468 view_type = chrome::VIEW_TYPE_EXTENSION_POPUP; |
| 469 break; |
| 470 case AutomationId::kTypeExtensionBgPage: |
| 471 view_type = chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE; |
| 472 break; |
| 473 case AutomationId::kTypeExtensionInfobar: |
| 474 view_type = chrome::VIEW_TYPE_EXTENSION_INFOBAR; |
| 475 break; |
| 476 default: |
| 477 return false; |
| 478 } |
| 479 |
| 480 ExtensionProcessManager* extension_mgr = |
| 481 profile->GetExtensionProcessManager(); |
| 482 ExtensionProcessManager::const_iterator iter; |
| 483 for (iter = extension_mgr->begin(); iter != extension_mgr->end(); |
| 484 ++iter) { |
| 485 ExtensionHost* host = *iter; |
| 486 AutomationId this_id = GetIdForExtensionView(host); |
| 487 if (id == this_id) { |
| 488 *rvh = host->render_view_host(); |
| 489 return true; |
| 490 } |
| 491 } |
| 492 return false; |
| 493 } |
| 494 |
| 495 } // namespace |
| 496 |
| 497 bool GetRenderViewForId( |
| 498 const AutomationId& id, |
| 499 Profile* profile, |
| 500 RenderViewHost** rvh) { |
| 501 switch (id.type()) { |
| 502 case AutomationId::kTypeTab: { |
| 503 TabContents* tab; |
| 504 if (!GetTabForId(id, &tab)) |
| 505 return false; |
| 506 *rvh = tab->render_view_host(); |
| 507 break; |
| 508 } |
| 509 case AutomationId::kTypeExtensionPopup: |
| 510 case AutomationId::kTypeExtensionBgPage: |
| 511 case AutomationId::kTypeExtensionInfobar: |
| 512 if (!GetExtensionRenderViewForId(id, profile, rvh)) |
| 513 return false; |
| 514 break; |
| 515 default: |
| 516 return false; |
| 517 } |
| 518 return true; |
| 519 } |
| 520 |
| 521 bool GetExtensionForId( |
| 522 const AutomationId& id, |
| 523 Profile* profile, |
| 524 const Extension** extension) { |
| 525 if (id.type() != AutomationId::kTypeExtension) |
| 526 return false; |
| 527 ExtensionService* service = profile->GetExtensionService(); |
| 528 const Extension* installed_extension = |
| 529 service->GetInstalledExtension(id.id()); |
| 530 if (installed_extension) |
| 531 *extension = installed_extension; |
| 532 return !!installed_extension; |
| 533 } |
| 534 |
| 535 bool DoesObjectWithIdExist(const AutomationId& id, Profile* profile) { |
| 536 switch (id.type()) { |
| 537 case AutomationId::kTypeTab: { |
| 538 TabContents* tab; |
| 539 return GetTabForId(id, &tab); |
| 540 } |
| 541 case AutomationId::kTypeExtensionPopup: |
| 542 case AutomationId::kTypeExtensionBgPage: |
| 543 case AutomationId::kTypeExtensionInfobar: { |
| 544 RenderViewHost* rvh; |
| 545 return GetExtensionRenderViewForId(id, profile, &rvh); |
| 546 } |
| 547 case AutomationId::kTypeExtension: { |
| 548 const Extension* extension; |
| 549 return GetExtensionForId(id, profile, &extension); |
| 550 } |
| 551 default: |
| 552 break; |
| 553 } |
| 554 return false; |
| 555 } |
| 556 |
384 } // namespace automation_util | 557 } // namespace automation_util |
OLD | NEW |