OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/extensions/api/tabs/tabs_api.h" | 5 #include "chrome/browser/extensions/api/tabs/tabs_api.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <limits> | 9 #include <limits> |
10 #include <memory> | 10 #include <memory> |
(...skipping 30 matching lines...) Expand all Loading... | |
41 #include "chrome/browser/sessions/session_tab_helper.h" | 41 #include "chrome/browser/sessions/session_tab_helper.h" |
42 #include "chrome/browser/translate/chrome_translate_client.h" | 42 #include "chrome/browser/translate/chrome_translate_client.h" |
43 #include "chrome/browser/ui/apps/chrome_app_delegate.h" | 43 #include "chrome/browser/ui/apps/chrome_app_delegate.h" |
44 #include "chrome/browser/ui/browser.h" | 44 #include "chrome/browser/ui/browser.h" |
45 #include "chrome/browser/ui/browser_commands.h" | 45 #include "chrome/browser/ui/browser_commands.h" |
46 #include "chrome/browser/ui/browser_finder.h" | 46 #include "chrome/browser/ui/browser_finder.h" |
47 #include "chrome/browser/ui/browser_list.h" | 47 #include "chrome/browser/ui/browser_list.h" |
48 #include "chrome/browser/ui/browser_navigator.h" | 48 #include "chrome/browser/ui/browser_navigator.h" |
49 #include "chrome/browser/ui/browser_navigator_params.h" | 49 #include "chrome/browser/ui/browser_navigator_params.h" |
50 #include "chrome/browser/ui/browser_window.h" | 50 #include "chrome/browser/ui/browser_window.h" |
51 #include "chrome/browser/ui/panels/panel_manager.h" | |
52 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 51 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
53 #include "chrome/browser/ui/tabs/tab_utils.h" | 52 #include "chrome/browser/ui/tabs/tab_utils.h" |
54 #include "chrome/browser/ui/window_sizer/window_sizer.h" | 53 #include "chrome/browser/ui/window_sizer/window_sizer.h" |
55 #include "chrome/browser/web_applications/web_app.h" | 54 #include "chrome/browser/web_applications/web_app.h" |
56 #include "chrome/common/chrome_switches.h" | 55 #include "chrome/common/chrome_switches.h" |
57 #include "chrome/common/extensions/api/tabs.h" | 56 #include "chrome/common/extensions/api/tabs.h" |
58 #include "chrome/common/extensions/api/windows.h" | 57 #include "chrome/common/extensions/api/windows.h" |
59 #include "chrome/common/extensions/extension_constants.h" | 58 #include "chrome/common/extensions/extension_constants.h" |
60 #include "chrome/common/pref_names.h" | 59 #include "chrome/common/pref_names.h" |
61 #include "chrome/common/url_constants.h" | 60 #include "chrome/common/url_constants.h" |
(...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
499 return false; | 498 return false; |
500 } | 499 } |
501 } | 500 } |
502 | 501 |
503 if (!IsValidStateForWindowsCreateFunction(create_data)) { | 502 if (!IsValidStateForWindowsCreateFunction(create_data)) { |
504 error_ = keys::kInvalidWindowStateError; | 503 error_ = keys::kInvalidWindowStateError; |
505 return false; | 504 return false; |
506 } | 505 } |
507 | 506 |
508 Browser::Type window_type = Browser::TYPE_TABBED; | 507 Browser::Type window_type = Browser::TYPE_TABBED; |
509 bool create_panel = false; | 508 bool create_chromeOS_panel = false; |
sky
2016/08/22 15:05:55
Style guide says all lower case, e.g. chrome_os_pa
stevenjb
2016/08/22 15:45:02
create_ash_panel would be best (see below). Also t
Dmitry Titov
2016/08/22 23:45:53
Done.
Dmitry Titov
2016/08/22 23:45:53
Done.
| |
510 | |
511 // panel_create_mode only applies if create_panel = true | |
512 PanelManager::CreateMode panel_create_mode = PanelManager::CREATE_AS_DOCKED; | |
513 | 509 |
514 gfx::Rect window_bounds; | 510 gfx::Rect window_bounds; |
515 bool focused = true; | 511 bool focused = true; |
516 bool saw_focus_key = false; | 512 bool saw_focus_key = false; |
517 std::string extension_id; | 513 std::string extension_id; |
518 | 514 |
519 if (create_data) { | 515 if (create_data) { |
520 // Figure out window type before figuring out bounds so that default | 516 // Figure out window type before figuring out bounds so that default |
521 // bounds can be set according to the window type. | 517 // bounds can be set according to the window type. |
522 switch (create_data->type) { | 518 switch (create_data->type) { |
523 case windows::CREATE_TYPE_POPUP: | 519 case windows::CREATE_TYPE_POPUP: |
524 window_type = Browser::TYPE_POPUP; | 520 window_type = Browser::TYPE_POPUP; |
525 extension_id = extension()->id(); | 521 extension_id = extension()->id(); |
526 break; | 522 break; |
523 | |
527 case windows::CREATE_TYPE_PANEL: | 524 case windows::CREATE_TYPE_PANEL: |
528 case windows::CREATE_TYPE_DETACHED_PANEL: { | 525 case windows::CREATE_TYPE_DETACHED_PANEL: { |
526 #if defined(OS_CHROMEOS) | |
527 // Only ChromeOS' version of chrome.windows.create would create a panel | |
528 // window. It is whitelisted to Hangouts extension for limited time until | |
529 // it transitioned to other types of windows. | |
529 extension_id = extension()->id(); | 530 extension_id = extension()->id(); |
530 bool use_panels = PanelManager::ShouldUsePanels(extension_id); | 531 for (const char* id : extension_misc::kHangoutsExtensionIds) { |
531 if (use_panels) { | 532 if (extension_id == id) { |
532 create_panel = true; | 533 create_chromeOS_panel = true; |
533 #if !defined(USE_ASH) | 534 break; |
534 // Non-ash supports both docked and detached panel types. | |
535 if (create_data->type == windows::CREATE_TYPE_DETACHED_PANEL) { | |
536 panel_create_mode = PanelManager::CREATE_AS_DETACHED; | |
537 } | 535 } |
538 #endif // USE_ASH | 536 } |
539 } else { | 537 #endif // defined(OS_CHROMEOS) |
538 // Everything else gets POPUP instead of PANEL. | |
539 // TODO(dimich): Eventually, remove the 'panel' values form valid | |
540 // window.create parameters. However, this is a more breaking change, so | |
541 // for now simply treat it as a POPUP. | |
542 if (!create_chromeOS_panel) | |
540 window_type = Browser::TYPE_POPUP; | 543 window_type = Browser::TYPE_POPUP; |
stevenjb
2016/08/22 15:45:02
Don't we need to set extension_id for non chrome o
Dmitry Titov
2016/08/22 23:45:54
Done.
| |
541 } | |
542 break; | 544 break; |
543 } | 545 } |
546 | |
544 case windows::CREATE_TYPE_NONE: | 547 case windows::CREATE_TYPE_NONE: |
545 case windows::CREATE_TYPE_NORMAL: | 548 case windows::CREATE_TYPE_NORMAL: |
546 break; | 549 break; |
547 default: | 550 default: |
548 error_ = keys::kInvalidWindowTypeError; | 551 error_ = keys::kInvalidWindowTypeError; |
549 return false; | 552 return false; |
550 } | 553 } |
551 | 554 |
552 // Initialize default window bounds according to window type. | 555 // Initialize default window bounds according to window type. |
553 if (window_type == Browser::TYPE_TABBED || | 556 if (window_type == Browser::TYPE_TABBED || |
554 window_type == Browser::TYPE_POPUP || | 557 window_type == Browser::TYPE_POPUP || |
555 create_panel) { | 558 create_chromeOS_panel) { |
556 // Try to position the new browser relative to its originating | 559 // Try to position the new browser relative to its originating |
557 // browser window. The call offsets the bounds by kWindowTilePixels | 560 // browser window. The call offsets the bounds by kWindowTilePixels |
558 // (defined in WindowSizer to be 10). | 561 // (defined in WindowSizer to be 10). |
559 // | 562 // |
560 // NOTE(rafaelw): It's ok if GetCurrentBrowser() returns NULL here. | 563 // NOTE(rafaelw): It's ok if GetCurrentBrowser() returns NULL here. |
561 // GetBrowserWindowBounds will default to saved "default" values for | 564 // GetBrowserWindowBounds will default to saved "default" values for |
562 // the app. | 565 // the app. |
563 ui::WindowShowState show_state = ui::SHOW_STATE_DEFAULT; | 566 ui::WindowShowState show_state = ui::SHOW_STATE_DEFAULT; |
564 WindowSizer::GetBrowserWindowBoundsAndShowState(std::string(), | 567 WindowSizer::GetBrowserWindowBoundsAndShowState(std::string(), |
565 gfx::Rect(), | 568 gfx::Rect(), |
566 GetCurrentBrowser(), | 569 GetCurrentBrowser(), |
567 &window_bounds, | 570 &window_bounds, |
568 &show_state); | 571 &show_state); |
569 } | 572 } |
570 | 573 |
571 if (create_panel && PanelManager::CREATE_AS_DETACHED == panel_create_mode) { | |
572 window_bounds.set_origin( | |
573 PanelManager::GetInstance()->GetDefaultDetachedPanelOrigin()); | |
574 } | |
575 | |
576 // Any part of the bounds can optionally be set by the caller. | 574 // Any part of the bounds can optionally be set by the caller. |
577 if (create_data->left) | 575 if (create_data->left) |
578 window_bounds.set_x(*create_data->left); | 576 window_bounds.set_x(*create_data->left); |
579 | 577 |
580 if (create_data->top) | 578 if (create_data->top) |
581 window_bounds.set_y(*create_data->top); | 579 window_bounds.set_y(*create_data->top); |
582 | 580 |
583 if (create_data->width) | 581 if (create_data->width) |
584 window_bounds.set_width(*create_data->width); | 582 window_bounds.set_width(*create_data->width); |
585 | 583 |
586 if (create_data->height) | 584 if (create_data->height) |
587 window_bounds.set_height(*create_data->height); | 585 window_bounds.set_height(*create_data->height); |
588 | 586 |
589 if (create_data->focused) { | 587 if (create_data->focused) { |
590 focused = *create_data->focused; | 588 focused = *create_data->focused; |
591 saw_focus_key = true; | 589 saw_focus_key = true; |
592 } | 590 } |
593 } | 591 } |
594 | 592 |
595 if (create_panel) { | 593 if (create_chromeOS_panel) { |
596 if (urls.empty()) | 594 if (urls.empty()) |
597 urls.push_back(GURL(chrome::kChromeUINewTabURL)); | 595 urls.push_back(GURL(chrome::kChromeUINewTabURL)); |
598 | 596 |
599 #if defined(USE_ASH) | 597 #if defined(USE_ASH) |
sky
2016/08/22 15:05:55
If you're setting chrome_os_panel to true based on
stevenjb
2016/08/22 15:45:02
Agreed, we should be consistent. I think USE_ASH i
Dmitry Titov
2016/08/22 23:45:53
Done.
Dmitry Titov
2016/08/22 23:45:54
Done.
| |
600 AppWindow::CreateParams create_params; | 598 AppWindow::CreateParams create_params; |
601 create_params.window_type = AppWindow::WINDOW_TYPE_V1_PANEL; | 599 create_params.window_type = AppWindow::WINDOW_TYPE_V1_PANEL; |
602 create_params.window_key = extension_id; | 600 create_params.window_key = extension_id; |
603 create_params.window_spec.bounds = window_bounds; | 601 create_params.window_spec.bounds = window_bounds; |
604 create_params.focused = saw_focus_key && focused; | 602 create_params.focused = saw_focus_key && focused; |
605 AppWindow* app_window = | 603 AppWindow* app_window = |
606 new AppWindow(window_profile, new ChromeAppDelegate(true), extension()); | 604 new AppWindow(window_profile, new ChromeAppDelegate(true), extension()); |
607 AshPanelContents* ash_panel_contents = new AshPanelContents(app_window); | 605 AshPanelContents* ash_panel_contents = new AshPanelContents(app_window); |
608 app_window->Init(urls[0], ash_panel_contents, render_frame_host(), | 606 app_window->Init(urls[0], ash_panel_contents, render_frame_host(), |
609 create_params); | 607 create_params); |
610 WindowController* window_controller = | 608 WindowController* window_controller = |
611 WindowControllerList::GetInstance()->FindWindowById( | 609 WindowControllerList::GetInstance()->FindWindowById( |
612 app_window->session_id().id()); | 610 app_window->session_id().id()); |
613 if (!window_controller) | 611 if (!window_controller) |
614 return false; | 612 return false; |
615 SetResult(window_controller->CreateWindowValueWithTabs(extension())); | 613 SetResult(window_controller->CreateWindowValueWithTabs(extension())); |
616 return true; | 614 return true; |
617 #else | 615 #else |
618 std::string title = | 616 return false; |
619 web_app::GenerateApplicationNameFromExtensionId(extension_id); | |
620 content::SiteInstance* source_site_instance = | |
621 render_frame_host()->GetSiteInstance(); | |
622 // Note: Panels ignore all but the first url provided. | |
623 Panel* panel = PanelManager::GetInstance()->CreatePanel( | |
624 title, window_profile, urls[0], source_site_instance, window_bounds, | |
625 panel_create_mode); | |
626 | |
627 // Unlike other window types, Panels do not take focus by default. | |
628 if (!saw_focus_key || !focused) | |
629 panel->ShowInactive(); | |
630 else | |
631 panel->Show(); | |
632 | |
633 SetResult(panel->extension_window_controller()->CreateWindowValueWithTabs( | |
634 extension())); | |
635 return true; | |
636 #endif | 617 #endif |
637 } | 618 } |
638 | 619 |
639 // Create a new BrowserWindow. | 620 // Create a new BrowserWindow. |
640 if (create_panel) | |
641 window_type = Browser::TYPE_POPUP; | |
642 Browser::CreateParams create_params(window_type, window_profile); | 621 Browser::CreateParams create_params(window_type, window_profile); |
643 if (extension_id.empty()) { | 622 if (extension_id.empty()) { |
644 create_params.initial_bounds = window_bounds; | 623 create_params.initial_bounds = window_bounds; |
645 } else { | 624 } else { |
646 create_params = Browser::CreateParams::CreateForApp( | 625 create_params = Browser::CreateParams::CreateForApp( |
647 web_app::GenerateApplicationNameFromExtensionId(extension_id), | 626 web_app::GenerateApplicationNameFromExtensionId(extension_id), |
648 false /* trusted_source */, window_bounds, window_profile); | 627 false /* trusted_source */, window_bounds, window_profile); |
649 } | 628 } |
650 create_params.initial_show_state = ui::SHOW_STATE_NORMAL; | 629 create_params.initial_show_state = ui::SHOW_STATE_NORMAL; |
651 if (create_data && create_data->state) { | 630 if (create_data && create_data->state) { |
652 create_params.initial_show_state = | 631 create_params.initial_show_state = |
653 ConvertToWindowShowState(create_data->state); | 632 ConvertToWindowShowState(create_data->state); |
654 } | 633 } |
655 | 634 |
656 Browser* new_window = new Browser(create_params); | 635 Browser* new_window = new Browser(create_params); |
657 | 636 |
658 for (const GURL& url : urls) { | 637 for (const GURL& url : urls) { |
659 chrome::NavigateParams navigate_params(new_window, url, | 638 chrome::NavigateParams navigate_params(new_window, url, |
660 ui::PAGE_TRANSITION_LINK); | 639 ui::PAGE_TRANSITION_LINK); |
661 navigate_params.disposition = NEW_FOREGROUND_TAB; | 640 navigate_params.disposition = NEW_FOREGROUND_TAB; |
662 navigate_params.source_site_instance = | 641 navigate_params.source_site_instance = |
663 render_frame_host()->GetSiteInstance(); | 642 render_frame_host()->GetSiteInstance(); |
664 chrome::Navigate(&navigate_params); | 643 chrome::Navigate(&navigate_params); |
665 if (create_panel) { | |
666 TabHelper::FromWebContents(navigate_params.target_contents) | |
667 ->SetExtensionAppIconById(extension_id); | |
668 } | |
669 } | 644 } |
670 | 645 |
671 WebContents* contents = NULL; | 646 WebContents* contents = NULL; |
672 // Move the tab into the created window only if it's an empty popup or it's | 647 // Move the tab into the created window only if it's an empty popup or it's |
673 // a tabbed window. | 648 // a tabbed window. |
674 if ((window_type == Browser::TYPE_POPUP && urls.empty()) || | 649 if ((window_type == Browser::TYPE_POPUP && urls.empty()) || |
675 window_type == Browser::TYPE_TABBED) { | 650 window_type == Browser::TYPE_TABBED) { |
676 if (source_tab_strip) | 651 if (source_tab_strip) |
677 contents = source_tab_strip->DetachWebContentsAt(tab_index); | 652 contents = source_tab_strip->DetachWebContentsAt(tab_index); |
678 if (contents) { | 653 if (contents) { |
679 TabStripModel* target_tab_strip = new_window->tab_strip_model(); | 654 TabStripModel* target_tab_strip = new_window->tab_strip_model(); |
680 target_tab_strip->InsertWebContentsAt(urls.size(), contents, | 655 target_tab_strip->InsertWebContentsAt(urls.size(), contents, |
681 TabStripModel::ADD_NONE); | 656 TabStripModel::ADD_NONE); |
682 } | 657 } |
683 } | 658 } |
684 // Create a new tab if the created window is still empty. Don't create a new | 659 // Create a new tab if the created window is still empty. Don't create a new |
685 // tab when it is intended to create an empty popup. | 660 // tab when it is intended to create an empty popup. |
686 if (!contents && urls.empty() && window_type != Browser::TYPE_POPUP) { | 661 if (!contents && urls.empty() && window_type != Browser::TYPE_POPUP) { |
687 chrome::NewTab(new_window); | 662 chrome::NewTab(new_window); |
688 } | 663 } |
689 chrome::SelectNumberedTab(new_window, 0); | 664 chrome::SelectNumberedTab(new_window, 0); |
690 | 665 |
691 // Unlike other window types, Panels do not take focus by default. | |
692 if (!saw_focus_key && create_panel) | |
693 focused = false; | |
694 | |
695 if (focused) | 666 if (focused) |
696 new_window->window()->Show(); | 667 new_window->window()->Show(); |
697 else | 668 else |
698 new_window->window()->ShowInactive(); | 669 new_window->window()->ShowInactive(); |
699 | 670 |
700 WindowController* controller = new_window->extension_window_controller(); | 671 WindowController* controller = new_window->extension_window_controller(); |
701 | 672 |
702 if (new_window->profile()->IsOffTheRecord() && | 673 if (new_window->profile()->IsOffTheRecord() && |
703 !GetProfile()->IsOffTheRecord() && !include_incognito()) { | 674 !GetProfile()->IsOffTheRecord() && !include_incognito()) { |
704 // Don't expose incognito windows if extension itself works in non-incognito | 675 // Don't expose incognito windows if extension itself works in non-incognito |
(...skipping 1506 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2211 params->tab_id | 2182 params->tab_id |
2212 ? ErrorUtils::FormatErrorMessage(keys::kCannotDiscardTab, | 2183 ? ErrorUtils::FormatErrorMessage(keys::kCannotDiscardTab, |
2213 base::IntToString(*params->tab_id)) | 2184 base::IntToString(*params->tab_id)) |
2214 : keys::kCannotFindTabToDiscard)); | 2185 : keys::kCannotFindTabToDiscard)); |
2215 } | 2186 } |
2216 | 2187 |
2217 TabsDiscardFunction::TabsDiscardFunction() {} | 2188 TabsDiscardFunction::TabsDiscardFunction() {} |
2218 TabsDiscardFunction::~TabsDiscardFunction() {} | 2189 TabsDiscardFunction::~TabsDiscardFunction() {} |
2219 | 2190 |
2220 } // namespace extensions | 2191 } // namespace extensions |
OLD | NEW |