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

Side by Side Diff: chrome/browser/extensions/api/dial/dial_api.cc

Issue 2754703005: [Device Discovery] Make DialRegistry a Singleton (Closed)
Patch Set: Created 3 years, 9 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
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/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_factory.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) {
49 EventRouter::Get(profile)->RegisterObserver( 37 EventRouter::Get(profile)->RegisterObserver(
50 this, api::dial::OnDeviceList::kEventName); 38 this, api::dial::OnDeviceList::kEventName);
51 } 39 }
52 40
53 DialAPI::~DialAPI() {} 41 DialAPI::~DialAPI() {}
54 42
55 DialRegistry* DialAPI::dial_registry() { 43 DialRegistry* DialAPI::dial_registry() {
56 DCHECK_CURRENTLY_ON(BrowserThread::IO); 44 DCHECK_CURRENTLY_ON(BrowserThread::IO);
57 if (!dial_registry_.get()) { 45 if (!dial_registry_) {
58 dial_registry_.reset(new DialRegistry( 46 dial_registry_ =
59 TimeDelta::FromSeconds(kDialRefreshIntervalSecs), 47 media_router::DialRegistryFactory::GetInstance()->GetForBrowserContext(
60 TimeDelta::FromSeconds(kDialExpirationSecs), kDialMaxDevices)); 48 profile_);
61 dial_registry_->RegisterObserver(this); 49 dial_registry_->RegisterObserver(this);
62 if (test_device_data_) { 50 if (test_device_data_) {
63 dial_registry_->AddDeviceForTest(*test_device_data_); 51 dial_registry_->AddDeviceForTest(*test_device_data_);
64 } 52 }
65 } 53 }
66 return dial_registry_.get(); 54 return dial_registry_.get();
67 } 55 }
68 56
69 void DialAPI::OnListenerAdded(const EventListenerInfo& details) { 57 void DialAPI::OnListenerAdded(const EventListenerInfo& details) {
70 DCHECK_CURRENTLY_ON(BrowserThread::UI); 58 DCHECK_CURRENTLY_ON(BrowserThread::UI);
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
266 void DialFetchDeviceDescriptionFunction::OnFetchError( 254 void DialFetchDeviceDescriptionFunction::OnFetchError(
267 const std::string& message) { 255 const std::string& message) {
268 // Destroy the DeviceDescriptionFetcher since it still contains a reference 256 // Destroy the DeviceDescriptionFetcher since it still contains a reference
269 // to |this| in its un-invoked callback. 257 // to |this| in its un-invoked callback.
270 device_description_fetcher_.reset(); 258 device_description_fetcher_.reset();
271 SetError(message); 259 SetError(message);
272 SendResponse(false); 260 SendResponse(false);
273 } 261 }
274 262
275 } // namespace extensions 263 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698