OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_web_contents_data.h" | 5 #include "chrome/browser/memory/tab_manager_web_contents_data.h" |
6 | 6 |
7 #include "base/metrics/histogram.h" | 7 #include "base/metrics/histogram.h" |
8 #include "content/public/browser/browser_thread.h" | 8 #include "content/public/browser/browser_thread.h" |
9 #include "content/public/browser/web_contents.h" | 9 #include "content/public/browser/web_contents.h" |
10 | 10 |
11 using base::TimeTicks; | 11 using base::TimeTicks; |
12 using content::WebContents; | 12 using content::WebContents; |
13 | 13 |
14 namespace { | 14 DEFINE_WEB_CONTENTS_USER_DATA_KEY(memory::TabManager::WebContentsData); |
15 | |
16 const char kDiscardStateKey[] = "WebContentsData"; | |
17 | |
18 } // namespace | |
19 | 15 |
20 namespace memory { | 16 namespace memory { |
21 | 17 |
22 // static | 18 TabManager::WebContentsData::WebContentsData(content::WebContents* web_contents) |
23 TabManager::WebContentsData* TabManager::WebContentsData::Get( | 19 : WebContentsObserver(web_contents) {} |
24 WebContents* web_contents) { | |
25 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
26 TabManager::WebContentsData* discard_state = static_cast<WebContentsData*>( | |
27 web_contents->GetUserData(&kDiscardStateKey)); | |
28 | 20 |
29 // If this function is called, we probably need to query/change the discard | 21 TabManager::WebContentsData::~WebContentsData() {} |
30 // state. Let's go ahead a add one. | 22 |
31 if (!discard_state) { | 23 void TabManager::WebContentsData::DidStartLoading() { |
32 discard_state = new WebContentsData; | 24 // Marks the tab as no longer discarded if it has been reloaded from another |
33 web_contents->SetUserData(&kDiscardStateKey, discard_state); | 25 // source (ie: context menu). |
| 26 SetDiscardState(false); |
| 27 } |
| 28 |
| 29 bool TabManager::WebContentsData::IsDiscarded() { |
| 30 return tab_data_.is_discarded_; |
| 31 } |
| 32 |
| 33 void TabManager::WebContentsData::SetDiscardState(bool state) { |
| 34 if (tab_data_.is_discarded_ && !state) { |
| 35 static int reload_count = 0; |
| 36 UMA_HISTOGRAM_CUSTOM_COUNTS("TabManager.Discarding.ReloadCount", |
| 37 ++reload_count, 1, 1000, 50); |
| 38 auto delta = base::TimeTicks::Now() - tab_data_.last_discard_time_; |
| 39 // Capped to one day for now, will adjust if necessary. |
| 40 UMA_HISTOGRAM_CUSTOM_TIMES("TabManager.Discarding.DiscardToReloadTime", |
| 41 delta, base::TimeDelta::FromSeconds(1), |
| 42 base::TimeDelta::FromDays(1), 100); |
| 43 } else if (!tab_data_.is_discarded_ && state) { |
| 44 static int discard_count = 0; |
| 45 UMA_HISTOGRAM_CUSTOM_COUNTS("TabManager.Discarding.DiscardCount", |
| 46 ++discard_count, 1, 1000, 50); |
| 47 tab_data_.last_discard_time_ = base::TimeTicks::Now(); |
34 } | 48 } |
35 | 49 |
36 return discard_state; | 50 tab_data_.is_discarded_ = state; |
37 } | 51 } |
38 | 52 |
39 // static | 53 int TabManager::WebContentsData::DiscardCount() { |
40 void TabManager::WebContentsData::Set(WebContents* web_contents, | 54 return tab_data_.discard_count_; |
41 WebContentsData* state) { | 55 } |
42 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 56 |
43 web_contents->SetUserData(&kDiscardStateKey, state); | 57 void TabManager::WebContentsData::IncrementDiscardCount() { |
| 58 tab_data_.discard_count_++; |
| 59 } |
| 60 |
| 61 bool TabManager::WebContentsData::IsRecentlyAudible() { |
| 62 return tab_data_.is_recently_audible_; |
| 63 } |
| 64 |
| 65 void TabManager::WebContentsData::SetRecentlyAudible(bool state) { |
| 66 tab_data_.is_recently_audible_ = state; |
| 67 } |
| 68 |
| 69 TimeTicks TabManager::WebContentsData::LastAudioChangeTime() { |
| 70 return tab_data_.last_audio_change_time_; |
| 71 } |
| 72 |
| 73 void TabManager::WebContentsData::SetLastAudioChangeTime(TimeTicks timestamp) { |
| 74 tab_data_.last_audio_change_time_ = timestamp; |
44 } | 75 } |
45 | 76 |
46 // static | 77 // static |
47 void TabManager::WebContentsData::CopyState( | 78 void TabManager::WebContentsData::CopyState( |
48 content::WebContents* old_contents, | 79 content::WebContents* old_contents, |
49 content::WebContents* new_contents) { | 80 content::WebContents* new_contents) { |
50 WebContentsData* old_state = Get(old_contents); | 81 // Only copy if an existing state is found. |
51 WebContentsData* new_State = Get(new_contents); | 82 if (FromWebContents(old_contents)) { |
52 *new_State = *old_state; | 83 CreateForWebContents(new_contents); |
| 84 FromWebContents(new_contents)->tab_data_ = |
| 85 FromWebContents(old_contents)->tab_data_; |
| 86 } |
53 } | 87 } |
54 | 88 |
55 // static | 89 TabManager::WebContentsData::Data::Data() |
56 bool TabManager::WebContentsData::IsDiscarded(WebContents* web_contents) { | |
57 return TabManager::WebContentsData::Get(web_contents)->is_discarded_; | |
58 } | |
59 | |
60 // static | |
61 void TabManager::WebContentsData::SetDiscardState(WebContents* web_contents, | |
62 bool state) { | |
63 WebContentsData* discard_state = | |
64 TabManager::WebContentsData::Get(web_contents); | |
65 if (discard_state->is_discarded_ && !state) { | |
66 static int reload_count = 0; | |
67 UMA_HISTOGRAM_CUSTOM_COUNTS("TabManager.Discarding.ReloadCount", | |
68 ++reload_count, 1, 1000, 50); | |
69 auto delta = base::TimeTicks::Now() - discard_state->last_discard_time_; | |
70 // Capped to one day for now, will adjust if necessary. | |
71 UMA_HISTOGRAM_CUSTOM_TIMES("TabManager.Discarding.DiscardToReloadTime", | |
72 delta, base::TimeDelta::FromSeconds(1), | |
73 base::TimeDelta::FromDays(1), 100); | |
74 } else if (!discard_state->is_discarded_ && state) { | |
75 static int discard_count = 0; | |
76 UMA_HISTOGRAM_CUSTOM_COUNTS("TabManager.Discarding.DiscardCount", | |
77 ++discard_count, 1, 1000, 50); | |
78 discard_state->last_discard_time_ = base::TimeTicks::Now(); | |
79 } | |
80 | |
81 discard_state->is_discarded_ = state; | |
82 } | |
83 | |
84 // static | |
85 int TabManager::WebContentsData::DiscardCount(WebContents* web_contents) { | |
86 return TabManager::WebContentsData::Get(web_contents)->discard_count_; | |
87 } | |
88 | |
89 // static | |
90 void TabManager::WebContentsData::IncrementDiscardCount( | |
91 WebContents* web_contents) { | |
92 TabManager::WebContentsData::Get(web_contents)->discard_count_++; | |
93 } | |
94 | |
95 // static | |
96 bool TabManager::WebContentsData::IsRecentlyAudible( | |
97 content::WebContents* web_contents) { | |
98 return TabManager::WebContentsData::Get(web_contents)->is_recently_audible_; | |
99 } | |
100 | |
101 // static | |
102 void TabManager::WebContentsData::SetRecentlyAudible( | |
103 content::WebContents* web_contents, | |
104 bool state) { | |
105 TabManager::WebContentsData::Get(web_contents)->is_recently_audible_ = state; | |
106 } | |
107 | |
108 // static | |
109 TimeTicks TabManager::WebContentsData::LastAudioChangeTime( | |
110 content::WebContents* web_contents) { | |
111 return TabManager::WebContentsData::Get(web_contents) | |
112 ->last_audio_change_time_; | |
113 } | |
114 | |
115 // static | |
116 void TabManager::WebContentsData::SetLastAudioChangeTime( | |
117 content::WebContents* web_contents, | |
118 TimeTicks timestamp) { | |
119 TabManager::WebContentsData::Get(web_contents)->last_audio_change_time_ = | |
120 timestamp; | |
121 } | |
122 | |
123 TabManager::WebContentsData::WebContentsData() | |
124 : is_discarded_(false), | 90 : is_discarded_(false), |
125 discard_count_(0), | 91 discard_count_(0), |
126 is_recently_audible_(false), | 92 is_recently_audible_(false), |
127 last_audio_change_time_(TimeTicks::UnixEpoch()) {} | 93 last_audio_change_time_(TimeTicks::UnixEpoch()) {} |
128 | 94 |
129 } // namespace memory | 95 } // namespace memory |
OLD | NEW |