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 "base/bind.h" |
| 8 #include "base/memory/singleton.h" |
| 9 #include "content/browser/gamepad/data_fetcher.h" |
| 10 #include "content/browser/gamepad/gamepad_provider.h" |
| 11 #include "content/public/browser/browser_thread.h" |
| 12 #include "content/public/browser/notification_service.h" |
| 13 #include "content/public/browser/notification_source.h" |
| 14 #include "content/public/browser/notification_types.h" |
| 15 #include "content/public/browser/render_process_host.h" |
| 16 |
| 17 namespace content { |
| 18 |
| 19 GamepadService::GamepadService() : num_readers_(0) { |
| 20 } |
| 21 |
| 22 GamepadService::~GamepadService() { |
| 23 } |
| 24 |
| 25 GamepadService* GamepadService::GetInstance() { |
| 26 return Singleton<GamepadService>::get(); |
| 27 } |
| 28 |
| 29 void GamepadService::Start( |
| 30 GamepadDataFetcher* data_fetcher, |
| 31 content::RenderProcessHost* associated_rph) { |
| 32 num_readers_++; |
| 33 if (!provider_) |
| 34 provider_ = new GamepadProvider(data_fetcher); |
| 35 DCHECK(num_readers_ > 0); |
| 36 provider_->Resume(); |
| 37 |
| 38 content::BrowserThread::PostTask( |
| 39 content::BrowserThread::UI, |
| 40 FROM_HERE, |
| 41 base::Bind(&GamepadService::RegisterForCloseNotification, |
| 42 base::Unretained(this), |
| 43 associated_rph)); |
| 44 } |
| 45 |
| 46 void GamepadService::RegisterForCloseNotification( |
| 47 content::RenderProcessHost* rph) { |
| 48 registrar_.Add(this, |
| 49 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 50 content::Source<content::RenderProcessHost>(rph)); |
| 51 } |
| 52 |
| 53 base::SharedMemoryHandle GamepadService::GetSharedMemoryHandle( |
| 54 base::ProcessHandle handle) { |
| 55 return provider_->GetRendererSharedMemoryHandle(handle); |
| 56 } |
| 57 |
| 58 void GamepadService::Stop() { |
| 59 --num_readers_; |
| 60 DCHECK(num_readers_ >= 0); |
| 61 |
| 62 if (num_readers_ == 0) |
| 63 provider_->Pause(); |
| 64 } |
| 65 |
| 66 void GamepadService::Observe(int type, |
| 67 const content::NotificationSource& source, |
| 68 const content::NotificationDetails& details) { |
| 69 DCHECK(type == content::NOTIFICATION_RENDERER_PROCESS_CLOSED); |
| 70 content::BrowserThread::PostTask( |
| 71 content::BrowserThread::IO, |
| 72 FROM_HERE, |
| 73 base::Bind(&GamepadService::Stop, base::Unretained(this))); |
| 74 } |
| 75 |
| 76 } // namespace content |
OLD | NEW |