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

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

Issue 2754703005: [Device Discovery] Make DialRegistry a Singleton (Closed)
Patch Set: resolve code review comments from Mark 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.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 if (dial_registry_)
44 dial_registry_->StopPeriodicDiscovery();
imcheng 2017/03/23 23:06:00 Wouldn't that affect other observers of DialRegist
zhaobin 2017/03/24 00:42:35 Yes. It is fine for now since DialAPI is the only
imcheng 2017/03/24 00:54:24 Ok. Seems like the easiest way would be to have th
zhaobin 2017/03/24 20:00:14 Done.
45 }
54 46
55 DialRegistry* DialAPI::dial_registry() { 47 DialRegistry* DialAPI::dial_registry() {
56 DCHECK_CURRENTLY_ON(BrowserThread::IO); 48 DCHECK_CURRENTLY_ON(BrowserThread::IO);
57 if (!dial_registry_.get()) { 49 if (!dial_registry_) {
58 dial_registry_.reset(new DialRegistry( 50 dial_registry_ = media_router::DialRegistry::GetInstance();
59 TimeDelta::FromSeconds(kDialRefreshIntervalSecs),
60 TimeDelta::FromSeconds(kDialExpirationSecs), kDialMaxDevices));
61 dial_registry_->RegisterObserver(this); 51 dial_registry_->RegisterObserver(this);
62 if (test_device_data_) { 52 if (test_device_data_) {
63 dial_registry_->AddDeviceForTest(*test_device_data_); 53 dial_registry_->AddDeviceForTest(*test_device_data_);
64 } 54 }
65 } 55 }
66 return dial_registry_.get(); 56 return dial_registry_;
67 } 57 }
68 58
69 void DialAPI::OnListenerAdded(const EventListenerInfo& details) { 59 void DialAPI::OnListenerAdded(const EventListenerInfo& details) {
70 DCHECK_CURRENTLY_ON(BrowserThread::UI); 60 DCHECK_CURRENTLY_ON(BrowserThread::UI);
71 BrowserThread::PostTask( 61 BrowserThread::PostTask(
72 BrowserThread::IO, FROM_HERE, 62 BrowserThread::IO, FROM_HERE,
73 base::Bind(&DialAPI::NotifyListenerAddedOnIOThread, this)); 63 base::Bind(&DialAPI::NotifyListenerAddedOnIOThread, this));
74 } 64 }
75 65
76 void DialAPI::OnListenerRemoved(const EventListenerInfo& details) { 66 void DialAPI::OnListenerRemoved(const EventListenerInfo& details) {
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
266 void DialFetchDeviceDescriptionFunction::OnFetchError( 256 void DialFetchDeviceDescriptionFunction::OnFetchError(
267 const std::string& message) { 257 const std::string& message) {
268 // Destroy the DeviceDescriptionFetcher since it still contains a reference 258 // Destroy the DeviceDescriptionFetcher since it still contains a reference
269 // to |this| in its un-invoked callback. 259 // to |this| in its un-invoked callback.
270 device_description_fetcher_.reset(); 260 device_description_fetcher_.reset();
271 SetError(message); 261 SetError(message);
272 SendResponse(false); 262 SendResponse(false);
273 } 263 }
274 264
275 } // namespace extensions 265 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698