| 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 "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 9 #include "base/lazy_instance.h" |
| 9 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 10 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 11 #include "base/string_number_conversions.h" | 12 #include "base/string_number_conversions.h" |
| 12 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 13 #include "base/values.h" | 14 #include "base/values.h" |
| 14 | 15 |
| 15 #include "chrome/browser/extensions/api/processes/processes_api_constants.h" | 16 #include "chrome/browser/extensions/api/processes/processes_api_constants.h" |
| 16 #include "chrome/browser/extensions/api/processes/processes_api_factory.h" | |
| 17 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" | 17 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" |
| 18 #include "chrome/browser/extensions/event_router.h" | 18 #include "chrome/browser/extensions/event_router.h" |
| 19 #include "chrome/browser/extensions/extension_function_registry.h" | 19 #include "chrome/browser/extensions/extension_function_registry.h" |
| 20 #include "chrome/browser/extensions/extension_function_util.h" | 20 #include "chrome/browser/extensions/extension_function_util.h" |
| 21 #include "chrome/browser/extensions/extension_service.h" | 21 #include "chrome/browser/extensions/extension_service.h" |
| 22 #include "chrome/browser/extensions/extension_system.h" | 22 #include "chrome/browser/extensions/extension_system.h" |
| 23 #include "chrome/browser/extensions/extension_tab_util.h" | 23 #include "chrome/browser/extensions/extension_tab_util.h" |
| 24 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
| 25 #include "chrome/browser/task_manager/task_manager.h" | 25 #include "chrome/browser/task_manager/task_manager.h" |
| 26 #include "chrome/common/chrome_notification_types.h" | 26 #include "chrome/common/chrome_notification_types.h" |
| (...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 499 | 499 |
| 500 ProcessesAPI::~ProcessesAPI() { | 500 ProcessesAPI::~ProcessesAPI() { |
| 501 } | 501 } |
| 502 | 502 |
| 503 void ProcessesAPI::Shutdown() { | 503 void ProcessesAPI::Shutdown() { |
| 504 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); | 504 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); |
| 505 } | 505 } |
| 506 | 506 |
| 507 // static | 507 // static |
| 508 ProcessesAPI* ProcessesAPI::Get(Profile* profile) { | 508 ProcessesAPI* ProcessesAPI::Get(Profile* profile) { |
| 509 return ProcessesAPIFactory::GetForProfile(profile); | 509 return ProfileKeyedAPIFactory<ProcessesAPI>::GetForProfile(profile); |
| 510 } | 510 } |
| 511 | 511 |
| 512 ProcessesEventRouter* ProcessesAPI::processes_event_router() { | 512 ProcessesEventRouter* ProcessesAPI::processes_event_router() { |
| 513 if (!processes_event_router_) | 513 if (!processes_event_router_) |
| 514 processes_event_router_.reset(new ProcessesEventRouter(profile_)); | 514 processes_event_router_.reset(new ProcessesEventRouter(profile_)); |
| 515 return processes_event_router_.get(); | 515 return processes_event_router_.get(); |
| 516 } | 516 } |
| 517 | 517 |
| 518 void ProcessesAPI::OnListenerAdded(const EventListenerInfo& details) { | 518 void ProcessesAPI::OnListenerAdded(const EventListenerInfo& details) { |
| 519 // We lazily tell the TaskManager to start updating when listeners to the | 519 // We lazily tell the TaskManager to start updating when listeners to the |
| 520 // processes.onUpdated or processes.onUpdatedWithMemory events arrive. | 520 // processes.onUpdated or processes.onUpdatedWithMemory events arrive. |
| 521 processes_event_router()->ListenerAdded(); | 521 processes_event_router()->ListenerAdded(); |
| 522 } | 522 } |
| 523 | 523 |
| 524 void ProcessesAPI::OnListenerRemoved(const EventListenerInfo& details) { | 524 void ProcessesAPI::OnListenerRemoved(const EventListenerInfo& details) { |
| 525 // If a processes.onUpdated or processes.onUpdatedWithMemory event listener | 525 // If a processes.onUpdated or processes.onUpdatedWithMemory event listener |
| 526 // is removed (or a process with one exits), then we let the extension API | 526 // is removed (or a process with one exits), then we let the extension API |
| 527 // know that it has one fewer listener. | 527 // know that it has one fewer listener. |
| 528 processes_event_router()->ListenerRemoved(); | 528 processes_event_router()->ListenerRemoved(); |
| 529 } | 529 } |
| 530 | 530 |
| 531 static base::LazyInstance<ProfileKeyedAPIFactory<ProcessesAPI> > |
| 532 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 533 |
| 534 template <> |
| 535 ProfileKeyedAPIFactory<ProcessesAPI>* |
| 536 ProfileKeyedAPIFactory<ProcessesAPI>::GetInstance() { |
| 537 return &g_factory.Get(); |
| 538 } |
| 539 |
| 531 GetProcessIdForTabFunction::GetProcessIdForTabFunction() : tab_id_(-1) { | 540 GetProcessIdForTabFunction::GetProcessIdForTabFunction() : tab_id_(-1) { |
| 532 } | 541 } |
| 533 | 542 |
| 534 bool GetProcessIdForTabFunction::RunImpl() { | 543 bool GetProcessIdForTabFunction::RunImpl() { |
| 535 #if defined(ENABLE_TASK_MANAGER) | 544 #if defined(ENABLE_TASK_MANAGER) |
| 536 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id_)); | 545 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id_)); |
| 537 | 546 |
| 538 // Add a reference, which is balanced in GetProcessIdForTab to keep the object | 547 // Add a reference, which is balanced in GetProcessIdForTab to keep the object |
| 539 // around and allow for the callback to be invoked. | 548 // around and allow for the callback to be invoked. |
| 540 AddRef(); | 549 AddRef(); |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 764 | 773 |
| 765 SetResult(processes); | 774 SetResult(processes); |
| 766 SendResponse(true); | 775 SendResponse(true); |
| 767 | 776 |
| 768 // Balance the AddRef in the RunImpl. | 777 // Balance the AddRef in the RunImpl. |
| 769 Release(); | 778 Release(); |
| 770 #endif // defined(ENABLE_TASK_MANAGER) | 779 #endif // defined(ENABLE_TASK_MANAGER) |
| 771 } | 780 } |
| 772 | 781 |
| 773 } // namespace extensions | 782 } // namespace extensions |
| OLD | NEW |