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

Side by Side Diff: chrome/browser/extensions/api/processes/processes_api.cc

Issue 166053003: Move ProfileKeyedAPI implementations to take BrowserContext in the constructor (part 2). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: | Created 6 years, 10 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 | Annotate | Revision Log
OLDNEW
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/lazy_instance.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
11 #include "base/metrics/histogram.h" 11 #include "base/metrics/histogram.h"
12 #include "base/strings/string_number_conversions.h" 12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
14 #include "base/values.h" 14 #include "base/values.h"
15 #include "chrome/browser/chrome_notification_types.h" 15 #include "chrome/browser/chrome_notification_types.h"
16 #include "chrome/browser/extensions/api/processes/processes_api_constants.h" 16 #include "chrome/browser/extensions/api/processes/processes_api_constants.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/extension_function_registry.h" 18 #include "chrome/browser/extensions/extension_function_registry.h"
19 #include "chrome/browser/extensions/extension_function_util.h" 19 #include "chrome/browser/extensions/extension_function_util.h"
20 #include "chrome/browser/extensions/extension_service.h" 20 #include "chrome/browser/extensions/extension_service.h"
21 #include "chrome/browser/extensions/extension_tab_util.h" 21 #include "chrome/browser/extensions/extension_tab_util.h"
22 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/browser/task_manager/resource_provider.h" 22 #include "chrome/browser/task_manager/resource_provider.h"
24 #include "chrome/browser/task_manager/task_manager.h" 23 #include "chrome/browser/task_manager/task_manager.h"
24 #include "content/public/browser/browser_context.h"
25 #include "content/public/browser/notification_details.h" 25 #include "content/public/browser/notification_details.h"
26 #include "content/public/browser/notification_service.h" 26 #include "content/public/browser/notification_service.h"
27 #include "content/public/browser/notification_source.h" 27 #include "content/public/browser/notification_source.h"
28 #include "content/public/browser/notification_types.h" 28 #include "content/public/browser/notification_types.h"
29 #include "content/public/browser/render_process_host.h" 29 #include "content/public/browser/render_process_host.h"
30 #include "content/public/browser/render_view_host.h" 30 #include "content/public/browser/render_view_host.h"
31 #include "content/public/browser/render_widget_host.h" 31 #include "content/public/browser/render_widget_host.h"
32 #include "content/public/browser/render_widget_host_iterator.h" 32 #include "content/public/browser/render_widget_host_iterator.h"
33 #include "content/public/browser/web_contents.h" 33 #include "content/public/browser/web_contents.h"
34 #include "content/public/common/result_codes.h" 34 #include "content/public/common/result_codes.h"
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 size_t mem; 205 size_t mem;
206 int64 pr_mem = model->GetPrivateMemory(index, &mem) ? 206 int64 pr_mem = model->GetPrivateMemory(index, &mem) ?
207 static_cast<int64>(mem) : -1; 207 static_cast<int64>(mem) : -1;
208 result->SetDouble(keys::kPrivateMemoryKey, static_cast<double>(pr_mem)); 208 result->SetDouble(keys::kPrivateMemoryKey, static_cast<double>(pr_mem));
209 } 209 }
210 210
211 #endif // defined(ENABLE_TASK_MANAGER) 211 #endif // defined(ENABLE_TASK_MANAGER)
212 212
213 } // namespace 213 } // namespace
214 214
215 ProcessesEventRouter::ProcessesEventRouter(Profile* profile) 215 ProcessesEventRouter::ProcessesEventRouter(content::BrowserContext* context)
216 : profile_(profile), 216 : browser_context_(context), listeners_(0), task_manager_listening_(false) {
217 listeners_(0),
218 task_manager_listening_(false) {
219 #if defined(ENABLE_TASK_MANAGER) 217 #if defined(ENABLE_TASK_MANAGER)
220 model_ = TaskManager::GetInstance()->model(); 218 model_ = TaskManager::GetInstance()->model();
221 model_->AddObserver(this); 219 model_->AddObserver(this);
222 220
223 registrar_.Add(this, content::NOTIFICATION_RENDER_WIDGET_HOST_HANG, 221 registrar_.Add(this, content::NOTIFICATION_RENDER_WIDGET_HOST_HANG,
224 content::NotificationService::AllSources()); 222 content::NotificationService::AllSources());
225 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, 223 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED,
226 content::NotificationService::AllSources()); 224 content::NotificationService::AllSources());
227 #endif // defined(ENABLE_TASK_MANAGER) 225 #endif // defined(ENABLE_TASK_MANAGER)
228 } 226 }
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after
462 // Third arg: The exit code for the process. 460 // Third arg: The exit code for the process.
463 args->Append(new base::FundamentalValue(details->exit_code)); 461 args->Append(new base::FundamentalValue(details->exit_code));
464 462
465 DispatchEvent(keys::kOnExited, args.Pass()); 463 DispatchEvent(keys::kOnExited, args.Pass());
466 #endif // defined(ENABLE_TASK_MANAGER) 464 #endif // defined(ENABLE_TASK_MANAGER)
467 } 465 }
468 466
469 void ProcessesEventRouter::DispatchEvent( 467 void ProcessesEventRouter::DispatchEvent(
470 const std::string& event_name, 468 const std::string& event_name,
471 scoped_ptr<base::ListValue> event_args) { 469 scoped_ptr<base::ListValue> event_args) {
472 if (extensions::ExtensionSystem::Get(profile_)->event_router()) { 470 if (extensions::ExtensionSystem::Get(browser_context_)->event_router()) {
473 scoped_ptr<extensions::Event> event(new extensions::Event( 471 scoped_ptr<extensions::Event> event(new extensions::Event(
474 event_name, event_args.Pass())); 472 event_name, event_args.Pass()));
475 extensions::ExtensionSystem::Get(profile_)->event_router()-> 473 extensions::ExtensionSystem::Get(browser_context_)
476 BroadcastEvent(event.Pass()); 474 ->event_router()
475 ->BroadcastEvent(event.Pass());
477 } 476 }
478 } 477 }
479 478
480 bool ProcessesEventRouter::HasEventListeners(const std::string& event_name) { 479 bool ProcessesEventRouter::HasEventListeners(const std::string& event_name) {
481 extensions::EventRouter* router = 480 extensions::EventRouter* router =
482 extensions::ExtensionSystem::Get(profile_)->event_router(); 481 extensions::ExtensionSystem::Get(browser_context_)->event_router();
483 if (router && router->HasEventListener(event_name)) 482 if (router && router->HasEventListener(event_name))
484 return true; 483 return true;
485 return false; 484 return false;
486 } 485 }
487 486
488 ProcessesAPI::ProcessesAPI(Profile* profile) : profile_(profile) { 487 ProcessesAPI::ProcessesAPI(content::BrowserContext* context)
489 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( 488 : browser_context_(context) {
489 ExtensionSystem::Get(browser_context_)->event_router()->RegisterObserver(
490 this, processes_api_constants::kOnUpdated); 490 this, processes_api_constants::kOnUpdated);
491 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( 491 ExtensionSystem::Get(browser_context_)->event_router()->RegisterObserver(
492 this, processes_api_constants::kOnUpdatedWithMemory); 492 this, processes_api_constants::kOnUpdatedWithMemory);
493 ExtensionFunctionRegistry* registry = 493 ExtensionFunctionRegistry* registry =
494 ExtensionFunctionRegistry::GetInstance(); 494 ExtensionFunctionRegistry::GetInstance();
495 registry->RegisterFunction<extensions::GetProcessIdForTabFunction>(); 495 registry->RegisterFunction<extensions::GetProcessIdForTabFunction>();
496 registry->RegisterFunction<extensions::TerminateFunction>(); 496 registry->RegisterFunction<extensions::TerminateFunction>();
497 registry->RegisterFunction<extensions::GetProcessInfoFunction>(); 497 registry->RegisterFunction<extensions::GetProcessInfoFunction>();
498 } 498 }
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(browser_context_)->event_router()->UnregisterObserver(
505 this);
505 } 506 }
506 507
507 static base::LazyInstance<ProfileKeyedAPIFactory<ProcessesAPI> > 508 static base::LazyInstance<ProfileKeyedAPIFactory<ProcessesAPI> >
508 g_factory = LAZY_INSTANCE_INITIALIZER; 509 g_factory = LAZY_INSTANCE_INITIALIZER;
509 510
510 // static 511 // static
511 ProfileKeyedAPIFactory<ProcessesAPI>* ProcessesAPI::GetFactoryInstance() { 512 ProfileKeyedAPIFactory<ProcessesAPI>* ProcessesAPI::GetFactoryInstance() {
512 return g_factory.Pointer(); 513 return g_factory.Pointer();
513 } 514 }
514 515
515 // static 516 // static
516 ProcessesAPI* ProcessesAPI::Get(Profile* profile) { 517 ProcessesAPI* ProcessesAPI::Get(content::BrowserContext* context) {
517 return ProfileKeyedAPIFactory<ProcessesAPI>::GetForProfile(profile); 518 return ProfileKeyedAPIFactory<ProcessesAPI>::GetForProfile(context);
518 } 519 }
519 520
520 ProcessesEventRouter* ProcessesAPI::processes_event_router() { 521 ProcessesEventRouter* ProcessesAPI::processes_event_router() {
521 if (!processes_event_router_) 522 if (!processes_event_router_)
522 processes_event_router_.reset(new ProcessesEventRouter(profile_)); 523 processes_event_router_.reset(new ProcessesEventRouter(browser_context_));
523 return processes_event_router_.get(); 524 return processes_event_router_.get();
524 } 525 }
525 526
526 void ProcessesAPI::OnListenerAdded(const EventListenerInfo& details) { 527 void ProcessesAPI::OnListenerAdded(const EventListenerInfo& details) {
527 // We lazily tell the TaskManager to start updating when listeners to the 528 // We lazily tell the TaskManager to start updating when listeners to the
528 // processes.onUpdated or processes.onUpdatedWithMemory events arrive. 529 // processes.onUpdated or processes.onUpdatedWithMemory events arrive.
529 processes_event_router()->ListenerAdded(); 530 processes_event_router()->ListenerAdded();
530 } 531 }
531 532
532 void ProcessesAPI::OnListenerRemoved(const EventListenerInfo& details) { 533 void ProcessesAPI::OnListenerRemoved(const EventListenerInfo& details) {
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
758 759
759 SetResult(processes); 760 SetResult(processes);
760 SendResponse(true); 761 SendResponse(true);
761 762
762 // Balance the AddRef in the RunImpl. 763 // Balance the AddRef in the RunImpl.
763 Release(); 764 Release();
764 #endif // defined(ENABLE_TASK_MANAGER) 765 #endif // defined(ENABLE_TASK_MANAGER)
765 } 766 }
766 767
767 } // namespace extensions 768 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698