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

Side by Side Diff: chrome/browser/memory/tab_manager.cc

Issue 2250183002: New Public IdFromWebContents method on TabManager. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: implemented suggestion Created 4 years, 4 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 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 #endif 81 #endif
82 82
83 // If there has been no priority adjustment in this interval, assume the 83 // If there has been no priority adjustment in this interval, assume the
84 // machine was suspended and correct the timing statistics. 84 // machine was suspended and correct the timing statistics.
85 const int kSuspendThresholdSeconds = kAdjustmentIntervalSeconds * 4; 85 const int kSuspendThresholdSeconds = kAdjustmentIntervalSeconds * 4;
86 86
87 // The time during which a tab is protected from discarding after it stops being 87 // The time during which a tab is protected from discarding after it stops being
88 // audible. 88 // audible.
89 const int kAudioProtectionTimeSeconds = 60; 89 const int kAudioProtectionTimeSeconds = 60;
90 90
91 // Returns a unique ID for a WebContents. Do not cast back to a pointer, as
92 // the WebContents could be deleted if the user closed the tab.
93 int64_t IdFromWebContents(WebContents* web_contents) {
94 return reinterpret_cast<int64_t>(web_contents);
95 }
96
97 int FindTabStripModelById(int64_t target_web_contents_id, 91 int FindTabStripModelById(int64_t target_web_contents_id,
98 TabStripModel** model) { 92 TabStripModel** model) {
99 DCHECK(model); 93 DCHECK(model);
100 for (auto* browser : *BrowserList::GetInstance()) { 94 for (auto* browser : *BrowserList::GetInstance()) {
101 TabStripModel* local_model = browser->tab_strip_model(); 95 TabStripModel* local_model = browser->tab_strip_model();
102 for (int idx = 0; idx < local_model->count(); idx++) { 96 for (int idx = 0; idx < local_model->count(); idx++) {
103 WebContents* web_contents = local_model->GetWebContentsAt(idx); 97 WebContents* web_contents = local_model->GetWebContentsAt(idx);
104 int64_t web_contents_id = IdFromWebContents(web_contents); 98 int64_t web_contents_id = TabManager::IdFromWebContents(web_contents);
105 if (web_contents_id == target_web_contents_id) { 99 if (web_contents_id == target_web_contents_id) {
106 *model = local_model; 100 *model = local_model;
107 return idx; 101 return idx;
108 } 102 }
109 } 103 }
110 } 104 }
111 105
112 return -1; 106 return -1;
113 } 107 }
114 108
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 if (index == -1) 339 if (index == -1)
346 return nullptr; 340 return nullptr;
347 341
348 VLOG(1) << "Discarding tab " << index << " id " << target_web_contents_id; 342 VLOG(1) << "Discarding tab " << index << " id " << target_web_contents_id;
349 343
350 return DiscardWebContentsAt(index, model); 344 return DiscardWebContentsAt(index, model);
351 } 345 }
352 346
353 WebContents* TabManager::DiscardTabByExtension(content::WebContents* contents) { 347 WebContents* TabManager::DiscardTabByExtension(content::WebContents* contents) {
354 if (contents) 348 if (contents)
355 return DiscardTabById(reinterpret_cast<int64_t>(contents)); 349 return DiscardTabById(IdFromWebContents(contents));
356 350
357 return DiscardTabImpl(); 351 return DiscardTabImpl();
358 } 352 }
359 353
360 void TabManager::LogMemoryAndDiscardTab() { 354 void TabManager::LogMemoryAndDiscardTab() {
361 LogMemory("Tab Discards Memory details", 355 LogMemory("Tab Discards Memory details",
362 base::Bind(&TabManager::PurgeMemoryAndDiscardTab)); 356 base::Bind(&TabManager::PurgeMemoryAndDiscardTab));
363 } 357 }
364 358
365 void TabManager::LogMemory(const std::string& title, 359 void TabManager::LogMemory(const std::string& title,
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
458 // order. This is currently not done because pages with unload handlers set 452 // order. This is currently not done because pages with unload handlers set
459 // sudden_termination_allowed false, and that covers too many common pages 453 // sudden_termination_allowed false, and that covers too many common pages
460 // with ad networks and statistics scripts. Ideally check for beforeUnload 454 // with ad networks and statistics scripts. Ideally check for beforeUnload
461 // handlers, which are likely to present a dialog asking if the user wants to 455 // handlers, which are likely to present a dialog asking if the user wants to
462 // discard state. crbug.com/123049. 456 // discard state. crbug.com/123049.
463 457
464 // Being more recently active is more important. 458 // Being more recently active is more important.
465 return first.last_active > second.last_active; 459 return first.last_active > second.last_active;
466 } 460 }
467 461
462 // static
463 int64_t TabManager::IdFromWebContents(WebContents* web_contents) {
464 return reinterpret_cast<int64_t>(web_contents);
465 }
466
468 /////////////////////////////////////////////////////////////////////////////// 467 ///////////////////////////////////////////////////////////////////////////////
469 // TabManager, private: 468 // TabManager, private:
470 469
471 void TabManager::OnDiscardedStateChange(content::WebContents* contents, 470 void TabManager::OnDiscardedStateChange(content::WebContents* contents,
472 bool is_discarded) { 471 bool is_discarded) {
473 FOR_EACH_OBSERVER(TabManagerObserver, observers_, 472 FOR_EACH_OBSERVER(TabManagerObserver, observers_,
474 OnDiscardedStateChange(contents, is_discarded)); 473 OnDiscardedStateChange(contents, is_discarded));
475 } 474 }
476 475
477 void TabManager::OnAutoDiscardableStateChange(content::WebContents* contents, 476 void TabManager::OnAutoDiscardableStateChange(content::WebContents* contents,
(...skipping 460 matching lines...) Expand 10 before | Expand all | Expand 10 after
938 // platform. 937 // platform.
939 std::string allow_multiple_discards = variations::GetVariationParamValue( 938 std::string allow_multiple_discards = variations::GetVariationParamValue(
940 features::kAutomaticTabDiscarding.name, "AllowMultipleDiscards"); 939 features::kAutomaticTabDiscarding.name, "AllowMultipleDiscards");
941 return (allow_multiple_discards != "true"); 940 return (allow_multiple_discards != "true");
942 #else 941 #else
943 return false; 942 return false;
944 #endif 943 #endif
945 } 944 }
946 945
947 } // namespace memory 946 } // namespace memory
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698