| 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.h" | 5 #include "chrome/browser/extensions/api/tabs/tabs.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 446 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 457 return false; | 457 return false; |
| 458 contents = source_tab_strip->DetachTabContentsAt(tab_index); | 458 contents = source_tab_strip->DetachTabContentsAt(tab_index); |
| 459 if (!contents) { | 459 if (!contents) { |
| 460 error_ = ExtensionErrorUtils::FormatErrorMessage( | 460 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 461 keys::kTabNotFoundError, base::IntToString(tab_id)); | 461 keys::kTabNotFoundError, base::IntToString(tab_id)); |
| 462 return false; | 462 return false; |
| 463 } | 463 } |
| 464 } | 464 } |
| 465 } | 465 } |
| 466 | 466 |
| 467 // Try to position the new browser relative its originating browser window. | |
| 468 gfx::Rect window_bounds; | |
| 469 // The call offsets the bounds by kWindowTilePixels (defined in WindowSizer to | |
| 470 // be 10) | |
| 471 // | |
| 472 // NOTE(rafaelw): It's ok if GetCurrentBrowser() returns NULL here. | |
| 473 // GetBrowserWindowBounds will default to saved "default" values for the app. | |
| 474 WindowSizer::GetBrowserWindowBounds(std::string(), gfx::Rect(), | |
| 475 GetCurrentBrowser(), &window_bounds); | |
| 476 | |
| 477 // Calculate popup and panels bounds separately. | |
| 478 gfx::Rect popup_bounds; | |
| 479 gfx::Rect panel_bounds; // Use 0x0 for panels. Panel manager sizes them. | |
| 480 | |
| 481 // In ChromiumOS the default popup bounds is 0x0 which indicates default | |
| 482 // window sizes in PanelBrowserView. In other OSs use the same default | |
| 483 // bounds as windows. | |
| 484 #if defined(OS_CHROMEOS) | |
| 485 popup_bounds = panel_bounds; | |
| 486 #else | |
| 487 popup_bounds = window_bounds; // Use window size as default for popups | |
| 488 #endif | |
| 489 | |
| 490 Profile* window_profile = profile(); | 467 Profile* window_profile = profile(); |
| 491 Browser::Type window_type = Browser::TYPE_TABBED; | 468 Browser::Type window_type = Browser::TYPE_TABBED; |
| 469 |
| 470 // panel_create_mode only applies if window is TYPE_PANEL. |
| 471 PanelManager::CreateMode panel_create_mode = PanelManager::CREATE_DOCKED; |
| 472 |
| 473 gfx::Rect window_bounds; |
| 492 bool focused = true; | 474 bool focused = true; |
| 493 bool saw_focus_key = false; | 475 bool saw_focus_key = false; |
| 494 std::string extension_id; | 476 std::string extension_id; |
| 495 | 477 |
| 496 // Decide whether we are opening a normal window or an incognito window. | 478 // Decide whether we are opening a normal window or an incognito window. |
| 497 bool is_error = true; | 479 bool is_error = true; |
| 498 bool open_incognito_window = ShouldOpenIncognitoWindow(args, &urls, | 480 bool open_incognito_window = ShouldOpenIncognitoWindow(args, &urls, |
| 499 &is_error); | 481 &is_error); |
| 500 if (is_error) { | 482 if (is_error) { |
| 501 // error_ member variable is set inside of ShouldOpenIncognitoWindow. | 483 // error_ member variable is set inside of ShouldOpenIncognitoWindow. |
| 502 return false; | 484 return false; |
| 503 } | 485 } |
| 504 if (open_incognito_window) { | 486 if (open_incognito_window) { |
| 505 window_profile = window_profile->GetOffTheRecordProfile(); | 487 window_profile = window_profile->GetOffTheRecordProfile(); |
| 506 } | 488 } |
| 507 | 489 |
| 508 if (args) { | 490 if (args) { |
| 491 // Figure out window type before figuring out bounds so that default |
| 492 // bounds can be set according to the window type. |
| 493 std::string type_str; |
| 494 if (args->HasKey(keys::kWindowTypeKey)) { |
| 495 EXTENSION_FUNCTION_VALIDATE(args->GetString(keys::kWindowTypeKey, |
| 496 &type_str)); |
| 497 if (type_str == keys::kWindowTypeValuePopup) { |
| 498 window_type = Browser::TYPE_POPUP; |
| 499 extension_id = GetExtension()->id(); |
| 500 } else if (type_str == keys::kWindowTypeValuePanel || |
| 501 type_str == keys::kWindowTypeValueDetachedPanel) { |
| 502 extension_id = GetExtension()->id(); |
| 503 bool use_panels = false; |
| 504 #if !defined(OS_ANDROID) |
| 505 use_panels = PanelManager::ShouldUsePanels(extension_id); |
| 506 #endif |
| 507 if (use_panels) { |
| 508 window_type = Browser::TYPE_PANEL; |
| 509 if (type_str == keys::kWindowTypeValueDetachedPanel) |
| 510 panel_create_mode = PanelManager::CREATE_DETACHED; |
| 511 } else { |
| 512 window_type = Browser::TYPE_POPUP; |
| 513 } |
| 514 } else if (type_str != keys::kWindowTypeValueNormal) { |
| 515 error_ = keys::kInvalidWindowTypeError; |
| 516 return false; |
| 517 } |
| 518 } |
| 519 |
| 520 // Initialize default window bounds according to window type. |
| 521 // In ChromiumOS the default popup bounds is 0x0 which indicates default |
| 522 // window sizes in PanelBrowserView. In other OSs use the same default |
| 523 // bounds as windows. |
| 524 #if !defined(OS_CHROMEOS) |
| 525 if (Browser::TYPE_TABBED == window_type || |
| 526 Browser::TYPE_POPUP == window_type) { |
| 527 #else |
| 528 if (Browser::TYPE_TABBED == window_type) { |
| 529 #endif |
| 530 // Try to position the new browser relative to its originating |
| 531 // browser window. The call offsets the bounds by kWindowTilePixels |
| 532 // (defined in WindowSizer to be 10). |
| 533 // |
| 534 // NOTE(rafaelw): It's ok if GetCurrentBrowser() returns NULL here. |
| 535 // GetBrowserWindowBounds will default to saved "default" values for |
| 536 // the app. |
| 537 WindowSizer::GetBrowserWindowBounds(std::string(), gfx::Rect(), |
| 538 GetCurrentBrowser(), |
| 539 &window_bounds); |
| 540 } |
| 541 |
| 542 #if !defined(USE_ASH) |
| 543 if (Browser::TYPE_PANEL == window_type && |
| 544 PanelManager::CREATE_DETACHED == panel_create_mode) { |
| 545 window_bounds.set_origin( |
| 546 PanelManager::GetInstance()->GetDefaultDetachedPanelOrigin()); |
| 547 } |
| 548 #endif |
| 549 |
| 509 // Any part of the bounds can optionally be set by the caller. | 550 // Any part of the bounds can optionally be set by the caller. |
| 510 int bounds_val = -1; | 551 int bounds_val = -1; |
| 511 if (args->HasKey(keys::kLeftKey)) { | 552 if (args->HasKey(keys::kLeftKey)) { |
| 512 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(keys::kLeftKey, | 553 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(keys::kLeftKey, |
| 513 &bounds_val)); | 554 &bounds_val)); |
| 514 window_bounds.set_x(bounds_val); | 555 window_bounds.set_x(bounds_val); |
| 515 popup_bounds.set_x(bounds_val); | |
| 516 panel_bounds.set_x(bounds_val); | |
| 517 } | 556 } |
| 518 | 557 |
| 519 if (args->HasKey(keys::kTopKey)) { | 558 if (args->HasKey(keys::kTopKey)) { |
| 520 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(keys::kTopKey, | 559 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(keys::kTopKey, |
| 521 &bounds_val)); | 560 &bounds_val)); |
| 522 window_bounds.set_y(bounds_val); | 561 window_bounds.set_y(bounds_val); |
| 523 popup_bounds.set_y(bounds_val); | |
| 524 panel_bounds.set_y(bounds_val); | |
| 525 } | 562 } |
| 526 | 563 |
| 527 if (args->HasKey(keys::kWidthKey)) { | 564 if (args->HasKey(keys::kWidthKey)) { |
| 528 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(keys::kWidthKey, | 565 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(keys::kWidthKey, |
| 529 &bounds_val)); | 566 &bounds_val)); |
| 530 window_bounds.set_width(bounds_val); | 567 window_bounds.set_width(bounds_val); |
| 531 popup_bounds.set_width(bounds_val); | |
| 532 panel_bounds.set_width(bounds_val); | |
| 533 } | 568 } |
| 534 | 569 |
| 535 if (args->HasKey(keys::kHeightKey)) { | 570 if (args->HasKey(keys::kHeightKey)) { |
| 536 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(keys::kHeightKey, | 571 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(keys::kHeightKey, |
| 537 &bounds_val)); | 572 &bounds_val)); |
| 538 window_bounds.set_height(bounds_val); | 573 window_bounds.set_height(bounds_val); |
| 539 popup_bounds.set_height(bounds_val); | |
| 540 panel_bounds.set_height(bounds_val); | |
| 541 } | 574 } |
| 542 | 575 |
| 543 if (args->HasKey(keys::kFocusedKey)) { | 576 if (args->HasKey(keys::kFocusedKey)) { |
| 544 EXTENSION_FUNCTION_VALIDATE(args->GetBoolean(keys::kFocusedKey, | 577 EXTENSION_FUNCTION_VALIDATE(args->GetBoolean(keys::kFocusedKey, |
| 545 &focused)); | 578 &focused)); |
| 546 saw_focus_key = true; | 579 saw_focus_key = true; |
| 547 } | 580 } |
| 548 | |
| 549 std::string type_str; | |
| 550 if (args->HasKey(keys::kWindowTypeKey)) { | |
| 551 EXTENSION_FUNCTION_VALIDATE(args->GetString(keys::kWindowTypeKey, | |
| 552 &type_str)); | |
| 553 if (type_str == keys::kWindowTypeValuePopup) { | |
| 554 window_type = Browser::TYPE_POPUP; | |
| 555 extension_id = GetExtension()->id(); | |
| 556 } else if (type_str == keys::kWindowTypeValuePanel) { | |
| 557 extension_id = GetExtension()->id(); | |
| 558 bool use_panels = false; | |
| 559 #if !defined(OS_ANDROID) | |
| 560 use_panels = PanelManager::ShouldUsePanels(extension_id); | |
| 561 #endif | |
| 562 if (use_panels) | |
| 563 window_type = Browser::TYPE_PANEL; | |
| 564 else | |
| 565 window_type = Browser::TYPE_POPUP; | |
| 566 } else if (type_str != keys::kWindowTypeValueNormal) { | |
| 567 error_ = keys::kInvalidWindowTypeError; | |
| 568 return false; | |
| 569 } | |
| 570 } | |
| 571 } | 581 } |
| 572 | 582 |
| 573 if (window_type == Browser::TYPE_PANEL) { | 583 #if !defined(USE_ASH) |
| 584 if (window_type == Browser::TYPE_PANEL && |
| 585 PanelManager::UseBrowserlessPanels()) { |
| 574 std::string title = | 586 std::string title = |
| 575 web_app::GenerateApplicationNameFromExtensionId(extension_id); | 587 web_app::GenerateApplicationNameFromExtensionId(extension_id); |
| 576 #if !defined(USE_ASH) | 588 // Note: Panels ignore all but the first url provided. |
| 577 if (PanelManager::UseBrowserlessPanels()) { | 589 Panel* panel = PanelManager::GetInstance()->CreatePanel( |
| 578 // Note: Panels ignore all but the first url provided. | 590 title, window_profile, urls[0], window_bounds, panel_create_mode); |
| 579 Panel* panel = PanelManager::GetInstance()->CreatePanel( | |
| 580 title, window_profile, urls[0], panel_bounds.size()); | |
| 581 | 591 |
| 582 // Unlike other window types, Panels do not take focus by default. | 592 // Unlike other window types, Panels do not take focus by default. |
| 583 if (!saw_focus_key || !focused) | 593 if (!saw_focus_key || !focused) |
| 584 panel->ShowInactive(); | 594 panel->ShowInactive(); |
| 585 else | 595 else |
| 586 panel->Show(); | 596 panel->Show(); |
| 587 | 597 |
| 588 SetResult( | 598 SetResult( |
| 589 panel->extension_window_controller()->CreateWindowValueWithTabs()); | 599 panel->extension_window_controller()->CreateWindowValueWithTabs()); |
| 590 return true; | 600 return true; |
| 591 } | 601 } |
| 602 // else fall through to create BrowserWindow |
| 592 #endif | 603 #endif |
| 593 // else fall through to create BrowserWindow | |
| 594 } | |
| 595 | 604 |
| 596 // Create a new BrowserWindow. | 605 // Create a new BrowserWindow. |
| 597 Browser::CreateParams create_params(window_type, window_profile); | 606 Browser::CreateParams create_params(window_type, window_profile); |
| 598 if (extension_id.empty()) { | 607 if (extension_id.empty()) { |
| 599 create_params.initial_bounds = window_bounds; | 608 create_params.initial_bounds = window_bounds; |
| 600 } else { | 609 } else { |
| 601 create_params = Browser::CreateParams::CreateForApp( | 610 create_params = Browser::CreateParams::CreateForApp( |
| 602 window_type, | 611 window_type, |
| 603 web_app::GenerateApplicationNameFromExtensionId(extension_id), | 612 web_app::GenerateApplicationNameFromExtensionId(extension_id), |
| 604 (window_type == Browser::TYPE_PANEL ? panel_bounds : popup_bounds), | 613 window_bounds, |
| 605 window_profile); | 614 window_profile); |
| 606 } | 615 } |
| 607 create_params.initial_show_state = ui::SHOW_STATE_NORMAL; | 616 create_params.initial_show_state = ui::SHOW_STATE_NORMAL; |
| 608 | 617 |
| 609 Browser* new_window = CreateBrowserWindow(create_params, window_profile, | 618 Browser* new_window = CreateBrowserWindow(create_params, window_profile, |
| 610 extension_id); | 619 extension_id); |
| 611 | 620 |
| 612 for (std::vector<GURL>::iterator i = urls.begin(); i != urls.end(); ++i) { | 621 for (std::vector<GURL>::iterator i = urls.begin(); i != urls.end(); ++i) { |
| 613 TabContents* tab = chrome::AddSelectedTabWithURL( | 622 TabContents* tab = chrome::AddSelectedTabWithURL( |
| 614 new_window, *i, content::PAGE_TRANSITION_LINK); | 623 new_window, *i, content::PAGE_TRANSITION_LINK); |
| (...skipping 1207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1822 // called for every API call the extension made. | 1831 // called for every API call the extension made. |
| 1823 GotLanguage(language); | 1832 GotLanguage(language); |
| 1824 } | 1833 } |
| 1825 | 1834 |
| 1826 void DetectTabLanguageFunction::GotLanguage(const std::string& language) { | 1835 void DetectTabLanguageFunction::GotLanguage(const std::string& language) { |
| 1827 SetResult(Value::CreateStringValue(language.c_str())); | 1836 SetResult(Value::CreateStringValue(language.c_str())); |
| 1828 SendResponse(true); | 1837 SendResponse(true); |
| 1829 | 1838 |
| 1830 Release(); // Balanced in Run() | 1839 Release(); // Balanced in Run() |
| 1831 } | 1840 } |
| OLD | NEW |