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

Side by Side Diff: chrome/browser/extensions/api/tabs/tabs.cc

Issue 10919046: Allow panels to be created as detached panels. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Synced Created 8 years, 3 months 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) 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
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());
jianli 2012/09/04 21:58:47 Why not just passing (0, 0) and letting PanelManag
jennb 2012/09/04 23:02:44 (0, 0) is a valid position. We wanted a way to dis
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 GetExtension())); 600 GetExtension()));
591 return true; 601 return true;
592 } 602 }
603 // else fall through to create BrowserWindow
593 #endif 604 #endif
594 // else fall through to create BrowserWindow
595 }
596 605
597 // Create a new BrowserWindow. 606 // Create a new BrowserWindow.
598 Browser::CreateParams create_params(window_type, window_profile); 607 Browser::CreateParams create_params(window_type, window_profile);
599 if (extension_id.empty()) { 608 if (extension_id.empty()) {
600 create_params.initial_bounds = window_bounds; 609 create_params.initial_bounds = window_bounds;
601 } else { 610 } else {
602 create_params = Browser::CreateParams::CreateForApp( 611 create_params = Browser::CreateParams::CreateForApp(
603 window_type, 612 window_type,
604 web_app::GenerateApplicationNameFromExtensionId(extension_id), 613 web_app::GenerateApplicationNameFromExtensionId(extension_id),
605 (window_type == Browser::TYPE_PANEL ? panel_bounds : popup_bounds), 614 window_bounds,
606 window_profile); 615 window_profile);
607 } 616 }
608 create_params.initial_show_state = ui::SHOW_STATE_NORMAL; 617 create_params.initial_show_state = ui::SHOW_STATE_NORMAL;
609 618
610 Browser* new_window = CreateBrowserWindow(create_params, window_profile, 619 Browser* new_window = CreateBrowserWindow(create_params, window_profile,
611 extension_id); 620 extension_id);
612 621
613 for (std::vector<GURL>::iterator i = urls.begin(); i != urls.end(); ++i) { 622 for (std::vector<GURL>::iterator i = urls.begin(); i != urls.end(); ++i) {
614 TabContents* tab = chrome::AddSelectedTabWithURL( 623 TabContents* tab = chrome::AddSelectedTabWithURL(
615 new_window, *i, content::PAGE_TRANSITION_LINK); 624 new_window, *i, content::PAGE_TRANSITION_LINK);
(...skipping 1212 matching lines...) Expand 10 before | Expand all | Expand 10 after
1828 // called for every API call the extension made. 1837 // called for every API call the extension made.
1829 GotLanguage(language); 1838 GotLanguage(language);
1830 } 1839 }
1831 1840
1832 void DetectTabLanguageFunction::GotLanguage(const std::string& language) { 1841 void DetectTabLanguageFunction::GotLanguage(const std::string& language) {
1833 SetResult(Value::CreateStringValue(language.c_str())); 1842 SetResult(Value::CreateStringValue(language.c_str()));
1834 SendResponse(true); 1843 SendResponse(true);
1835 1844
1836 Release(); // Balanced in Run() 1845 Release(); // Balanced in Run()
1837 } 1846 }
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/extensions/api/tabs/tabs_constants.h » ('j') | chrome/browser/ui/panels/detached_panel_strip.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698