| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/extensions/api/log_private/log_private_api.h" | 5 #include "chrome/browser/extensions/api/log_private/log_private_api.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 const int kNetLogEventDelayMilliseconds = 100; | 54 const int kNetLogEventDelayMilliseconds = 100; |
| 55 | 55 |
| 56 // Gets sequenced task runner for file specific calls within this API. | 56 // Gets sequenced task runner for file specific calls within this API. |
| 57 scoped_refptr<base::SequencedTaskRunner> GetSequencedTaskRunner() { | 57 scoped_refptr<base::SequencedTaskRunner> GetSequencedTaskRunner() { |
| 58 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); | 58 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); |
| 59 return pool->GetSequencedTaskRunnerWithShutdownBehavior( | 59 return pool->GetSequencedTaskRunnerWithShutdownBehavior( |
| 60 pool->GetNamedSequenceToken(FileResource::kSequenceToken), | 60 pool->GetNamedSequenceToken(FileResource::kSequenceToken), |
| 61 base::SequencedWorkerPool::BLOCK_SHUTDOWN); | 61 base::SequencedWorkerPool::BLOCK_SHUTDOWN); |
| 62 } | 62 } |
| 63 | 63 |
| 64 #if DCHECK_IS_ON() |
| 65 base::LazyInstance<base::SequenceChecker>::Leaky |
| 66 g_file_resource_sequence_checker = LAZY_INSTANCE_INITIALIZER; |
| 67 #endif |
| 68 |
| 64 // Checks if we are running on sequenced task runner thread. | 69 // Checks if we are running on sequenced task runner thread. |
| 65 bool IsRunningOnSequenceThread() { | 70 void AssertCurrentlyOnFileResourceSequence() { |
| 66 base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool(); | 71 #if DCHECK_IS_ON() |
| 67 return pool->IsRunningSequenceOnCurrentThread( | 72 DCHECK(g_file_resource_sequence_checker.Get().CalledOnValidSequence()); |
| 68 pool->GetNamedSequenceToken(FileResource::kSequenceToken)); | 73 #endif |
| 69 } | 74 } |
| 70 | 75 |
| 71 std::unique_ptr<LogParser> CreateLogParser(const std::string& log_type) { | 76 std::unique_ptr<LogParser> CreateLogParser(const std::string& log_type) { |
| 72 if (log_type == "syslog") | 77 if (log_type == "syslog") |
| 73 return std::unique_ptr<LogParser>(new SyslogParser()); | 78 return std::unique_ptr<LogParser>(new SyslogParser()); |
| 74 // TODO(shinfan): Add more parser here | 79 // TODO(shinfan): Add more parser here |
| 75 | 80 |
| 76 NOTREACHED() << "Invalid log type: " << log_type; | 81 NOTREACHED() << "Invalid log type: " << log_type; |
| 77 return std::unique_ptr<LogParser>(); | 82 return std::unique_ptr<LogParser>(); |
| 78 } | 83 } |
| (...skipping 28 matching lines...) Expand all Loading... |
| 107 } | 112 } |
| 108 | 113 |
| 109 // Removes direcotry content of |logs_dumps| and |app_logs_dir| (only for the | 114 // Removes direcotry content of |logs_dumps| and |app_logs_dir| (only for the |
| 110 // primary profile). | 115 // primary profile). |
| 111 void CleanUpLeftoverLogs(bool is_primary_profile, | 116 void CleanUpLeftoverLogs(bool is_primary_profile, |
| 112 const base::FilePath& app_logs_dir, | 117 const base::FilePath& app_logs_dir, |
| 113 const base::FilePath& logs_dumps) { | 118 const base::FilePath& logs_dumps) { |
| 114 LOG(WARNING) << "Deleting " << app_logs_dir.value(); | 119 LOG(WARNING) << "Deleting " << app_logs_dir.value(); |
| 115 LOG(WARNING) << "Deleting " << logs_dumps.value(); | 120 LOG(WARNING) << "Deleting " << logs_dumps.value(); |
| 116 | 121 |
| 117 DCHECK(IsRunningOnSequenceThread()); | 122 AssertCurrentlyOnFileResourceSequence(); |
| 118 base::DeleteFile(logs_dumps, true); | 123 base::DeleteFile(logs_dumps, true); |
| 119 | 124 |
| 120 // App-specific logs are stored in /home/chronos/user/log/apps directory that | 125 // App-specific logs are stored in /home/chronos/user/log/apps directory that |
| 121 // is shared between all profiles in multi-profile case. We should not | 126 // is shared between all profiles in multi-profile case. We should not |
| 122 // nuke it for non-primary profiles. | 127 // nuke it for non-primary profiles. |
| 123 if (!is_primary_profile) | 128 if (!is_primary_profile) |
| 124 return; | 129 return; |
| 125 | 130 |
| 126 base::DeleteFile(app_logs_dir, true); | 131 base::DeleteFile(app_logs_dir, true); |
| 127 } | 132 } |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 MaybeStopNetInternalLogging(closure); | 191 MaybeStopNetInternalLogging(closure); |
| 187 } | 192 } |
| 188 | 193 |
| 189 void LogPrivateAPI::StopAllWatches(const std::string& extension_id, | 194 void LogPrivateAPI::StopAllWatches(const std::string& extension_id, |
| 190 const base::Closure& closure) { | 195 const base::Closure& closure) { |
| 191 StopNetInternalsWatch(extension_id, closure); | 196 StopNetInternalsWatch(extension_id, closure); |
| 192 } | 197 } |
| 193 | 198 |
| 194 void LogPrivateAPI::RegisterTempFile(const std::string& owner_extension_id, | 199 void LogPrivateAPI::RegisterTempFile(const std::string& owner_extension_id, |
| 195 const base::FilePath& file_path) { | 200 const base::FilePath& file_path) { |
| 196 if (!IsRunningOnSequenceThread()) { | 201 GetSequencedTaskRunner()->PostTask( |
| 197 GetSequencedTaskRunner()->PostTask( | 202 FROM_HERE, |
| 198 FROM_HERE, | 203 base::Bind(&LogPrivateAPI::RegisterTempFileOnFileResourceSequence, |
| 199 base::Bind(&LogPrivateAPI::RegisterTempFile, | 204 base::Unretained(this), owner_extension_id, file_path)); |
| 200 base::Unretained(this), | |
| 201 owner_extension_id, | |
| 202 file_path)); | |
| 203 return; | |
| 204 } | |
| 205 | |
| 206 log_file_resources_.Add(new FileResource(owner_extension_id, file_path)); | |
| 207 } | 205 } |
| 208 | 206 |
| 209 static base::LazyInstance<BrowserContextKeyedAPIFactory<LogPrivateAPI> > | 207 static base::LazyInstance<BrowserContextKeyedAPIFactory<LogPrivateAPI> > |
| 210 g_factory = LAZY_INSTANCE_INITIALIZER; | 208 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 211 | 209 |
| 212 // static | 210 // static |
| 213 BrowserContextKeyedAPIFactory<LogPrivateAPI>* | 211 BrowserContextKeyedAPIFactory<LogPrivateAPI>* |
| 214 LogPrivateAPI::GetFactoryInstance() { | 212 LogPrivateAPI::GetFactoryInstance() { |
| 215 return g_factory.Pointer(); | 213 return g_factory.Pointer(); |
| 216 } | 214 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 std::unique_ptr<Event> event( | 250 std::unique_ptr<Event> event( |
| 253 new Event(::extensions::events::LOG_PRIVATE_ON_CAPTURED_EVENTS, | 251 new Event(::extensions::events::LOG_PRIVATE_ON_CAPTURED_EVENTS, |
| 254 ::events::kOnCapturedEvents, std::move(event_args))); | 252 ::events::kOnCapturedEvents, std::move(event_args))); |
| 255 EventRouter::Get(browser_context_) | 253 EventRouter::Get(browser_context_) |
| 256 ->DispatchEventToExtension(*ix, std::move(event)); | 254 ->DispatchEventToExtension(*ix, std::move(event)); |
| 257 } | 255 } |
| 258 } | 256 } |
| 259 | 257 |
| 260 void LogPrivateAPI::CreateTempNetLogFile(const std::string& owner_extension_id, | 258 void LogPrivateAPI::CreateTempNetLogFile(const std::string& owner_extension_id, |
| 261 base::ScopedFILE* file) { | 259 base::ScopedFILE* file) { |
| 262 DCHECK(IsRunningOnSequenceThread()); | 260 AssertCurrentlyOnFileResourceSequence(); |
| 263 | 261 |
| 264 // Create app-specific subdirectory in session logs folder. | 262 // Create app-specific subdirectory in session logs folder. |
| 265 base::FilePath app_log_dir = GetAppLogDirectory().Append(owner_extension_id); | 263 base::FilePath app_log_dir = GetAppLogDirectory().Append(owner_extension_id); |
| 266 if (!base::DirectoryExists(app_log_dir)) { | 264 if (!base::DirectoryExists(app_log_dir)) { |
| 267 if (!base::CreateDirectory(app_log_dir)) { | 265 if (!base::CreateDirectory(app_log_dir)) { |
| 268 LOG(ERROR) << "Could not create dir " << app_log_dir.value(); | 266 LOG(ERROR) << "Could not create dir " << app_log_dir.value(); |
| 269 return; | 267 return; |
| 270 } | 268 } |
| 271 } | 269 } |
| 272 | 270 |
| 273 base::FilePath file_path = app_log_dir.Append(kLogFileNameBase); | 271 base::FilePath file_path = app_log_dir.Append(kLogFileNameBase); |
| 274 file_path = logging::GenerateTimestampedName(file_path, base::Time::Now()); | 272 file_path = logging::GenerateTimestampedName(file_path, base::Time::Now()); |
| 275 FILE* file_ptr = fopen(file_path.value().c_str(), "w"); | 273 FILE* file_ptr = fopen(file_path.value().c_str(), "w"); |
| 276 if (file_ptr == nullptr) { | 274 if (file_ptr == nullptr) { |
| 277 LOG(ERROR) << "Could not open " << file_path.value(); | 275 LOG(ERROR) << "Could not open " << file_path.value(); |
| 278 return; | 276 return; |
| 279 } | 277 } |
| 280 | 278 |
| 281 RegisterTempFile(owner_extension_id, file_path); | 279 RegisterTempFileOnFileResourceSequence(owner_extension_id, file_path); |
| 282 return file->reset(file_ptr); | 280 return file->reset(file_ptr); |
| 283 } | 281 } |
| 284 | 282 |
| 285 void LogPrivateAPI::StartObservingNetEvents( | 283 void LogPrivateAPI::StartObservingNetEvents( |
| 286 IOThread* io_thread, | 284 IOThread* io_thread, |
| 287 base::ScopedFILE* file) { | 285 base::ScopedFILE* file) { |
| 288 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 286 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 289 if (!file->get()) | 287 if (!file->get()) |
| 290 return; | 288 return; |
| 291 | 289 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); | 379 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); |
| 382 GetSequencedTaskRunner()->PostTask( | 380 GetSequencedTaskRunner()->PostTask( |
| 383 FROM_HERE, | 381 FROM_HERE, |
| 384 base::Bind(&CleanUpLeftoverLogs, | 382 base::Bind(&CleanUpLeftoverLogs, |
| 385 Profile::FromBrowserContext(browser_context_) == | 383 Profile::FromBrowserContext(browser_context_) == |
| 386 ProfileManager::GetPrimaryUserProfile(), | 384 ProfileManager::GetPrimaryUserProfile(), |
| 387 GetAppLogDirectory(), | 385 GetAppLogDirectory(), |
| 388 GetLogDumpDirectory(browser_context_))); | 386 GetLogDumpDirectory(browser_context_))); |
| 389 } | 387 } |
| 390 | 388 |
| 389 void LogPrivateAPI::RegisterTempFileOnFileResourceSequence( |
| 390 const std::string& owner_extension_id, |
| 391 const base::FilePath& file_path) { |
| 392 AssertCurrentlyOnFileResourceSequence(); |
| 393 log_file_resources_.Add(new FileResource(owner_extension_id, file_path)); |
| 394 } |
| 395 |
| 391 void LogPrivateAPI::OnExtensionUnloaded( | 396 void LogPrivateAPI::OnExtensionUnloaded( |
| 392 content::BrowserContext* browser_context, | 397 content::BrowserContext* browser_context, |
| 393 const Extension* extension, | 398 const Extension* extension, |
| 394 UnloadedExtensionInfo::Reason reason) { | 399 UnloadedExtensionInfo::Reason reason) { |
| 395 StopNetInternalsWatch(extension->id(), base::Closure()); | 400 StopNetInternalsWatch(extension->id(), base::Closure()); |
| 396 } | 401 } |
| 397 | 402 |
| 398 LogPrivateGetHistoricalFunction::LogPrivateGetHistoricalFunction() { | 403 LogPrivateGetHistoricalFunction::LogPrivateGetHistoricalFunction() { |
| 399 } | 404 } |
| 400 | 405 |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 538 entry->SetBoolean("isDirectory", false); | 543 entry->SetBoolean("isDirectory", false); |
| 539 base::ListValue* entry_list = new base::ListValue(); | 544 base::ListValue* entry_list = new base::ListValue(); |
| 540 entry_list->Append(entry); | 545 entry_list->Append(entry); |
| 541 response->Set("entries", entry_list); | 546 response->Set("entries", entry_list); |
| 542 response->SetBoolean("multiple", false); | 547 response->SetBoolean("multiple", false); |
| 543 SetResult(std::move(response)); | 548 SetResult(std::move(response)); |
| 544 SendResponse(succeeded); | 549 SendResponse(succeeded); |
| 545 } | 550 } |
| 546 | 551 |
| 547 } // namespace extensions | 552 } // namespace extensions |
| OLD | NEW |