Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(377)

Side by Side Diff: chrome/browser/extensions/api/log_private/log_private_api_chromeos.cc

Issue 329853010: Additional methods for chrome.logPrivate API (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/command_line.h"
10 #include "base/json/json_writer.h" 11 #include "base/json/json_writer.h"
11 #include "base/lazy_instance.h" 12 #include "base/lazy_instance.h"
12 #include "base/logging.h" 13 #include "base/logging.h"
13 #include "base/memory/linked_ptr.h" 14 #include "base/memory/linked_ptr.h"
14 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/scoped_ptr.h"
15 #include "chrome/browser/browser_process.h" 16 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/download/download_prefs.h"
18 #include "chrome/browser/extensions/api/file_handlers/app_file_handler_util.h"
16 #include "chrome/browser/extensions/api/log_private/filter_handler.h" 19 #include "chrome/browser/extensions/api/log_private/filter_handler.h"
17 #include "chrome/browser/extensions/api/log_private/log_parser.h" 20 #include "chrome/browser/extensions/api/log_private/log_parser.h"
18 #include "chrome/browser/extensions/api/log_private/syslog_parser.h" 21 #include "chrome/browser/extensions/api/log_private/syslog_parser.h"
19 #include "chrome/browser/feedback/system_logs/scrubbed_system_logs_fetcher.h" 22 #include "chrome/browser/feedback/system_logs/scrubbed_system_logs_fetcher.h"
20 #include "chrome/browser/io_thread.h" 23 #include "chrome/browser/io_thread.h"
21 #include "chrome/browser/net/chrome_net_log.h" 24 #include "chrome/browser/net/chrome_net_log.h"
22 #include "chrome/browser/profiles/profile.h" 25 #include "chrome/browser/profiles/profile.h"
26 #include "chrome/browser/profiles/profile_manager.h"
23 #include "chrome/common/extensions/api/log_private.h" 27 #include "chrome/common/extensions/api/log_private.h"
28 #include "chrome/common/logging_chrome.h"
29 #include "content/public/browser/render_process_host.h"
24 #include "extensions/browser/event_router.h" 30 #include "extensions/browser/event_router.h"
25 #include "extensions/browser/extension_function.h" 31 #include "extensions/browser/extension_function.h"
26 #include "extensions/browser/extension_registry.h" 32 #include "extensions/browser/extension_registry.h"
33 #include "extensions/browser/granted_file_entry.h"
34
35 #if defined(OS_CHROMEOS)
36 #include "chrome/browser/chromeos/system_logs/debug_log_writer.h"
37 #endif
27 38
28 using content::BrowserThread; 39 using content::BrowserThread;
29 40
30 namespace events { 41 namespace events {
31 const char kOnAddNetInternalsEntries[] = "logPrivate.onAddNetInternalsEntries"; 42 const char kOnCapturedEvents[] = "logPrivate.onCapturedEvents";
32 } // namespace events 43 } // namespace events
33 44
34 namespace extensions { 45 namespace extensions {
35 namespace { 46 namespace {
36 47
48 const char kAppLogsSubdir[] = "apps";
49 const char kLogDumpsSubdir[] = "log_dumps";
50 const char kLogFileNameBase[] = "net-internals";
37 const int kNetLogEventDelayMilliseconds = 100; 51 const int kNetLogEventDelayMilliseconds = 100;
38 52
53 // Gets sequenced task runner for file specific calls within this API.
54 scoped_refptr<base::SequencedTaskRunner> GetSequencedTaskRunner() {
55 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool();
56 return pool->GetSequencedTaskRunnerWithShutdownBehavior(
57 pool->GetNamedSequenceToken(FileResource::kSequenceToken),
58 base::SequencedWorkerPool::BLOCK_SHUTDOWN);
59 }
60
61 // Checks if we are running on sequenced task runner thread.
62 bool IsRunningOnSequenceThread() {
63 base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool();
64 return pool->IsRunningSequenceOnCurrentThread(
65 pool->GetNamedSequenceToken(FileResource::kSequenceToken));
66 }
67
39 scoped_ptr<LogParser> CreateLogParser(const std::string& log_type) { 68 scoped_ptr<LogParser> CreateLogParser(const std::string& log_type) {
40 if (log_type == "syslog") 69 if (log_type == "syslog")
41 return scoped_ptr<LogParser>(new SyslogParser()); 70 return scoped_ptr<LogParser>(new SyslogParser());
42 // TODO(shinfan): Add more parser here 71 // TODO(shinfan): Add more parser here
43 72
44 NOTREACHED() << "Invalid log type: " << log_type; 73 NOTREACHED() << "Invalid log type: " << log_type;
45 return scoped_ptr<LogParser>(); 74 return scoped_ptr<LogParser>();
46 } 75 }
47 76
48 void CollectLogInfo( 77 void CollectLogInfo(
49 FilterHandler* filter_handler, 78 FilterHandler* filter_handler,
50 system_logs::SystemLogsResponse* logs, 79 system_logs::SystemLogsResponse* logs,
51 std::vector<linked_ptr<api::log_private::LogEntry> >* output) { 80 std::vector<linked_ptr<api::log_private::LogEntry> >* output) {
52 for (system_logs::SystemLogsResponse::const_iterator 81 for (system_logs::SystemLogsResponse::const_iterator
53 request_it = logs->begin(); request_it != logs->end(); ++request_it) { 82 request_it = logs->begin(); request_it != logs->end(); ++request_it) {
54 if (!filter_handler->IsValidSource(request_it->first)) { 83 if (!filter_handler->IsValidSource(request_it->first)) {
55 continue; 84 continue;
56 } 85 }
57 scoped_ptr<LogParser> parser(CreateLogParser(request_it->first)); 86 scoped_ptr<LogParser> parser(CreateLogParser(request_it->first));
58 if (parser) { 87 if (parser) {
59 parser->Parse(request_it->second, output, filter_handler); 88 parser->Parse(request_it->second, output, filter_handler);
60 } 89 }
61 } 90 }
62 } 91 }
63 92
93 // Returns directory location of app-specific logs that are initiated with
94 // logPrivate.startEventRecorder() calls - /home/chronos/user/log/apps
95 base::FilePath GetAppLogDirectory() {
96 return logging::GetSessionLogDir(*CommandLine::ForCurrentProcess())
97 .Append(kAppLogsSubdir);
98 }
99
100 // Returns directory location where logs dumps initiated with chrome.dumpLogs
101 // will be stored - /home/chronos/<user_profile_dir>/Downloads/log_dumps
102 base::FilePath GetLogDumpDirectory(content::BrowserContext* context) {
103 const DownloadPrefs* const prefs = DownloadPrefs::FromBrowserContext(context);
104 return prefs->DownloadPath().Append(kLogDumpsSubdir);
105 }
106
107 // Removes direcotry content of |logs_dumps| and |app_logs_dir| (only for the
108 // primary profile).
109 void CleanUpLeftoverLogs(bool is_primary_profile,
110 const base::FilePath& app_logs_dir,
111 const base::FilePath& logs_dumps) {
112 LOG(WARNING) << "Deleting " << app_logs_dir.value();
113 LOG(WARNING) << "Deleting " << logs_dumps.value();
114
115 DCHECK(IsRunningOnSequenceThread());
116 base::DeleteFile(logs_dumps, true);
117
118 // App-specific logs are stored in /home/chronos/user/log/apps directory that
119 // is shared between all profiles in multi-profile case. We should not
120 // nuke it for non-primary profiles.
121 if (!is_primary_profile)
122 return;
123
124 base::DeleteFile(app_logs_dir, true);
125 }
126
64 } // namespace 127 } // namespace
65 128
129 const char FileResource::kSequenceToken[] = "log_api_files";
130
131 FileResource::FileResource(const std::string& owner_extension_id,
132 const base::FilePath& path)
133 : ApiResource(owner_extension_id), path_(path) {
134 }
135
136 FileResource::~FileResource() {
137 base::DeleteFile(path_, true);
138 }
139
140 bool FileResource::IsPersistent() const {
141 return false;
142 }
143
66 // static 144 // static
67 LogPrivateAPI* LogPrivateAPI::Get(content::BrowserContext* context) { 145 LogPrivateAPI* LogPrivateAPI::Get(content::BrowserContext* context) {
68 return GetFactoryInstance()->Get(context); 146 LogPrivateAPI* api = GetFactoryInstance()->Get(context);
147 api->Initialize();
148 return api;
69 } 149 }
70 150
71 LogPrivateAPI::LogPrivateAPI(content::BrowserContext* context) 151 LogPrivateAPI::LogPrivateAPI(content::BrowserContext* context)
72 : browser_context_(context), 152 : browser_context_(context),
73 logging_net_internals_(false), 153 logging_net_internals_(false),
74 extension_registry_observer_(this) { 154 event_sink_(api::log_private::EVENT_SINK_CAPTURE),
155 extension_registry_observer_(this),
156 log_file_resources_(context),
157 initialized_(false) {
75 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); 158 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_));
76 } 159 }
77 160
78 LogPrivateAPI::~LogPrivateAPI() { 161 LogPrivateAPI::~LogPrivateAPI() {
79 } 162 }
80 163
81 void LogPrivateAPI::StartNetInternalsWatch(const std::string& extension_id) { 164 void LogPrivateAPI::StartNetInternalsWatch(
165 const std::string& extension_id,
166 api::log_private::EventSink event_sink,
167 const base::Closure& closure) {
82 net_internal_watches_.insert(extension_id); 168 net_internal_watches_.insert(extension_id);
83 BrowserThread::PostTask( 169
84 BrowserThread::IO, FROM_HERE, 170 // Nuke any leftover app-specific or dumped log files from previous sessions.
171 BrowserThread::PostTaskAndReply(
172 BrowserThread::IO,
173 FROM_HERE,
85 base::Bind(&LogPrivateAPI::MaybeStartNetInternalLogging, 174 base::Bind(&LogPrivateAPI::MaybeStartNetInternalLogging,
86 base::Unretained(this))); 175 base::Unretained(this),
176 extension_id,
177 g_browser_process->io_thread(),
178 event_sink),
179 closure);
87 } 180 }
88 181
89 void LogPrivateAPI::StopNetInternalsWatch(const std::string& extension_id) { 182 void LogPrivateAPI::StopNetInternalsWatch(const std::string& extension_id,
183 const base::Closure& closure) {
90 net_internal_watches_.erase(extension_id); 184 net_internal_watches_.erase(extension_id);
91 MaybeStopNetInternalLogging(); 185 MaybeStopNetInternalLogging(closure);
186 }
187
188 void LogPrivateAPI::StopAllWatches(const std::string& extension_id,
189 const base::Closure& closure) {
190 StopNetInternalsWatch(extension_id, closure);
191 }
192
193 void LogPrivateAPI::RegisterTempFile(const std::string& owner_extension_id,
194 const base::FilePath& file_path) {
195 if (!IsRunningOnSequenceThread()) {
196 GetSequencedTaskRunner()->PostTask(
197 FROM_HERE,
198 base::Bind(&LogPrivateAPI::RegisterTempFile,
199 base::Unretained(this),
200 owner_extension_id,
201 file_path));
202 return;
203 }
204
205 log_file_resources_.Add(new FileResource(owner_extension_id, file_path));
92 } 206 }
93 207
94 static base::LazyInstance<BrowserContextKeyedAPIFactory<LogPrivateAPI> > 208 static base::LazyInstance<BrowserContextKeyedAPIFactory<LogPrivateAPI> >
95 g_factory = LAZY_INSTANCE_INITIALIZER; 209 g_factory = LAZY_INSTANCE_INITIALIZER;
96 210
97 // static 211 // static
98 BrowserContextKeyedAPIFactory<LogPrivateAPI>* 212 BrowserContextKeyedAPIFactory<LogPrivateAPI>*
99 LogPrivateAPI::GetFactoryInstance() { 213 LogPrivateAPI::GetFactoryInstance() {
100 return g_factory.Pointer(); 214 return g_factory.Pointer();
101 } 215 }
102 216
103 void LogPrivateAPI::OnAddEntry(const net::NetLog::Entry& entry) { 217 void LogPrivateAPI::OnAddEntry(const net::NetLog::Entry& entry) {
104 DCHECK_CURRENTLY_ON(BrowserThread::IO); 218 // We could receive events on whatever thread they happen to be generated,
219 // since we are only interested in network events, we should ignore any
220 // other thread than BrowserThread::IO.
221 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
222 return;
223 }
224
105 if (!pending_entries_.get()) { 225 if (!pending_entries_.get()) {
106 pending_entries_.reset(new base::ListValue()); 226 pending_entries_.reset(new base::ListValue());
107 BrowserThread::PostDelayedTask( 227 BrowserThread::PostDelayedTask(
108 BrowserThread::IO, FROM_HERE, 228 BrowserThread::IO, FROM_HERE,
109 base::Bind(&LogPrivateAPI::PostPendingEntries, base::Unretained(this)), 229 base::Bind(&LogPrivateAPI::PostPendingEntries, base::Unretained(this)),
110 base::TimeDelta::FromMilliseconds(kNetLogEventDelayMilliseconds)); 230 base::TimeDelta::FromMilliseconds(kNetLogEventDelayMilliseconds));
111 } 231 }
112 pending_entries_->Append(entry.ToValue()); 232 pending_entries_->Append(entry.ToValue());
113 } 233 }
114 234
115 void LogPrivateAPI::PostPendingEntries() { 235 void LogPrivateAPI::PostPendingEntries() {
116 BrowserThread::PostTask( 236 BrowserThread::PostTask(
117 BrowserThread::UI, FROM_HERE, 237 BrowserThread::UI, FROM_HERE,
118 base::Bind(&LogPrivateAPI:: AddEntriesOnUI, 238 base::Bind(&LogPrivateAPI:: AddEntriesOnUI,
119 base::Unretained(this), 239 base::Unretained(this),
120 base::Passed(&pending_entries_))); 240 base::Passed(&pending_entries_)));
121 } 241 }
122 242
123 void LogPrivateAPI::AddEntriesOnUI(scoped_ptr<base::ListValue> value) { 243 void LogPrivateAPI::AddEntriesOnUI(scoped_ptr<base::ListValue> value) {
124 DCHECK_CURRENTLY_ON(BrowserThread::UI); 244 DCHECK_CURRENTLY_ON(BrowserThread::UI);
125 245
126 for (std::set<std::string>::iterator ix = net_internal_watches_.begin(); 246 for (std::set<std::string>::iterator ix = net_internal_watches_.begin();
127 ix != net_internal_watches_.end(); ++ix) { 247 ix != net_internal_watches_.end(); ++ix) {
128 // Create the event's arguments value. 248 // Create the event's arguments value.
129 scoped_ptr<base::ListValue> event_args(new base::ListValue()); 249 scoped_ptr<base::ListValue> event_args(new base::ListValue());
130 event_args->Append(value->DeepCopy()); 250 event_args->Append(value->DeepCopy());
131 scoped_ptr<Event> event(new Event(events::kOnAddNetInternalsEntries, 251 scoped_ptr<Event> event(
132 event_args.Pass())); 252 new Event(events::kOnCapturedEvents, event_args.Pass()));
133 EventRouter::Get(browser_context_) 253 EventRouter::Get(browser_context_)
134 ->DispatchEventToExtension(*ix, event.Pass()); 254 ->DispatchEventToExtension(*ix, event.Pass());
135 } 255 }
136 } 256 }
137 257
138 void LogPrivateAPI::MaybeStartNetInternalLogging() { 258 void LogPrivateAPI::InitializeNetLogger(const std::string& owner_extension_id,
259 net::NetLogLogger** net_log_logger) {
260 DCHECK(IsRunningOnSequenceThread());
261 (*net_log_logger) = NULL;
262
263 // Create app-specific subdirectory in session logs folder.
264 base::FilePath app_log_dir = GetAppLogDirectory().Append(owner_extension_id);
265 if (!base::DirectoryExists(app_log_dir)) {
266 if (!base::CreateDirectory(app_log_dir)) {
267 LOG(ERROR) << "Could not create dir " << app_log_dir.value();
268 return;
269 }
270 }
271
272 base::FilePath file_path = app_log_dir.Append(kLogFileNameBase);
273 file_path = logging::GenerateTimestampedName(file_path, base::Time::Now());
274 FILE* file = NULL;
275 file = fopen(file_path.value().c_str(), "w");
276 if (file == NULL) {
277 LOG(ERROR) << "Could not open " << file_path.value();
278 return;
279 }
280
281 RegisterTempFile(owner_extension_id, file_path);
282 scoped_ptr<base::Value> constants(net::NetLogLogger::GetConstants());
283 *net_log_logger = new net::NetLogLogger(file, *constants);
284 (*net_log_logger)->set_log_level(net::NetLog::LOG_ALL_BUT_BYTES);
285 }
286
287 void LogPrivateAPI::StartObservingNetEvents(
288 IOThread* io_thread,
289 net::NetLogLogger** net_log_logger) {
290 DCHECK_CURRENTLY_ON(BrowserThread::IO);
291 if (!(*net_log_logger))
292 return;
293
294 net_log_logger_.reset(*net_log_logger);
295 net_log_logger_->StartObserving(io_thread->net_log());
296 }
297
298 void LogPrivateAPI::MaybeStartNetInternalLogging(
299 const std::string& caller_extension_id,
300 IOThread* io_thread,
301 api::log_private::EventSink event_sink) {
139 DCHECK_CURRENTLY_ON(BrowserThread::IO); 302 DCHECK_CURRENTLY_ON(BrowserThread::IO);
140 if (!logging_net_internals_) { 303 if (!logging_net_internals_) {
141 g_browser_process->io_thread()->net_log()->AddThreadSafeObserver(
142 this, net::NetLog::LOG_ALL_BUT_BYTES);
143 logging_net_internals_ = true; 304 logging_net_internals_ = true;
305 event_sink_ = event_sink;
306 switch (event_sink_) {
307 case api::log_private::EVENT_SINK_CAPTURE: {
308 io_thread->net_log()->AddThreadSafeObserver(
309 this, net::NetLog::LOG_ALL_BUT_BYTES);
310 break;
311 }
312 case api::log_private::EVENT_SINK_FILE: {
313 net::NetLogLogger** net_logger_ptr = new net::NetLogLogger* [1];
314 // Initialize net logger on the blocking pool and start observing event
315 // with in on IO thread.
316 GetSequencedTaskRunner()->PostTaskAndReply(
317 FROM_HERE,
318 base::Bind(&LogPrivateAPI::InitializeNetLogger,
319 base::Unretained(this),
320 caller_extension_id,
321 net_logger_ptr),
322 base::Bind(&LogPrivateAPI::StartObservingNetEvents,
323 base::Unretained(this),
324 io_thread,
325 base::Owned(net_logger_ptr)));
326 break;
327 }
328 case api::log_private::EVENT_SINK_NONE: {
329 NOTREACHED();
330 break;
331 }
332 }
144 } 333 }
145 } 334 }
146 335
147 void LogPrivateAPI::MaybeStopNetInternalLogging() { 336 void LogPrivateAPI::MaybeStopNetInternalLogging(const base::Closure& closure) {
148 if (net_internal_watches_.empty()) { 337 if (net_internal_watches_.empty()) {
149 BrowserThread::PostTask( 338 BrowserThread::PostTaskAndReply(
150 BrowserThread::IO, FROM_HERE, 339 BrowserThread::IO,
151 base::Bind(&LogPrivateAPI:: StopNetInternalLogging, 340 FROM_HERE,
152 base::Unretained(this))); 341 base::Bind(&LogPrivateAPI::StopNetInternalLogging,
342 base::Unretained(this)),
343 closure);
153 } 344 }
154 } 345 }
155 346
156 void LogPrivateAPI::StopNetInternalLogging() { 347 void LogPrivateAPI::StopNetInternalLogging() {
157 DCHECK_CURRENTLY_ON(BrowserThread::IO); 348 DCHECK_CURRENTLY_ON(BrowserThread::IO);
158 if (net_log() && logging_net_internals_) { 349 if (net_log() && logging_net_internals_) {
159 net_log()->RemoveThreadSafeObserver(this);
160 logging_net_internals_ = false; 350 logging_net_internals_ = false;
351 switch (event_sink_) {
352 case api::log_private::EVENT_SINK_CAPTURE:
353 net_log()->RemoveThreadSafeObserver(this);
354 break;
355 case api::log_private::EVENT_SINK_FILE:
356 net_log_logger_->StopObserving();
357 net_log_logger_.reset();
358 break;
359 case api::log_private::EVENT_SINK_NONE:
360 NOTREACHED();
361 break;
362 }
161 } 363 }
162 } 364 }
163 365
366 void LogPrivateAPI::Initialize() {
367 if (initialized_)
368 return;
369
370 // Clean up temp files and folders from the previous sessions.
371 initialized_ = true;
372 GetSequencedTaskRunner()->PostTask(
373 FROM_HERE,
374 base::Bind(&CleanUpLeftoverLogs,
375 Profile::FromBrowserContext(browser_context_) ==
376 ProfileManager::GetPrimaryUserProfile(),
377 GetAppLogDirectory(),
378 GetLogDumpDirectory(browser_context_)));
379 }
380
164 void LogPrivateAPI::OnExtensionUnloaded( 381 void LogPrivateAPI::OnExtensionUnloaded(
165 content::BrowserContext* browser_context, 382 content::BrowserContext* browser_context,
166 const Extension* extension, 383 const Extension* extension,
167 UnloadedExtensionInfo::Reason reason) { 384 UnloadedExtensionInfo::Reason reason) {
168 StopNetInternalsWatch(extension->id()); 385 StopNetInternalsWatch(extension->id(), base::Closure());
169 } 386 }
170 387
171 LogPrivateGetHistoricalFunction::LogPrivateGetHistoricalFunction() { 388 LogPrivateGetHistoricalFunction::LogPrivateGetHistoricalFunction() {
172 } 389 }
173 390
174 LogPrivateGetHistoricalFunction::~LogPrivateGetHistoricalFunction() { 391 LogPrivateGetHistoricalFunction::~LogPrivateGetHistoricalFunction() {
175 } 392 }
176 393
177 bool LogPrivateGetHistoricalFunction::RunAsync() { 394 bool LogPrivateGetHistoricalFunction::RunAsync() {
178 // Get parameters 395 // Get parameters
(...skipping 22 matching lines...) Expand all
201 418
202 // Prepare result 419 // Prepare result
203 api::log_private::Result result; 420 api::log_private::Result result;
204 result.data = data; 421 result.data = data;
205 api::log_private::Filter::Populate( 422 api::log_private::Filter::Populate(
206 *((filter_handler_->GetFilter())->ToValue()), &result.filter); 423 *((filter_handler_->GetFilter())->ToValue()), &result.filter);
207 SetResult(result.ToValue().release()); 424 SetResult(result.ToValue().release());
208 SendResponse(true); 425 SendResponse(true);
209 } 426 }
210 427
211 LogPrivateStartNetInternalsWatchFunction:: 428 LogPrivateStartEventRecorderFunction::LogPrivateStartEventRecorderFunction() {
212 LogPrivateStartNetInternalsWatchFunction() {
213 } 429 }
214 430
215 LogPrivateStartNetInternalsWatchFunction:: 431 LogPrivateStartEventRecorderFunction::~LogPrivateStartEventRecorderFunction() {
216 ~LogPrivateStartNetInternalsWatchFunction() {
217 } 432 }
218 433
219 bool LogPrivateStartNetInternalsWatchFunction::RunSync() { 434 bool LogPrivateStartEventRecorderFunction::RunAsync() {
220 LogPrivateAPI::Get(GetProfile())->StartNetInternalsWatch(extension_id()); 435 scoped_ptr<api::log_private::StartEventRecorder::Params> params(
436 api::log_private::StartEventRecorder::Params::Create(*args_));
437 EXTENSION_FUNCTION_VALIDATE(params.get());
438 switch (params->event_type) {
439 case api::log_private::EVENT_TYPE_NETWORK:
440 LogPrivateAPI::Get(Profile::FromBrowserContext(browser_context()))
441 ->StartNetInternalsWatch(
442 extension_id(),
443 params->sink,
444 base::Bind(
445 &LogPrivateStartEventRecorderFunction::OnEventRecorderStarted,
446 this));
447 break;
448 case api::log_private::EVENT_TYPE_NONE:
449 NOTREACHED();
450 return false;
451 }
452
221 return true; 453 return true;
222 } 454 }
223 455
224 LogPrivateStopNetInternalsWatchFunction:: 456 void LogPrivateStartEventRecorderFunction::OnEventRecorderStarted() {
225 LogPrivateStopNetInternalsWatchFunction() { 457 SendResponse(true);
226 } 458 }
227 459
228 LogPrivateStopNetInternalsWatchFunction:: 460 LogPrivateStopEventRecorderFunction::LogPrivateStopEventRecorderFunction() {
229 ~LogPrivateStopNetInternalsWatchFunction() {
230 } 461 }
231 462
232 bool LogPrivateStopNetInternalsWatchFunction::RunSync() { 463 LogPrivateStopEventRecorderFunction::~LogPrivateStopEventRecorderFunction() {
233 LogPrivateAPI::Get(GetProfile())->StopNetInternalsWatch(extension_id()); 464 }
465
466 bool LogPrivateStopEventRecorderFunction::RunAsync() {
467 scoped_ptr<api::log_private::StopEventRecorder::Params> params(
468 api::log_private::StopEventRecorder::Params::Create(*args_));
469 EXTENSION_FUNCTION_VALIDATE(params.get());
470 switch (params->event_type) {
471 case api::log_private::EVENT_TYPE_NETWORK:
472 LogPrivateAPI::Get(Profile::FromBrowserContext(browser_context()))
473 ->StopNetInternalsWatch(
474 extension_id(),
475 base::Bind(
476 &LogPrivateStopEventRecorderFunction::OnEventRecorderStopped,
477 this));
478 break;
479 case api::log_private::EVENT_TYPE_NONE:
480 NOTREACHED();
481 return false;
482 }
234 return true; 483 return true;
235 } 484 }
236 485
486 void LogPrivateStopEventRecorderFunction::OnEventRecorderStopped() {
487 SendResponse(true);
488 }
489
490 LogPrivateDumpLogsFunction::LogPrivateDumpLogsFunction() {
491 }
492
493 LogPrivateDumpLogsFunction::~LogPrivateDumpLogsFunction() {
494 }
495
496 bool LogPrivateDumpLogsFunction::RunAsync() {
497 LogPrivateAPI::Get(Profile::FromBrowserContext(browser_context()))
498 ->StopAllWatches(
499 extension_id(),
500 base::Bind(&LogPrivateDumpLogsFunction::OnStopAllWatches, this));
501 return true;
502 }
503
504 void LogPrivateDumpLogsFunction::OnStopAllWatches() {
505 chromeos::DebugLogWriter::StoreCombinedLogs(
506 GetLogDumpDirectory(browser_context()).Append(extension_id()),
507 FileResource::kSequenceToken,
508 base::Bind(&LogPrivateDumpLogsFunction::OnStoreLogsCompleted, this));
509 }
510
511 void LogPrivateDumpLogsFunction::OnStoreLogsCompleted(
512 const base::FilePath& log_path,
513 bool succeeded) {
514 if (succeeded) {
515 LogPrivateAPI::Get(Profile::FromBrowserContext(browser_context()))
516 ->RegisterTempFile(extension_id(), log_path);
517 }
518
519 scoped_ptr<base::DictionaryValue> response(new base::DictionaryValue());
520 extensions::GrantedFileEntry file_entry =
521 extensions::app_file_handler_util::CreateFileEntry(
522 Profile::FromBrowserContext(browser_context()),
523 GetExtension(),
524 render_view_host_->GetProcess()->GetID(),
525 log_path,
526 false);
527
528 base::DictionaryValue* entry = new base::DictionaryValue();
529 entry->SetString("fileSystemId", file_entry.filesystem_id);
530 entry->SetString("baseName", file_entry.registered_name);
531 entry->SetString("id", file_entry.id);
532 entry->SetBoolean("isDirectory", false);
533 response->Set("entry", entry);
534 SetResult(response.release());
535 SendResponse(succeeded);
536 }
537
237 } // namespace extensions 538 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698