| 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/dial/dial_api.h" | 5 #include "chrome/browser/extensions/api/dial/dial_api.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "chrome/browser/extensions/api/dial/dial_api_factory.h" | 14 #include "chrome/browser/extensions/api/dial/dial_api_factory.h" |
| 15 #include "chrome/browser/media/router/discovery/dial/device_description_fetcher.
h" | 15 #include "chrome/browser/media/router/discovery/dial/device_description_fetcher.
h" |
| 16 #include "chrome/browser/media/router/discovery/dial/dial_registry.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/common/extensions/api/dial.h" | 18 #include "chrome/common/extensions/api/dial.h" |
| 18 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 19 #include "extensions/browser/event_router.h" | 20 #include "extensions/browser/event_router.h" |
| 20 #include "extensions/browser/extension_system.h" | 21 #include "extensions/browser/extension_system.h" |
| 21 #include "url/gurl.h" | 22 #include "url/gurl.h" |
| 22 | 23 |
| 23 using base::TimeDelta; | 24 using base::TimeDelta; |
| 24 using content::BrowserThread; | 25 using content::BrowserThread; |
| 25 using media_router::DeviceDescriptionFetcher; | 26 using media_router::DeviceDescriptionFetcher; |
| 26 using media_router::DialDeviceData; | 27 using media_router::DialDeviceData; |
| 27 using media_router::DialDeviceDescriptionData; | 28 using media_router::DialDeviceDescriptionData; |
| 28 using media_router::DialRegistry; | 29 using media_router::DialRegistry; |
| 29 | 30 |
| 30 namespace extensions { | 31 namespace extensions { |
| 31 | 32 |
| 32 namespace { | |
| 33 | |
| 34 // How often to poll for devices. | |
| 35 const int kDialRefreshIntervalSecs = 120; | |
| 36 | |
| 37 // We prune a device if it does not respond after this time. | |
| 38 const int kDialExpirationSecs = 240; | |
| 39 | |
| 40 // The maximum number of devices retained at once in the registry. | |
| 41 const size_t kDialMaxDevices = 256; | |
| 42 | |
| 43 } // namespace | |
| 44 | |
| 45 DialAPI::DialAPI(Profile* profile) | 33 DialAPI::DialAPI(Profile* profile) |
| 46 : RefcountedKeyedService( | 34 : RefcountedKeyedService( |
| 47 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO)), | 35 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO)), |
| 48 profile_(profile) { | 36 profile_(profile), |
| 37 dial_registry_(nullptr) { |
| 49 EventRouter::Get(profile)->RegisterObserver( | 38 EventRouter::Get(profile)->RegisterObserver( |
| 50 this, api::dial::OnDeviceList::kEventName); | 39 this, api::dial::OnDeviceList::kEventName); |
| 51 } | 40 } |
| 52 | 41 |
| 53 DialAPI::~DialAPI() {} | 42 DialAPI::~DialAPI() { |
| 43 // TODO(zhaobin): Call dial_registry_->UnregisterObserver() instead. In |
| 44 // current implementation, UnregistryObserver() does not StopDiscovery() and |
| 45 // causes crash in ~DialRegistry(). May keep a listener count and |
| 46 // Register/UnregisterObserver as needed. |
| 47 if (dial_registry_) |
| 48 dial_registry_->StopPeriodicDiscovery(); |
| 49 } |
| 54 | 50 |
| 55 DialRegistry* DialAPI::dial_registry() { | 51 DialRegistry* DialAPI::dial_registry() { |
| 56 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 52 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 57 if (!dial_registry_.get()) { | 53 if (!dial_registry_) { |
| 58 dial_registry_.reset(new DialRegistry( | 54 dial_registry_ = media_router::DialRegistry::GetInstance(); |
| 59 TimeDelta::FromSeconds(kDialRefreshIntervalSecs), | |
| 60 TimeDelta::FromSeconds(kDialExpirationSecs), kDialMaxDevices)); | |
| 61 dial_registry_->RegisterObserver(this); | 55 dial_registry_->RegisterObserver(this); |
| 62 if (test_device_data_) { | 56 if (test_device_data_) { |
| 63 dial_registry_->AddDeviceForTest(*test_device_data_); | 57 dial_registry_->AddDeviceForTest(*test_device_data_); |
| 64 } | 58 } |
| 65 } | 59 } |
| 66 return dial_registry_.get(); | 60 return dial_registry_; |
| 67 } | 61 } |
| 68 | 62 |
| 69 void DialAPI::OnListenerAdded(const EventListenerInfo& details) { | 63 void DialAPI::OnListenerAdded(const EventListenerInfo& details) { |
| 70 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 64 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 71 BrowserThread::PostTask( | 65 BrowserThread::PostTask( |
| 72 BrowserThread::IO, FROM_HERE, | 66 BrowserThread::IO, FROM_HERE, |
| 73 base::Bind(&DialAPI::NotifyListenerAddedOnIOThread, this)); | 67 base::Bind(&DialAPI::NotifyListenerAddedOnIOThread, this)); |
| 74 } | 68 } |
| 75 | 69 |
| 76 void DialAPI::OnListenerRemoved(const EventListenerInfo& details) { | 70 void DialAPI::OnListenerRemoved(const EventListenerInfo& details) { |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 void DialFetchDeviceDescriptionFunction::OnFetchError( | 260 void DialFetchDeviceDescriptionFunction::OnFetchError( |
| 267 const std::string& message) { | 261 const std::string& message) { |
| 268 // Destroy the DeviceDescriptionFetcher since it still contains a reference | 262 // Destroy the DeviceDescriptionFetcher since it still contains a reference |
| 269 // to |this| in its un-invoked callback. | 263 // to |this| in its un-invoked callback. |
| 270 device_description_fetcher_.reset(); | 264 device_description_fetcher_.reset(); |
| 271 SetError(message); | 265 SetError(message); |
| 272 SendResponse(false); | 266 SendResponse(false); |
| 273 } | 267 } |
| 274 | 268 |
| 275 } // namespace extensions | 269 } // namespace extensions |
| OLD | NEW |