1 // Copyright 2013 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.
5 #include "chrome/browser/local_discovery/service_discovery_shared_client.h"
7 #include "content/public/browser/browser_thread.h"
10 #include "base/files/file_path.h"
11 #include "base/metrics/histogram.h"
12 #include "base/path_service.h"
13 #include "base/timer/elapsed_timer.h"
14 #include "chrome/installer/util/browser_distribution.h"
15 #include "chrome/installer/util/firewall_manager_win.h"
18 #if defined(OS_MACOSX)
19 #include "chrome/browser/local_discovery/service_discovery_client_mac_factory.h"
22 #if defined(ENABLE_MDNS)
23 #include "chrome/browser/local_discovery/service_discovery_client_mdns.h"
24 #include "chrome/browser/local_discovery/service_discovery_client_utility.h"
31 bool g_is_firewall_ready
= false;
32 bool g_is_firewall_state_reported
= false;
34 void ReportFirewallStats() {
35 if (g_is_firewall_state_reported
)
37 g_is_firewall_state_reported
= true;
38 base::FilePath exe_path
;
39 if (!PathService::Get(base::FILE_EXE
, &exe_path
))
41 base::ElapsedTimer timer
;
42 scoped_ptr
<installer::FirewallManager
> manager
=
43 installer::FirewallManager::Create(BrowserDistribution::GetDistribution(),
47 g_is_firewall_ready
= manager
->CanUseLocalPorts();
48 UMA_HISTOGRAM_TIMES("LocalDiscovery.FirewallAccessTime", timer
.Elapsed());
49 UMA_HISTOGRAM_BOOLEAN("LocalDiscovery.IsFirewallReady", g_is_firewall_ready
);
56 namespace local_discovery
{
58 using content::BrowserThread
;
61 ServiceDiscoverySharedClient
* g_service_discovery_client
= NULL
;
64 ServiceDiscoverySharedClient::ServiceDiscoverySharedClient() {
65 DCHECK(!g_service_discovery_client
);
66 g_service_discovery_client
= this;
69 ServiceDiscoverySharedClient::~ServiceDiscoverySharedClient() {
70 DCHECK_EQ(g_service_discovery_client
, this);
71 g_service_discovery_client
= NULL
;
74 #if defined(ENABLE_MDNS) || defined(OS_MACOSX)
76 scoped_refptr
<ServiceDiscoverySharedClient
>
77 ServiceDiscoverySharedClient::GetInstance() {
78 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
80 if (g_service_discovery_client
)
81 return g_service_discovery_client
;
84 if (!g_is_firewall_state_reported
) {
85 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE
,
86 base::Bind(&ReportFirewallStats
));
90 #if defined(OS_MACOSX)
91 return ServiceDiscoveryClientMacFactory::CreateInstance();
94 return new ServiceDiscoveryClientMdns();
99 void ServiceDiscoverySharedClient::GetInstanceWithoutAlert(
100 const GetInstanceCallback
& callback
) {
103 scoped_refptr
<ServiceDiscoverySharedClient
> result
= GetInstance();
104 return callback
.Run(result
);
107 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
108 // TODO(vitalybuka): Switch to |ServiceDiscoveryClientMdns| after we find what
109 // to do with firewall for user-level installs. crbug.com/366408
110 scoped_refptr
<ServiceDiscoverySharedClient
> result
=
111 g_service_discovery_client
;
113 return callback
.Run(result
);
115 if (!g_is_firewall_state_reported
) {
116 BrowserThread::PostTaskAndReply(
119 base::Bind(&ReportFirewallStats
),
120 base::Bind(&ServiceDiscoverySharedClient::GetInstanceWithoutAlert
,
126 g_is_firewall_ready
? GetInstance() : new ServiceDiscoveryClientUtility();
127 callback
.Run(result
);
133 scoped_refptr
<ServiceDiscoverySharedClient
>
134 ServiceDiscoverySharedClient::GetInstance() {
135 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
140 #endif // ENABLE_MDNS
142 } // namespace local_discovery