OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/bind.h" |
| 6 #include "base/prefs/pref_service.h" |
| 7 #include "chrome/app/chrome_command_ids.h" |
| 8 #include "chrome/browser/browser_process.h" |
| 9 #include "chrome/browser/ui/browser_commands.h" |
| 10 #include "chrome/browser/ui/browser_list.h" |
| 11 #include "chrome/common/pref_names.h" |
| 12 #include "chrome/test/base/in_process_browser_test.h" |
| 13 #include "content/public/browser/background_tracing_manager.h" |
| 14 #include "content/public/browser/background_tracing_preemptive_config.h" |
| 15 #include "content/public/browser/background_tracing_reactive_config.h" |
| 16 #include "content/public/browser/browser_thread.h" |
| 17 #include "content/public/test/test_utils.h" |
| 18 |
| 19 namespace { |
| 20 |
| 21 class ChromeTracingDelegateBrowserTest : public InProcessBrowserTest { |
| 22 public: |
| 23 ChromeTracingDelegateBrowserTest() |
| 24 : receive_count_(0), |
| 25 started_finalizations_count_(0), |
| 26 last_on_started_finalizing_success_(false) {} |
| 27 |
| 28 bool StartPreemptiveScenario(const base::Closure& on_upload_callback, |
| 29 bool require_anonymized_data) { |
| 30 on_upload_callback_ = on_upload_callback; |
| 31 |
| 32 scoped_ptr<content::BackgroundTracingPreemptiveConfig> config( |
| 33 new content::BackgroundTracingPreemptiveConfig()); |
| 34 |
| 35 content::BackgroundTracingPreemptiveConfig::MonitoringRule rule; |
| 36 rule.type = content::BackgroundTracingPreemptiveConfig:: |
| 37 MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED; |
| 38 rule.named_trigger_info.trigger_name = "test"; |
| 39 |
| 40 config->configs.push_back(rule); |
| 41 |
| 42 content::BackgroundTracingManager::ReceiveCallback receive_callback = |
| 43 base::Bind(&ChromeTracingDelegateBrowserTest::OnUpload, |
| 44 base::Unretained(this)); |
| 45 |
| 46 return content::BackgroundTracingManager::GetInstance()->SetActiveScenario( |
| 47 config.Pass(), receive_callback, require_anonymized_data); |
| 48 } |
| 49 |
| 50 void TriggerReactiveScenario( |
| 51 const base::Closure& on_started_finalization_callback) { |
| 52 on_started_finalization_callback_ = on_started_finalization_callback; |
| 53 trigger_handle_ = |
| 54 content::BackgroundTracingManager::GetInstance()->RegisterTriggerType( |
| 55 "test"); |
| 56 |
| 57 content::BackgroundTracingManager::StartedFinalizingCallback |
| 58 started_finalizing_callback = |
| 59 base::Bind(&ChromeTracingDelegateBrowserTest::OnStartedFinalizing, |
| 60 base::Unretained(this)); |
| 61 content::BackgroundTracingManager::GetInstance()->TriggerNamedEvent( |
| 62 trigger_handle_, started_finalizing_callback); |
| 63 } |
| 64 |
| 65 int get_receive_count() const { return receive_count_; } |
| 66 bool get_started_finalizations() const { |
| 67 return started_finalizations_count_; |
| 68 } |
| 69 bool get_last_started_finalization_success() const { |
| 70 return last_on_started_finalizing_success_; |
| 71 } |
| 72 |
| 73 private: |
| 74 void OnUpload(const base::RefCountedString* file_contents, |
| 75 base::Callback<void()> done_callback) { |
| 76 receive_count_ += 1; |
| 77 |
| 78 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 79 base::Bind(done_callback)); |
| 80 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 81 base::Bind(on_upload_callback_)); |
| 82 } |
| 83 |
| 84 void OnStartedFinalizing(bool success) { |
| 85 started_finalizations_count_++; |
| 86 last_on_started_finalizing_success_ = success; |
| 87 |
| 88 if (!on_started_finalization_callback_.is_null()) { |
| 89 content::BrowserThread::PostTask( |
| 90 content::BrowserThread::UI, FROM_HERE, |
| 91 base::Bind(on_started_finalization_callback_)); |
| 92 } |
| 93 } |
| 94 |
| 95 base::Closure on_upload_callback_; |
| 96 base::Closure on_started_finalization_callback_; |
| 97 int receive_count_; |
| 98 int started_finalizations_count_; |
| 99 content::BackgroundTracingManager::TriggerHandle trigger_handle_; |
| 100 bool last_on_started_finalizing_success_; |
| 101 }; |
| 102 |
| 103 IN_PROC_BROWSER_TEST_F(ChromeTracingDelegateBrowserTest, |
| 104 BackgroundTracingTimeThrottled) { |
| 105 base::RunLoop wait_for_upload; |
| 106 |
| 107 EXPECT_TRUE(StartPreemptiveScenario(wait_for_upload.QuitClosure(), false)); |
| 108 |
| 109 TriggerReactiveScenario(base::Closure()); |
| 110 |
| 111 wait_for_upload.Run(); |
| 112 |
| 113 EXPECT_TRUE(get_receive_count() == 1); |
| 114 |
| 115 PrefService* local_state = g_browser_process->local_state(); |
| 116 DCHECK(local_state); |
| 117 const base::Time last_upload_time = base::Time::FromInternalValue( |
| 118 local_state->GetInt64(prefs::kBackgroundTracingLastUpload)); |
| 119 EXPECT_FALSE(last_upload_time.is_null()); |
| 120 |
| 121 // We should not be able to start a new reactive scenario immediately after |
| 122 // a previous one gets uploaded. |
| 123 EXPECT_FALSE(StartPreemptiveScenario(base::Closure(), false)); |
| 124 } |
| 125 |
| 126 IN_PROC_BROWSER_TEST_F(ChromeTracingDelegateBrowserTest, |
| 127 BackgroundTracingThrottleTimeElapsed) { |
| 128 base::RunLoop wait_for_upload; |
| 129 |
| 130 EXPECT_TRUE(StartPreemptiveScenario(wait_for_upload.QuitClosure(), false)); |
| 131 |
| 132 TriggerReactiveScenario(base::Closure()); |
| 133 |
| 134 wait_for_upload.Run(); |
| 135 |
| 136 EXPECT_TRUE(get_receive_count() == 1); |
| 137 |
| 138 PrefService* local_state = g_browser_process->local_state(); |
| 139 DCHECK(local_state); |
| 140 const base::Time last_upload_time = base::Time::FromInternalValue( |
| 141 local_state->GetInt64(prefs::kBackgroundTracingLastUpload)); |
| 142 EXPECT_FALSE(last_upload_time.is_null()); |
| 143 |
| 144 // We move the last upload time to eight days in the past, |
| 145 // and at that point should be able to start a scenario again. |
| 146 base::Time new_upload_time = last_upload_time - base::TimeDelta::FromDays(8); |
| 147 local_state->SetInt64(prefs::kBackgroundTracingLastUpload, |
| 148 new_upload_time.ToInternalValue()); |
| 149 EXPECT_TRUE(StartPreemptiveScenario(base::Closure(), false)); |
| 150 } |
| 151 |
| 152 // If we need a PII-stripped trace, any existing OTR session should block the |
| 153 // trace. |
| 154 IN_PROC_BROWSER_TEST_F(ChromeTracingDelegateBrowserTest, |
| 155 ExistingIncognitoSessionBlockingTraceStart) { |
| 156 EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_NEW_INCOGNITO_WINDOW)); |
| 157 EXPECT_TRUE(BrowserList::IsOffTheRecordSessionActive()); |
| 158 const bool require_anonymized_data = true; |
| 159 EXPECT_FALSE( |
| 160 StartPreemptiveScenario(base::Closure(), require_anonymized_data)); |
| 161 } |
| 162 |
| 163 // If we need a PII-stripped trace, any new OTR session during tracing should |
| 164 // block the finalization of the trace. |
| 165 IN_PROC_BROWSER_TEST_F(ChromeTracingDelegateBrowserTest, |
| 166 NewIncognitoSessionBlockingTraceFinalization) { |
| 167 const bool require_anonymized_data = true; |
| 168 EXPECT_TRUE( |
| 169 StartPreemptiveScenario(base::Closure(), require_anonymized_data)); |
| 170 |
| 171 EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_NEW_INCOGNITO_WINDOW)); |
| 172 EXPECT_TRUE(BrowserList::IsOffTheRecordSessionActive()); |
| 173 |
| 174 base::RunLoop wait_for_finalization_start; |
| 175 TriggerReactiveScenario(wait_for_finalization_start.QuitClosure()); |
| 176 wait_for_finalization_start.Run(); |
| 177 |
| 178 EXPECT_TRUE(get_started_finalizations() == 1); |
| 179 EXPECT_FALSE(get_last_started_finalization_success()); |
| 180 } |
| 181 |
| 182 } // namespace |
OLD | NEW |