Chromium Code Reviews| 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/memory/tab_manager.h" | 5 #include "chrome/browser/memory/tab_manager.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <set> | 10 #include <set> |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 34 #include "chrome/browser/memory/tab_manager_web_contents_data.h" | 34 #include "chrome/browser/memory/tab_manager_web_contents_data.h" |
| 35 #include "chrome/browser/profiles/profile.h" | 35 #include "chrome/browser/profiles/profile.h" |
| 36 #include "chrome/browser/ui/browser.h" | 36 #include "chrome/browser/ui/browser.h" |
| 37 #include "chrome/browser/ui/browser_list.h" | 37 #include "chrome/browser/ui/browser_list.h" |
| 38 #include "chrome/browser/ui/browser_window.h" | 38 #include "chrome/browser/ui/browser_window.h" |
| 39 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" | 39 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" |
| 40 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 40 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 41 #include "chrome/browser/ui/tabs/tab_utils.h" | 41 #include "chrome/browser/ui/tabs/tab_utils.h" |
| 42 #include "chrome/common/chrome_constants.h" | 42 #include "chrome/common/chrome_constants.h" |
| 43 #include "chrome/common/chrome_features.h" | 43 #include "chrome/common/chrome_features.h" |
| 44 #include "chrome/common/chrome_switches.h" | |
| 44 #include "chrome/common/url_constants.h" | 45 #include "chrome/common/url_constants.h" |
| 45 #include "components/metrics/system_memory_stats_recorder.h" | 46 #include "components/metrics/system_memory_stats_recorder.h" |
| 46 #include "components/variations/variations_associated_data.h" | 47 #include "components/variations/variations_associated_data.h" |
| 47 #include "content/public/browser/browser_thread.h" | 48 #include "content/public/browser/browser_thread.h" |
| 48 #include "content/public/browser/memory_pressure_controller.h" | 49 #include "content/public/browser/memory_pressure_controller.h" |
| 49 #include "content/public/browser/navigation_controller.h" | 50 #include "content/public/browser/navigation_controller.h" |
| 50 #include "content/public/browser/render_process_host.h" | 51 #include "content/public/browser/render_process_host.h" |
| 51 #include "content/public/browser/web_contents.h" | 52 #include "content/public/browser/web_contents.h" |
| 52 #include "content/public/common/page_importance_signals.h" | 53 #include "content/public/common/page_importance_signals.h" |
| 53 | 54 |
| (...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 477 bool is_active_window = browser->window()->IsActive(); | 478 bool is_active_window = browser->window()->IsActive(); |
| 478 AddTabStats(browser->tab_strip_model(), browser->is_app(), is_active_window, | 479 AddTabStats(browser->tab_strip_model(), browser->is_app(), is_active_window, |
| 479 stats_list); | 480 stats_list); |
| 480 } | 481 } |
| 481 } | 482 } |
| 482 | 483 |
| 483 void TabManager::AddTabStats(const TabStripModel* model, | 484 void TabManager::AddTabStats(const TabStripModel* model, |
| 484 bool is_app, | 485 bool is_app, |
| 485 bool active_model, | 486 bool active_model, |
| 486 TabStatsList* stats_list) { | 487 TabStatsList* stats_list) { |
| 487 for (int i = 0; i < model->count(); i++) { | 488 for (int i = 0; i < model->count(); i++) { |
| 488 WebContents* contents = model->GetWebContentsAt(i); | 489 WebContents* contents = model->GetWebContentsAt(i); |
| 489 if (!contents->IsCrashed()) { | 490 if (!contents->IsCrashed()) { |
| 490 TabStats stats; | 491 TabStats stats; |
| 491 stats.is_app = is_app; | 492 stats.is_app = is_app; |
| 492 stats.is_internal_page = | 493 stats.is_internal_page = |
| 493 IsInternalPage(contents->GetLastCommittedURL()); | 494 IsInternalPage(contents->GetLastCommittedURL()); |
| 494 stats.is_media = IsMediaTab(contents); | 495 stats.is_media = IsMediaTab(contents); |
| 495 stats.is_pinned = model->IsTabPinned(i); | 496 stats.is_pinned = model->IsTabPinned(i); |
| 496 stats.is_selected = active_model && model->IsTabSelected(i); | 497 stats.is_selected = active_model && model->IsTabSelected(i); |
| 497 stats.is_discarded = GetWebContentsData(contents)->IsDiscarded(); | 498 stats.is_discarded = GetWebContentsData(contents)->IsDiscarded(); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 534 last_discard_time_ += suspend_time; | 535 last_discard_time_ += suspend_time; |
| 535 } | 536 } |
| 536 } | 537 } |
| 537 last_adjust_time_ = NowTicks(); | 538 last_adjust_time_ = NowTicks(); |
| 538 | 539 |
| 539 #if defined(OS_CHROMEOS) | 540 #if defined(OS_CHROMEOS) |
| 540 TabStatsList stats_list = GetTabStats(); | 541 TabStatsList stats_list = GetTabStats(); |
| 541 // This starts the CrOS specific OOM adjustments in /proc/<pid>/oom_score_adj. | 542 // This starts the CrOS specific OOM adjustments in /proc/<pid>/oom_score_adj. |
| 542 delegate_->AdjustOomPriorities(stats_list); | 543 delegate_->AdjustOomPriorities(stats_list); |
| 543 #endif | 544 #endif |
| 545 | |
| 546 PurgeAndSuspendBackgroundedTabs(); | |
| 547 } | |
| 548 | |
| 549 void TabManager::PurgeAndSuspendBackgroundedTabs() { | |
| 550 const base::CommandLine& command_line = | |
| 551 *base::CommandLine::ForCurrentProcess(); | |
| 552 if (!command_line.HasSwitch(switches::kPurgeAndSuspendTime)) | |
| 553 return; | |
| 554 int purge_and_suspend_time = 0; | |
| 555 if (!base::StringToInt( | |
| 556 command_line.GetSwitchValueASCII(switches::kPurgeAndSuspendTime), | |
| 557 &purge_and_suspend_time)) { | |
| 558 return; | |
| 559 } | |
| 560 if (purge_and_suspend_time <= 0) | |
| 561 return; | |
| 562 auto purge_and_suspend_time_delta = base::TimeDelta::FromSeconds( | |
| 563 purge_and_suspend_time); | |
| 564 auto tab_stats = GetUnsortedTabStats(); | |
| 565 for (auto& tab : tab_stats) { | |
| 566 if (!tab.render_process_host->IsProcessBackgrounded()) | |
| 567 continue; | |
| 568 // TODO(hajimehoshi): Now calling PurgeAndSuspend is implemented without | |
| 569 // timers for simplicity, so PurgeAndSuspend is called even after the | |
| 570 // renderer is purged and suspended once. | |
| 571 if (NowTicks() - tab.last_active < purge_and_suspend_time_delta) | |
|
dcheng
2016/05/10 04:49:53
Nit: maybe just get NowTicks() once outside the lo
hajimehoshi
2016/05/10 06:54:11
Done.
| |
| 572 continue; | |
| 573 tab.render_process_host->PurgeAndSuspend(); | |
| 574 } | |
| 544 } | 575 } |
| 545 | 576 |
| 546 bool TabManager::CanDiscardTab(int64_t target_web_contents_id) const { | 577 bool TabManager::CanDiscardTab(int64_t target_web_contents_id) const { |
| 547 TabStripModel* model; | 578 TabStripModel* model; |
| 548 int idx = FindTabStripModelById(target_web_contents_id, &model); | 579 int idx = FindTabStripModelById(target_web_contents_id, &model); |
| 549 | 580 |
| 550 if (idx == -1) | 581 if (idx == -1) |
| 551 return false; | 582 return false; |
| 552 | 583 |
| 553 WebContents* web_contents = model->GetWebContentsAt(idx); | 584 WebContents* web_contents = model->GetWebContentsAt(idx); |
| (...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 844 } | 875 } |
| 845 } else { | 876 } else { |
| 846 // The code here can only be tested under a full browser test. | 877 // The code here can only be tested under a full browser test. |
| 847 AddTabStats(&stats_list); | 878 AddTabStats(&stats_list); |
| 848 } | 879 } |
| 849 | 880 |
| 850 return stats_list; | 881 return stats_list; |
| 851 } | 882 } |
| 852 | 883 |
| 853 } // namespace memory | 884 } // namespace memory |
| OLD | NEW |