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 void TabManager::WebContentsData::DidStartLoading() { |
23 TabManager::WebContentsData* TabManager::WebContentsData::Get( | 19 // Marks the tab as no longer discarded if it has been reloaded from another |
24 WebContents* web_contents) { | 20 // source (ie: context menu). |
25 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 21 WebContentsData::FromWebContents(web_contents())->SetDiscardState(false); |
sky
2015/10/27 21:16:46
Isn't WebContentsData::FromWebContents(web_content
Georges Khalil
2015/10/30 14:17:44
Yep. SetDiscardedState was static at first but I c
| |
26 TabManager::WebContentsData* discard_state = static_cast<WebContentsData*>( | 22 } |
27 web_contents->GetUserData(&kDiscardStateKey)); | |
28 | 23 |
29 // If this function is called, we probably need to query/change the discard | 24 bool TabManager::WebContentsData::IsDiscarded() { |
30 // state. Let's go ahead a add one. | 25 return tab_data_.is_discarded_; |
31 if (!discard_state) { | 26 } |
32 discard_state = new WebContentsData; | 27 |
33 web_contents->SetUserData(&kDiscardStateKey, discard_state); | 28 void TabManager::WebContentsData::SetDiscardState(bool state) { |
29 if (tab_data_.is_discarded_ && !state) { | |
30 static int reload_count = 0; | |
31 UMA_HISTOGRAM_CUSTOM_COUNTS("TabManager.Discarding.ReloadCount", | |
32 ++reload_count, 1, 1000, 50); | |
33 auto delta = base::TimeTicks::Now() - tab_data_.last_discard_time_; | |
34 // Capped to one day for now, will adjust if necessary. | |
35 UMA_HISTOGRAM_CUSTOM_TIMES("TabManager.Discarding.DiscardToReloadTime", | |
36 delta, base::TimeDelta::FromSeconds(1), | |
37 base::TimeDelta::FromDays(1), 100); | |
38 } else if (!tab_data_.is_discarded_ && state) { | |
39 static int discard_count = 0; | |
40 UMA_HISTOGRAM_CUSTOM_COUNTS("TabManager.Discarding.DiscardCount", | |
41 ++discard_count, 1, 1000, 50); | |
42 tab_data_.last_discard_time_ = base::TimeTicks::Now(); | |
34 } | 43 } |
35 | 44 |
36 return discard_state; | 45 tab_data_.is_discarded_ = state; |
37 } | 46 } |
38 | 47 |
39 // static | 48 int TabManager::WebContentsData::DiscardCount() { |
40 void TabManager::WebContentsData::Set(WebContents* web_contents, | 49 return tab_data_.discard_count_; |
41 WebContentsData* state) { | 50 } |
42 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 51 |
43 web_contents->SetUserData(&kDiscardStateKey, state); | 52 void TabManager::WebContentsData::IncrementDiscardCount() { |
53 tab_data_.discard_count_++; | |
54 } | |
55 | |
56 bool TabManager::WebContentsData::IsRecentlyAudible() { | |
57 return tab_data_.is_recently_audible_; | |
58 } | |
59 | |
60 void TabManager::WebContentsData::SetRecentlyAudible(bool state) { | |
61 tab_data_.is_recently_audible_ = state; | |
62 } | |
63 | |
64 TimeTicks TabManager::WebContentsData::LastAudioChangeTime() { | |
65 return tab_data_.last_audio_change_time_; | |
66 } | |
67 | |
68 void TabManager::WebContentsData::SetLastAudioChangeTime(TimeTicks timestamp) { | |
69 tab_data_.last_audio_change_time_ = timestamp; | |
44 } | 70 } |
45 | 71 |
46 // static | 72 // static |
47 void TabManager::WebContentsData::CopyState( | 73 void TabManager::WebContentsData::CopyState( |
48 content::WebContents* old_contents, | 74 content::WebContents* old_contents, |
49 content::WebContents* new_contents) { | 75 content::WebContents* new_contents) { |
50 WebContentsData* old_state = Get(old_contents); | 76 // Only copy if an existing state is found. |
51 WebContentsData* new_State = Get(new_contents); | 77 if (FromWebContents(old_contents)) { |
52 *new_State = *old_state; | 78 CreateForWebContents(new_contents); |
79 FromWebContents(new_contents)->tab_data_ = | |
80 FromWebContents(old_contents)->tab_data_; | |
81 } | |
53 } | 82 } |
54 | 83 |
55 // static | 84 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), | 85 : is_discarded_(false), |
125 discard_count_(0), | 86 discard_count_(0), |
126 is_recently_audible_(false), | 87 is_recently_audible_(false), |
127 last_audio_change_time_(TimeTicks::UnixEpoch()) {} | 88 last_audio_change_time_(TimeTicks::UnixEpoch()) {} |
128 | 89 |
129 } // namespace memory | 90 } // namespace memory |
OLD | NEW |