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