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 "base/threading/thread_checker.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 |
17 // Ensures that all access is done on the same thread. | |
18 base::ThreadChecker thread_checker; | |
chrisha
2015/09/28 15:07:49
The thread check remembers the thread on which it
Georges Khalil
2015/09/28 18:41:13
I was under the (wrong) impression that it remembe
| |
19 | |
15 } // namespace | 20 } // namespace |
16 | 21 |
17 // static | 22 // static |
18 TabDiscardState* TabDiscardState::Get(WebContents* web_contents) { | 23 TabDiscardState* TabDiscardState::Get(WebContents* web_contents) { |
24 DCHECK(thread_checker.CalledOnValidThread()); | |
19 TabDiscardState* discard_state = static_cast<TabDiscardState*>( | 25 TabDiscardState* discard_state = static_cast<TabDiscardState*>( |
20 web_contents->GetUserData(&kDiscardStateKey)); | 26 web_contents->GetUserData(&kDiscardStateKey)); |
21 | 27 |
22 // If this function is called, we probably need to query/change the discard | 28 // If this function is called, we probably need to query/change the discard |
23 // state. Let's go ahead a add one. | 29 // state. Let's go ahead a add one. |
24 if (!discard_state) { | 30 if (!discard_state) { |
25 discard_state = new TabDiscardState; | 31 discard_state = new TabDiscardState; |
26 web_contents->SetUserData(&kDiscardStateKey, discard_state); | 32 web_contents->SetUserData(&kDiscardStateKey, discard_state); |
27 } | 33 } |
28 | 34 |
29 return discard_state; | 35 return discard_state; |
30 } | 36 } |
31 | 37 |
32 // static | 38 // static |
33 void TabDiscardState::Set(WebContents* web_contents, TabDiscardState* state) { | 39 void TabDiscardState::Set(WebContents* web_contents, TabDiscardState* state) { |
34 web_contents->SetUserData(&kDiscardStateKey, state); | 40 web_contents->SetUserData(&kDiscardStateKey, state); |
35 } | 41 } |
36 | 42 |
37 // static | 43 // static |
38 bool TabDiscardState::IsDiscarded(WebContents* web_contents) { | 44 bool TabDiscardState::IsDiscarded(WebContents* web_contents) { |
39 TabDiscardState* discard_state = TabDiscardState::Get(web_contents); | 45 TabDiscardState* discard_state = TabDiscardState::Get(web_contents); |
40 return discard_state->is_discarded_; | 46 return discard_state->is_discarded_; |
41 } | 47 } |
42 | 48 |
43 // static | 49 // static |
44 void TabDiscardState::SetDiscardState(WebContents* web_contents, bool state) { | 50 void TabDiscardState::SetDiscardState(WebContents* web_contents, bool state) { |
45 TabDiscardState* discard_state = TabDiscardState::Get(web_contents); | 51 TabDiscardState* discard_state = TabDiscardState::Get(web_contents); |
52 if (discard_state->is_discarded_ && !state) { | |
53 static int reload_count = 0; | |
54 UMA_HISTOGRAM_CUSTOM_COUNTS("Tabs.Discard.ReloadCount", ++reload_count, 1, | |
55 1000, 50); | |
56 } | |
57 | |
46 discard_state->is_discarded_ = state; | 58 discard_state->is_discarded_ = state; |
47 } | 59 } |
48 | 60 |
49 // static | 61 // static |
50 int TabDiscardState::DiscardCount(WebContents* web_contents) { | 62 int TabDiscardState::DiscardCount(WebContents* web_contents) { |
51 TabDiscardState* discard_state = TabDiscardState::Get(web_contents); | 63 TabDiscardState* discard_state = TabDiscardState::Get(web_contents); |
52 return discard_state->discard_count_; | 64 return discard_state->discard_count_; |
53 } | 65 } |
54 | 66 |
55 // static | 67 // static |
56 void TabDiscardState::IncrementDiscardCount(WebContents* web_contents) { | 68 void TabDiscardState::IncrementDiscardCount(WebContents* web_contents) { |
57 TabDiscardState* discard_state = TabDiscardState::Get(web_contents); | 69 TabDiscardState* discard_state = TabDiscardState::Get(web_contents); |
58 discard_state->discard_count_++; | 70 discard_state->discard_count_++; |
59 } | 71 } |
60 | 72 |
61 // static | 73 // static |
62 void TabDiscardState::CopyState(content::WebContents* old_contents, | 74 void TabDiscardState::CopyState(content::WebContents* old_contents, |
63 content::WebContents* new_contents) { | 75 content::WebContents* new_contents) { |
64 TabDiscardState* old_state = Get(old_contents); | 76 TabDiscardState* old_state = Get(old_contents); |
65 TabDiscardState* new_State = Get(new_contents); | 77 TabDiscardState* new_State = Get(new_contents); |
66 *new_State = *old_state; | 78 *new_State = *old_state; |
67 } | 79 } |
OLD | NEW |