| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/processes/processes_api.h" | 5 #include "chrome/browser/extensions/api/processes/processes_api.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <utility> |
| 9 | 10 |
| 10 #include "base/callback.h" | 11 #include "base/callback.h" |
| 11 #include "base/json/json_writer.h" | 12 #include "base/json/json_writer.h" |
| 12 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
| 13 #include "base/location.h" | 14 #include "base/location.h" |
| 14 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
| 15 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
| 16 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
| 18 #include "base/thread_task_runner_handle.h" | 19 #include "base/thread_task_runner_handle.h" |
| (...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 scoped_ptr<base::ListValue> args(new base::ListValue()); | 309 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 309 base::DictionaryValue* process = CreateProcessFromModel( | 310 base::DictionaryValue* process = CreateProcessFromModel( |
| 310 model_->GetUniqueChildProcessId(index), model_, index, false); | 311 model_->GetUniqueChildProcessId(index), model_, index, false); |
| 311 DCHECK(process != NULL); | 312 DCHECK(process != NULL); |
| 312 | 313 |
| 313 if (process == NULL) | 314 if (process == NULL) |
| 314 return; | 315 return; |
| 315 | 316 |
| 316 args->Append(process); | 317 args->Append(process); |
| 317 | 318 |
| 318 DispatchEvent(events::PROCESSES_ON_CREATED, keys::kOnCreated, args.Pass()); | 319 DispatchEvent(events::PROCESSES_ON_CREATED, keys::kOnCreated, |
| 320 std::move(args)); |
| 319 #endif // defined(ENABLE_TASK_MANAGER) | 321 #endif // defined(ENABLE_TASK_MANAGER) |
| 320 } | 322 } |
| 321 | 323 |
| 322 void ProcessesEventRouter::OnItemsChanged(int start, int length) { | 324 void ProcessesEventRouter::OnItemsChanged(int start, int length) { |
| 323 #if defined(ENABLE_TASK_MANAGER) | 325 #if defined(ENABLE_TASK_MANAGER) |
| 324 // If we don't have any listeners, return immediately. | 326 // If we don't have any listeners, return immediately. |
| 325 if (listeners_ == 0) | 327 if (listeners_ == 0) |
| 326 return; | 328 return; |
| 327 | 329 |
| 328 if (!model_) | 330 if (!model_) |
| (...skipping 27 matching lines...) Expand all Loading... |
| 356 for (; !it.IsAtEnd(); it.Advance()) { | 358 for (; !it.IsAtEnd(); it.Advance()) { |
| 357 if (!it.GetCurrentValue()->GetInteger(idkey, &id)) | 359 if (!it.GetCurrentValue()->GetInteger(idkey, &id)) |
| 358 continue; | 360 continue; |
| 359 | 361 |
| 360 // Store each process indexed by the string version of its id. | 362 // Store each process indexed by the string version of its id. |
| 361 processes->Set(base::IntToString(id), it.GetCurrentValue()); | 363 processes->Set(base::IntToString(id), it.GetCurrentValue()); |
| 362 } | 364 } |
| 363 | 365 |
| 364 scoped_ptr<base::ListValue> args(new base::ListValue()); | 366 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 365 args->Append(processes); | 367 args->Append(processes); |
| 366 DispatchEvent(events::PROCESSES_ON_UPDATED, keys::kOnUpdated, args.Pass()); | 368 DispatchEvent(events::PROCESSES_ON_UPDATED, keys::kOnUpdated, |
| 369 std::move(args)); |
| 367 } | 370 } |
| 368 | 371 |
| 369 if (updated_memory) { | 372 if (updated_memory) { |
| 370 IDMap<base::DictionaryValue>::iterator it(&processes_map); | 373 IDMap<base::DictionaryValue>::iterator it(&processes_map); |
| 371 for (; !it.IsAtEnd(); it.Advance()) { | 374 for (; !it.IsAtEnd(); it.Advance()) { |
| 372 if (!it.GetCurrentValue()->GetInteger(idkey, &id)) | 375 if (!it.GetCurrentValue()->GetInteger(idkey, &id)) |
| 373 continue; | 376 continue; |
| 374 | 377 |
| 375 AddMemoryDetails(it.GetCurrentValue(), model_, it.GetCurrentKey()); | 378 AddMemoryDetails(it.GetCurrentValue(), model_, it.GetCurrentKey()); |
| 376 | 379 |
| 377 // Store each process indexed by the string version of its id if we didn't | 380 // Store each process indexed by the string version of its id if we didn't |
| 378 // already insert it as part of the onUpdated processing above. | 381 // already insert it as part of the onUpdated processing above. |
| 379 if (!updated) | 382 if (!updated) |
| 380 processes->Set(base::IntToString(id), it.GetCurrentValue()); | 383 processes->Set(base::IntToString(id), it.GetCurrentValue()); |
| 381 } | 384 } |
| 382 | 385 |
| 383 scoped_ptr<base::ListValue> args(new base::ListValue()); | 386 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 384 args->Append(processes); | 387 args->Append(processes); |
| 385 DispatchEvent(events::PROCESSES_ON_UPDATED_WITH_MEMORY, | 388 DispatchEvent(events::PROCESSES_ON_UPDATED_WITH_MEMORY, |
| 386 keys::kOnUpdatedWithMemory, args.Pass()); | 389 keys::kOnUpdatedWithMemory, std::move(args)); |
| 387 } | 390 } |
| 388 #endif // defined(ENABLE_TASK_MANAGER) | 391 #endif // defined(ENABLE_TASK_MANAGER) |
| 389 } | 392 } |
| 390 | 393 |
| 391 void ProcessesEventRouter::OnItemsToBeRemoved(int start, int length) { | 394 void ProcessesEventRouter::OnItemsToBeRemoved(int start, int length) { |
| 392 #if defined(ENABLE_TASK_MANAGER) | 395 #if defined(ENABLE_TASK_MANAGER) |
| 393 DCHECK_EQ(length, 1); | 396 DCHECK_EQ(length, 1); |
| 394 | 397 |
| 395 // Process exit for renderer processes has the data about exit code and | 398 // Process exit for renderer processes has the data about exit code and |
| 396 // termination status, therefore we will rely on notifications and not on | 399 // termination status, therefore we will rely on notifications and not on |
| 397 // the Task Manager data. We do use the rest of this method for non-renderer | 400 // the Task Manager data. We do use the rest of this method for non-renderer |
| 398 // processes. | 401 // processes. |
| 399 if (model_->GetResourceType(start) == task_manager::Resource::RENDERER) | 402 if (model_->GetResourceType(start) == task_manager::Resource::RENDERER) |
| 400 return; | 403 return; |
| 401 | 404 |
| 402 // The callback function parameters. | 405 // The callback function parameters. |
| 403 scoped_ptr<base::ListValue> args(new base::ListValue()); | 406 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 404 | 407 |
| 405 // First arg: The id of the process that was closed. | 408 // First arg: The id of the process that was closed. |
| 406 args->Append(new base::FundamentalValue( | 409 args->Append(new base::FundamentalValue( |
| 407 model_->GetUniqueChildProcessId(start))); | 410 model_->GetUniqueChildProcessId(start))); |
| 408 | 411 |
| 409 // Second arg: The exit type for the process. | 412 // Second arg: The exit type for the process. |
| 410 args->Append(new base::FundamentalValue(0)); | 413 args->Append(new base::FundamentalValue(0)); |
| 411 | 414 |
| 412 // Third arg: The exit code for the process. | 415 // Third arg: The exit code for the process. |
| 413 args->Append(new base::FundamentalValue(0)); | 416 args->Append(new base::FundamentalValue(0)); |
| 414 | 417 |
| 415 DispatchEvent(events::PROCESSES_ON_EXITED, keys::kOnExited, args.Pass()); | 418 DispatchEvent(events::PROCESSES_ON_EXITED, keys::kOnExited, std::move(args)); |
| 416 #endif // defined(ENABLE_TASK_MANAGER) | 419 #endif // defined(ENABLE_TASK_MANAGER) |
| 417 } | 420 } |
| 418 | 421 |
| 419 void ProcessesEventRouter::ProcessHangEvent(content::RenderWidgetHost* widget) { | 422 void ProcessesEventRouter::ProcessHangEvent(content::RenderWidgetHost* widget) { |
| 420 #if defined(ENABLE_TASK_MANAGER) | 423 #if defined(ENABLE_TASK_MANAGER) |
| 421 std::string event(keys::kOnUnresponsive); | 424 std::string event(keys::kOnUnresponsive); |
| 422 if (!HasEventListeners(event)) | 425 if (!HasEventListeners(event)) |
| 423 return; | 426 return; |
| 424 | 427 |
| 425 base::DictionaryValue* process = NULL; | 428 base::DictionaryValue* process = NULL; |
| 426 int count = model_->ResourceCount(); | 429 int count = model_->ResourceCount(); |
| 427 int id = widget->GetProcess()->GetID(); | 430 int id = widget->GetProcess()->GetID(); |
| 428 | 431 |
| 429 for (int i = 0; i < count; ++i) { | 432 for (int i = 0; i < count; ++i) { |
| 430 if (model_->IsResourceFirstInGroup(i)) { | 433 if (model_->IsResourceFirstInGroup(i)) { |
| 431 if (id == model_->GetUniqueChildProcessId(i)) { | 434 if (id == model_->GetUniqueChildProcessId(i)) { |
| 432 process = CreateProcessFromModel(id, model_, i, false); | 435 process = CreateProcessFromModel(id, model_, i, false); |
| 433 break; | 436 break; |
| 434 } | 437 } |
| 435 } | 438 } |
| 436 } | 439 } |
| 437 | 440 |
| 438 if (process == NULL) | 441 if (process == NULL) |
| 439 return; | 442 return; |
| 440 | 443 |
| 441 scoped_ptr<base::ListValue> args(new base::ListValue()); | 444 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 442 args->Append(process); | 445 args->Append(process); |
| 443 | 446 |
| 444 DispatchEvent(events::PROCESSES_ON_UNRESPONSIVE, keys::kOnUnresponsive, | 447 DispatchEvent(events::PROCESSES_ON_UNRESPONSIVE, keys::kOnUnresponsive, |
| 445 args.Pass()); | 448 std::move(args)); |
| 446 #endif // defined(ENABLE_TASK_MANAGER) | 449 #endif // defined(ENABLE_TASK_MANAGER) |
| 447 } | 450 } |
| 448 | 451 |
| 449 void ProcessesEventRouter::ProcessClosedEvent( | 452 void ProcessesEventRouter::ProcessClosedEvent( |
| 450 content::RenderProcessHost* rph, | 453 content::RenderProcessHost* rph, |
| 451 content::RenderProcessHost::RendererClosedDetails* details) { | 454 content::RenderProcessHost::RendererClosedDetails* details) { |
| 452 #if defined(ENABLE_TASK_MANAGER) | 455 #if defined(ENABLE_TASK_MANAGER) |
| 453 // The callback function parameters. | 456 // The callback function parameters. |
| 454 scoped_ptr<base::ListValue> args(new base::ListValue()); | 457 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 455 | 458 |
| 456 // First arg: The id of the process that was closed. | 459 // First arg: The id of the process that was closed. |
| 457 args->Append(new base::FundamentalValue(rph->GetID())); | 460 args->Append(new base::FundamentalValue(rph->GetID())); |
| 458 | 461 |
| 459 // Second arg: The exit type for the process. | 462 // Second arg: The exit type for the process. |
| 460 args->Append(new base::FundamentalValue(details->status)); | 463 args->Append(new base::FundamentalValue(details->status)); |
| 461 | 464 |
| 462 // Third arg: The exit code for the process. | 465 // Third arg: The exit code for the process. |
| 463 args->Append(new base::FundamentalValue(details->exit_code)); | 466 args->Append(new base::FundamentalValue(details->exit_code)); |
| 464 | 467 |
| 465 DispatchEvent(events::PROCESSES_ON_EXITED, keys::kOnExited, args.Pass()); | 468 DispatchEvent(events::PROCESSES_ON_EXITED, keys::kOnExited, std::move(args)); |
| 466 #endif // defined(ENABLE_TASK_MANAGER) | 469 #endif // defined(ENABLE_TASK_MANAGER) |
| 467 } | 470 } |
| 468 | 471 |
| 469 void ProcessesEventRouter::DispatchEvent( | 472 void ProcessesEventRouter::DispatchEvent( |
| 470 events::HistogramValue histogram_value, | 473 events::HistogramValue histogram_value, |
| 471 const std::string& event_name, | 474 const std::string& event_name, |
| 472 scoped_ptr<base::ListValue> event_args) { | 475 scoped_ptr<base::ListValue> event_args) { |
| 473 EventRouter* event_router = EventRouter::Get(browser_context_); | 476 EventRouter* event_router = EventRouter::Get(browser_context_); |
| 474 if (event_router) { | 477 if (event_router) { |
| 475 scoped_ptr<Event> event( | 478 scoped_ptr<Event> event( |
| 476 new Event(histogram_value, event_name, event_args.Pass())); | 479 new Event(histogram_value, event_name, std::move(event_args))); |
| 477 event_router->BroadcastEvent(event.Pass()); | 480 event_router->BroadcastEvent(std::move(event)); |
| 478 } | 481 } |
| 479 } | 482 } |
| 480 | 483 |
| 481 bool ProcessesEventRouter::HasEventListeners(const std::string& event_name) { | 484 bool ProcessesEventRouter::HasEventListeners(const std::string& event_name) { |
| 482 EventRouter* event_router = EventRouter::Get(browser_context_); | 485 EventRouter* event_router = EventRouter::Get(browser_context_); |
| 483 return event_router && event_router->HasEventListener(event_name); | 486 return event_router && event_router->HasEventListener(event_name); |
| 484 } | 487 } |
| 485 | 488 |
| 486 ProcessesAPI::ProcessesAPI(content::BrowserContext* context) | 489 ProcessesAPI::ProcessesAPI(content::BrowserContext* context) |
| 487 : browser_context_(context) { | 490 : browser_context_(context) { |
| (...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 773 | 776 |
| 774 SetResult(processes); | 777 SetResult(processes); |
| 775 SendResponse(true); | 778 SendResponse(true); |
| 776 | 779 |
| 777 // Balance the AddRef in the RunAsync. | 780 // Balance the AddRef in the RunAsync. |
| 778 Release(); | 781 Release(); |
| 779 #endif // defined(ENABLE_TASK_MANAGER) | 782 #endif // defined(ENABLE_TASK_MANAGER) |
| 780 } | 783 } |
| 781 | 784 |
| 782 } // namespace extensions | 785 } // namespace extensions |
| OLD | NEW |