| 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/android/data_usage/tab_data_use_entry.h" | 5 #include "chrome/browser/android/data_usage/tab_data_use_entry.h" |
| 6 | 6 |
| 7 #include "base/gtest_prod_util.h" | 7 #include "base/gtest_prod_util.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/metrics/histogram_macros.h" |
| 9 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 10 #include "chrome/browser/android/data_usage/external_data_use_observer.h" | 11 #include "chrome/browser/android/data_usage/external_data_use_observer.h" |
| 11 #include "components/variations/variations_associated_data.h" | 12 #include "components/variations/variations_associated_data.h" |
| 12 | 13 |
| 13 namespace { | 14 namespace { |
| 14 | 15 |
| 15 // Indicates the default maximum number of tracking session history to maintain | 16 // Indicates the default maximum number of tracking session history to maintain |
| 16 // per tab. May be overridden by the field trial. | 17 // per tab. May be overridden by the field trial. |
| 17 const size_t kDefaultMaxSessionsPerTab = 5; | 18 const size_t kDefaultMaxSessionsPerTab = 5; |
| 18 | 19 |
| 19 // Indicates the default expiration duration in seconds, after which it can be | 20 // Indicates the default expiration duration in seconds, after which it can be |
| 20 // sweeped from history for a closed tab entry and an open tab entry | 21 // sweeped from history for a closed tab entry and an open tab entry |
| 21 // respectively. May be overridden by the field trial. | 22 // respectively. May be overridden by the field trial. |
| 22 const unsigned int kDefaultClosedTabExpirationDurationSeconds = | 23 const unsigned int kDefaultClosedTabExpirationDurationSeconds = |
| 23 30; // 30 seconds. | 24 30; // 30 seconds. |
| 24 const unsigned int kDefaultOpenTabExpirationDurationSeconds = | 25 const unsigned int kDefaultOpenTabExpirationDurationSeconds = |
| 25 60 * 60 * 24 * 5; // 5 days. | 26 60 * 60 * 24 * 5; // 5 days. |
| 26 | 27 |
| 28 const char kUMATrackingSessionLifetimeSecondsHistogram[] = |
| 29 "DataUse.TabModel.TrackingSessionLifetime"; |
| 30 const char kUMAOldInactiveSessionRemovalDurationSecondsHistogram[] = |
| 31 "DataUse.TabModel.OldInactiveSessionRemovalDuration"; |
| 32 |
| 27 // Returns various parameters from the values specified in the field trial. | 33 // Returns various parameters from the values specified in the field trial. |
| 28 size_t GetMaxSessionsPerTab() { | 34 size_t GetMaxSessionsPerTab() { |
| 29 size_t max_sessions_per_tab = -1; | 35 size_t max_sessions_per_tab = -1; |
| 30 std::string variation_value = variations::GetVariationParamValue( | 36 std::string variation_value = variations::GetVariationParamValue( |
| 31 chrome::android::ExternalDataUseObserver:: | 37 chrome::android::ExternalDataUseObserver:: |
| 32 kExternalDataUseObserverFieldTrial, | 38 kExternalDataUseObserverFieldTrial, |
| 33 "max_sessions_per_tab"); | 39 "max_sessions_per_tab"); |
| 34 if (!variation_value.empty() && | 40 if (!variation_value.empty() && |
| 35 base::StringToSizeT(variation_value, &max_sessions_per_tab)) { | 41 base::StringToSizeT(variation_value, &max_sessions_per_tab)) { |
| 36 return max_sessions_per_tab; | 42 return max_sessions_per_tab; |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 bool TabDataUseEntry::EndTracking() { | 109 bool TabDataUseEntry::EndTracking() { |
| 104 DCHECK(tab_close_time_.is_null()); | 110 DCHECK(tab_close_time_.is_null()); |
| 105 if (!IsTrackingDataUse()) | 111 if (!IsTrackingDataUse()) |
| 106 return false; // Duplicate end events. | 112 return false; // Duplicate end events. |
| 107 | 113 |
| 108 TabSessions::reverse_iterator back_iterator = sessions_.rbegin(); | 114 TabSessions::reverse_iterator back_iterator = sessions_.rbegin(); |
| 109 if (back_iterator == sessions_.rend()) | 115 if (back_iterator == sessions_.rend()) |
| 110 return false; | 116 return false; |
| 111 | 117 |
| 112 back_iterator->end_time = Now(); | 118 back_iterator->end_time = Now(); |
| 119 |
| 120 UMA_HISTOGRAM_CUSTOM_TIMES( |
| 121 kUMATrackingSessionLifetimeSecondsHistogram, |
| 122 back_iterator->end_time - back_iterator->start_time, |
| 123 base::TimeDelta::FromSeconds(1), base::TimeDelta::FromHours(1), 50); |
| 124 |
| 113 return true; | 125 return true; |
| 114 } | 126 } |
| 115 | 127 |
| 116 bool TabDataUseEntry::EndTrackingWithLabel(const std::string& label) { | 128 bool TabDataUseEntry::EndTrackingWithLabel(const std::string& label) { |
| 117 if (!sessions_.empty() && sessions_.back().label == label) | 129 if (!sessions_.empty() && sessions_.back().label == label) |
| 118 return EndTracking(); | 130 return EndTracking(); |
| 119 return false; | 131 return false; |
| 120 } | 132 } |
| 121 | 133 |
| 122 void TabDataUseEntry::OnTabCloseEvent() { | 134 void TabDataUseEntry::OnTabCloseEvent() { |
| 123 DCHECK(!IsTrackingDataUse()); | 135 DCHECK(!IsTrackingDataUse()); |
| 124 tab_close_time_ = Now(); | 136 tab_close_time_ = Now(); |
| 125 } | 137 } |
| 126 | 138 |
| 127 bool TabDataUseEntry::IsExpired() const { | 139 bool TabDataUseEntry::IsExpired() const { |
| 128 const base::TimeTicks now = Now(); | 140 const base::TimeTicks now = Now(); |
| 129 | 141 |
| 130 if (!tab_close_time_.is_null()) { | 142 if (!tab_close_time_.is_null()) { |
| 131 // Closed tab entry. | 143 // Closed tab entry. |
| 132 return ((now - tab_close_time_) > closed_tab_expiration_duration_); | 144 return ((now - tab_close_time_) > closed_tab_expiration_duration_); |
| 133 } | 145 } |
| 134 | 146 |
| 135 const base::TimeTicks latest_session_time = GetLatestStartOrEndTime(); | 147 const base::TimeTicks latest_session_time = GetLatestStartOrEndTime(); |
| 136 if (latest_session_time.is_null() || | 148 if (latest_session_time.is_null() || |
| 137 ((now - latest_session_time) > open_tab_expiration_duration_)) { | 149 ((now - latest_session_time) > open_tab_expiration_duration_)) { |
| 138 // TODO(rajendrant): Add UMA to track deletion of entries corresponding to | |
| 139 // existing tabs. | |
| 140 return true; | 150 return true; |
| 141 } | 151 } |
| 142 return false; | 152 return false; |
| 143 } | 153 } |
| 144 | 154 |
| 145 bool TabDataUseEntry::GetLabel(const base::TimeTicks& data_use_time, | 155 bool TabDataUseEntry::GetLabel(const base::TimeTicks& data_use_time, |
| 146 std::string* output_label) const { | 156 std::string* output_label) const { |
| 147 *output_label = ""; | 157 *output_label = ""; |
| 148 | 158 |
| 149 // Find a tracking session in history that was active at |data_use_time|. | 159 // Find a tracking session in history that was active at |data_use_time|. |
| (...skipping 28 matching lines...) Expand all Loading... |
| 178 if (back_iterator == sessions_.rend()) | 188 if (back_iterator == sessions_.rend()) |
| 179 return base::TimeTicks(); // No tab session found. | 189 return base::TimeTicks(); // No tab session found. |
| 180 if (!back_iterator->end_time.is_null()) | 190 if (!back_iterator->end_time.is_null()) |
| 181 return back_iterator->end_time; | 191 return back_iterator->end_time; |
| 182 | 192 |
| 183 DCHECK(!back_iterator->start_time.is_null()); | 193 DCHECK(!back_iterator->start_time.is_null()); |
| 184 return back_iterator->start_time; | 194 return back_iterator->start_time; |
| 185 } | 195 } |
| 186 | 196 |
| 187 void TabDataUseEntry::CompactSessionHistory() { | 197 void TabDataUseEntry::CompactSessionHistory() { |
| 188 // TODO(rajendrant): Add UMA to track how often old sessions are lost. | 198 while (sessions_.size() > max_sessions_per_tab_) { |
| 189 while (sessions_.size() > max_sessions_per_tab_) | 199 const auto& front = sessions_.front(); |
| 200 DCHECK(!front.end_time.is_null()); |
| 201 // Track how often old sessions are lost. |
| 202 UMA_HISTOGRAM_CUSTOM_TIMES( |
| 203 kUMAOldInactiveSessionRemovalDurationSecondsHistogram, |
| 204 Now() - front.end_time, base::TimeDelta::FromSeconds(1), |
| 205 base::TimeDelta::FromHours(1), 50); |
| 190 sessions_.pop_front(); | 206 sessions_.pop_front(); |
| 207 } |
| 191 } | 208 } |
| 192 | 209 |
| 193 } // namespace android | 210 } // namespace android |
| 194 | 211 |
| 195 } // namespace chrome | 212 } // namespace chrome |
| OLD | NEW |