| 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/ui/tabs/tab_discard_state.h" | 5 #include "chrome/browser/ui/tabs/tab_discard_state.h" |
| 6 | 6 |
| 7 #include "base/metrics/histogram.h" |
| 8 #include "content/public/browser/browser_thread.h" |
| 7 #include "content/public/browser/web_contents.h" | 9 #include "content/public/browser/web_contents.h" |
| 8 | 10 |
| 9 using content::WebContents; | 11 using content::WebContents; |
| 10 | 12 |
| 11 namespace { | 13 namespace { |
| 12 | 14 |
| 13 const char kDiscardStateKey[] = "TabDiscardState"; | 15 const char kDiscardStateKey[] = "TabDiscardState"; |
| 14 | 16 |
| 15 } // namespace | 17 } // namespace |
| 16 | 18 |
| 17 // static | 19 // static |
| 18 TabDiscardState* TabDiscardState::Get(WebContents* web_contents) { | 20 TabDiscardState* TabDiscardState::Get(WebContents* web_contents) { |
| 21 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 19 TabDiscardState* discard_state = static_cast<TabDiscardState*>( | 22 TabDiscardState* discard_state = static_cast<TabDiscardState*>( |
| 20 web_contents->GetUserData(&kDiscardStateKey)); | 23 web_contents->GetUserData(&kDiscardStateKey)); |
| 21 | 24 |
| 22 // If this function is called, we probably need to query/change the discard | 25 // If this function is called, we probably need to query/change the discard |
| 23 // state. Let's go ahead a add one. | 26 // state. Let's go ahead a add one. |
| 24 if (!discard_state) { | 27 if (!discard_state) { |
| 25 discard_state = new TabDiscardState; | 28 discard_state = new TabDiscardState; |
| 26 web_contents->SetUserData(&kDiscardStateKey, discard_state); | 29 web_contents->SetUserData(&kDiscardStateKey, discard_state); |
| 27 } | 30 } |
| 28 | 31 |
| 29 return discard_state; | 32 return discard_state; |
| 30 } | 33 } |
| 31 | 34 |
| 32 // static | 35 // static |
| 33 void TabDiscardState::Set(WebContents* web_contents, TabDiscardState* state) { | 36 void TabDiscardState::Set(WebContents* web_contents, TabDiscardState* state) { |
| 37 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 34 web_contents->SetUserData(&kDiscardStateKey, state); | 38 web_contents->SetUserData(&kDiscardStateKey, state); |
| 35 } | 39 } |
| 36 | 40 |
| 37 // static | 41 // static |
| 38 bool TabDiscardState::IsDiscarded(WebContents* web_contents) { | 42 bool TabDiscardState::IsDiscarded(WebContents* web_contents) { |
| 39 TabDiscardState* discard_state = TabDiscardState::Get(web_contents); | 43 TabDiscardState* discard_state = TabDiscardState::Get(web_contents); |
| 40 return discard_state->is_discarded_; | 44 return discard_state->is_discarded_; |
| 41 } | 45 } |
| 42 | 46 |
| 43 // static | 47 // static |
| 44 void TabDiscardState::SetDiscardState(WebContents* web_contents, bool state) { | 48 void TabDiscardState::SetDiscardState(WebContents* web_contents, bool state) { |
| 45 TabDiscardState* discard_state = TabDiscardState::Get(web_contents); | 49 TabDiscardState* discard_state = TabDiscardState::Get(web_contents); |
| 50 if (discard_state->is_discarded_ && !state) { |
| 51 static int reload_count = 0; |
| 52 UMA_HISTOGRAM_CUSTOM_COUNTS("Tabs.Discard.ReloadCount", ++reload_count, 1, |
| 53 1000, 50); |
| 54 } |
| 55 |
| 46 discard_state->is_discarded_ = state; | 56 discard_state->is_discarded_ = state; |
| 47 } | 57 } |
| 48 | 58 |
| 49 // static | 59 // static |
| 50 int TabDiscardState::DiscardCount(WebContents* web_contents) { | 60 int TabDiscardState::DiscardCount(WebContents* web_contents) { |
| 51 TabDiscardState* discard_state = TabDiscardState::Get(web_contents); | 61 TabDiscardState* discard_state = TabDiscardState::Get(web_contents); |
| 52 return discard_state->discard_count_; | 62 return discard_state->discard_count_; |
| 53 } | 63 } |
| 54 | 64 |
| 55 // static | 65 // static |
| 56 void TabDiscardState::IncrementDiscardCount(WebContents* web_contents) { | 66 void TabDiscardState::IncrementDiscardCount(WebContents* web_contents) { |
| 57 TabDiscardState* discard_state = TabDiscardState::Get(web_contents); | 67 TabDiscardState* discard_state = TabDiscardState::Get(web_contents); |
| 58 discard_state->discard_count_++; | 68 discard_state->discard_count_++; |
| 59 } | 69 } |
| 60 | 70 |
| 61 // static | 71 // static |
| 62 void TabDiscardState::CopyState(content::WebContents* old_contents, | 72 void TabDiscardState::CopyState(content::WebContents* old_contents, |
| 63 content::WebContents* new_contents) { | 73 content::WebContents* new_contents) { |
| 64 TabDiscardState* old_state = Get(old_contents); | 74 TabDiscardState* old_state = Get(old_contents); |
| 65 TabDiscardState* new_State = Get(new_contents); | 75 TabDiscardState* new_State = Get(new_contents); |
| 66 *new_State = *old_state; | 76 *new_State = *old_state; |
| 67 } | 77 } |
| OLD | NEW |