Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/gamepad/gamepad_service.h" | |
| 6 | |
| 7 #include "content/browser/gamepad/data_fetcher.h" | |
| 8 #include "content/browser/gamepad/gamepad_provider.h" | |
| 9 #include "content/public/browser/browser_thread.h" | |
| 10 #include "content/public/browser/notification_service.h" | |
| 11 #include "content/public/browser/notification_source.h" | |
| 12 #include "content/public/browser/notification_types.h" | |
| 13 #include "content/public/browser/render_process_host.h" | |
| 14 | |
| 15 namespace content { | |
| 16 | |
| 17 GamepadService* GamepadService::instance_; | |
| 18 | |
| 19 GamepadService::GamepadService() : num_readers_(0) { | |
| 20 } | |
| 21 | |
| 22 GamepadService::~GamepadService() { | |
| 23 } | |
| 24 | |
| 25 GamepadService* GamepadService::GetInstance() { | |
| 26 if (!instance_) { | |
| 27 instance_ = new GamepadService(); | |
|
jam
2011/12/01 20:07:31
just use Singleton for singletons :)
scottmg
2011/12/01 22:05:32
Done. (was because of the refcount cruft which is
| |
| 28 instance_->AddRef(); | |
| 29 } | |
| 30 return instance_; | |
| 31 } | |
| 32 | |
| 33 void GamepadService::Start( | |
| 34 GamepadDataFetcher* data_fetcher, | |
| 35 content::RenderProcessHost* associated_rph) { | |
| 36 num_readers_++; | |
| 37 if (!provider_) | |
| 38 provider_ = new GamepadProvider(data_fetcher); | |
| 39 DCHECK(num_readers_ > 0); | |
| 40 provider_->Resume(); | |
| 41 | |
| 42 content::BrowserThread::PostTask( | |
| 43 content::BrowserThread::UI, | |
| 44 FROM_HERE, | |
| 45 base::Bind(&GamepadService::RegisterForCloseNotification, | |
| 46 this, | |
| 47 associated_rph)); | |
| 48 } | |
| 49 | |
| 50 void GamepadService::RegisterForCloseNotification( | |
| 51 content::RenderProcessHost* rph) { | |
| 52 registrar_.Add(this, | |
| 53 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | |
| 54 content::Source<content::RenderProcessHost>(rph)); | |
| 55 } | |
| 56 | |
| 57 base::SharedMemoryHandle GamepadService::GetSharedMemoryHandle( | |
| 58 base::ProcessHandle handle) { | |
| 59 DCHECK(provider_); | |
|
jam
2011/12/01 20:07:31
nit: by convention we avoid DCHECKing variables be
scottmg
2011/12/01 22:05:32
Done.
| |
| 60 return provider_->GetRendererSharedMemoryHandle(handle); | |
| 61 } | |
| 62 | |
| 63 void GamepadService::Stop() { | |
| 64 --num_readers_; | |
| 65 DCHECK(provider_); | |
|
jam
2011/12/01 20:07:31
ditto
scottmg
2011/12/01 22:05:32
Done.
| |
| 66 DCHECK(num_readers_ >= 0); | |
| 67 | |
| 68 if (num_readers_ == 0) | |
| 69 provider_->Pause(); | |
| 70 } | |
| 71 | |
| 72 void GamepadService::Observe(int type, | |
| 73 const content::NotificationSource& source, | |
| 74 const content::NotificationDetails& details) { | |
| 75 switch (type) { | |
|
jam
2011/12/01 20:07:31
nit: just do line 77-80 only since you're register
scottmg
2011/12/01 22:05:32
Done.
| |
| 76 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { | |
| 77 content::BrowserThread::PostTask( | |
| 78 content::BrowserThread::IO, | |
| 79 FROM_HERE, | |
| 80 base::Bind(&GamepadService::Stop, this)); | |
| 81 break; | |
| 82 } | |
| 83 default: { | |
| 84 break; | |
| 85 } | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 } // namespace content | |
| OLD | NEW |