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/memory/tab_manager.cc

Issue 1914143002: Experimental 'purging and suspending' backgrounded tabs behind the flag (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 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
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/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
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 426 matching lines...) Expand 10 before | Expand all | Expand 10 after
480 bool is_active_window = browser->window()->IsActive(); 481 bool is_active_window = browser->window()->IsActive();
481 AddTabStats(browser->tab_strip_model(), browser->is_app(), is_active_window, 482 AddTabStats(browser->tab_strip_model(), browser->is_app(), is_active_window,
482 stats_list); 483 stats_list);
483 } 484 }
484 } 485 }
485 486
486 void TabManager::AddTabStats(const TabStripModel* model, 487 void TabManager::AddTabStats(const TabStripModel* model,
487 bool is_app, 488 bool is_app,
488 bool active_model, 489 bool active_model,
489 TabStatsList* stats_list) { 490 TabStatsList* stats_list) {
490 for (int i = 0; i < model->count(); i++) { 491 for (int i = 0; i < model->count(); i++) {
491 WebContents* contents = model->GetWebContentsAt(i); 492 WebContents* contents = model->GetWebContentsAt(i);
492 if (!contents->IsCrashed()) { 493 if (!contents->IsCrashed()) {
493 TabStats stats; 494 TabStats stats;
494 stats.is_app = is_app; 495 stats.is_app = is_app;
495 stats.is_internal_page = 496 stats.is_internal_page =
496 IsInternalPage(contents->GetLastCommittedURL()); 497 IsInternalPage(contents->GetLastCommittedURL());
497 stats.is_media = IsMediaTab(contents); 498 stats.is_media = IsMediaTab(contents);
498 stats.is_pinned = model->IsTabPinned(i); 499 stats.is_pinned = model->IsTabPinned(i);
499 stats.is_selected = active_model && model->IsTabSelected(i); 500 stats.is_selected = active_model && model->IsTabSelected(i);
500 stats.is_discarded = GetWebContentsData(contents)->IsDiscarded(); 501 stats.is_discarded = GetWebContentsData(contents)->IsDiscarded();
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
537 last_discard_time_ += suspend_time; 538 last_discard_time_ += suspend_time;
538 } 539 }
539 } 540 }
540 last_adjust_time_ = NowTicks(); 541 last_adjust_time_ = NowTicks();
541 542
542 #if defined(OS_CHROMEOS) 543 #if defined(OS_CHROMEOS)
543 TabStatsList stats_list = GetTabStats(); 544 TabStatsList stats_list = GetTabStats();
544 // This starts the CrOS specific OOM adjustments in /proc/<pid>/oom_score_adj. 545 // This starts the CrOS specific OOM adjustments in /proc/<pid>/oom_score_adj.
545 delegate_->AdjustOomPriorities(stats_list); 546 delegate_->AdjustOomPriorities(stats_list);
546 #endif 547 #endif
548
549 PurgeAndSuspendBackgroundedTabs();
550 }
551
552 void TabManager::PurgeAndSuspendBackgroundedTabs() {
553 const base::CommandLine& command_line =
554 *base::CommandLine::ForCurrentProcess();
555 if (!command_line.HasSwitch(switches::kPurgeAndSuspendTime))
556 return;
557 int purge_and_suspend_time = 0;
558 if (!base::StringToInt(
559 command_line.GetSwitchValueASCII(switches::kPurgeAndSuspendTime),
560 &purge_and_suspend_time)) {
561 return;
562 }
563 if (purge_and_suspend_time <= 0)
564 return;
565 auto purge_and_suspend_time_delta = base::TimeDelta::FromSeconds(
566 purge_and_suspend_time);
567 auto tab_stats = GetUnsortedTabStats();
568 for (auto& tab : tab_stats) {
569 if (!tab.render_process_host->IsProcessBackgrounded())
570 continue;
571 // TODO(hajimehoshi): Now calling PurgeAndSuspend is implemented without
572 // timers for simplicity, so PurgeAndSuspend is called even after the
573 // renderer is purged and suspended once.
574 if (NowTicks() - tab.last_active < purge_and_suspend_time_delta)
575 continue;
576 tab.render_process_host->PurgeAndSuspend();
577 }
547 } 578 }
548 579
549 bool TabManager::CanDiscardTab(int64_t target_web_contents_id) const { 580 bool TabManager::CanDiscardTab(int64_t target_web_contents_id) const {
550 TabStripModel* model; 581 TabStripModel* model;
551 int idx = FindTabStripModelById(target_web_contents_id, &model); 582 int idx = FindTabStripModelById(target_web_contents_id, &model);
552 583
553 if (idx == -1) 584 if (idx == -1)
554 return false; 585 return false;
555 586
556 WebContents* web_contents = model->GetWebContentsAt(idx); 587 WebContents* web_contents = model->GetWebContentsAt(idx);
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after
847 } 878 }
848 } else { 879 } else {
849 // The code here can only be tested under a full browser test. 880 // The code here can only be tested under a full browser test.
850 AddTabStats(&stats_list); 881 AddTabStats(&stats_list);
851 } 882 }
852 883
853 return stats_list; 884 return stats_list;
854 } 885 }
855 886
856 } // namespace memory 887 } // namespace memory
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698