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;
33 void ReportFirewallStats() {
34 base::FilePath exe_path
;
35 if (!PathService::Get(base::FILE_EXE
, &exe_path
))
37 base::ElapsedTimer timer
;
38 scoped_ptr
<installer::FirewallManager
> manager
=
39 installer::FirewallManager::Create(BrowserDistribution::GetDistribution(),
43 g_is_firewall_ready
= manager
->CanUseLocalPorts();
44 UMA_HISTOGRAM_TIMES("LocalDiscovery.FirewallAccessTime", timer
.Elapsed());
45 UMA_HISTOGRAM_BOOLEAN("LocalDiscovery.IsFirewallReady", g_is_firewall_ready
);
52 namespace local_discovery
{
54 using content::BrowserThread
;
57 ServiceDiscoverySharedClient
* g_service_discovery_client
= NULL
;
60 ServiceDiscoverySharedClient::ServiceDiscoverySharedClient() {
61 DCHECK(!g_service_discovery_client
);
62 g_service_discovery_client
= this;
65 ServiceDiscoverySharedClient::~ServiceDiscoverySharedClient() {
66 DCHECK_EQ(g_service_discovery_client
, this);
67 g_service_discovery_client
= NULL
;
70 #if defined(ENABLE_MDNS) || defined(OS_MACOSX)
72 scoped_refptr
<ServiceDiscoverySharedClient
>
73 ServiceDiscoverySharedClient::GetInstance() {
74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
76 if (g_service_discovery_client
)
77 return g_service_discovery_client
;
79 #if defined(OS_MACOSX)
80 return ServiceDiscoveryClientMacFactory::CreateInstance();
84 static bool reported
=
85 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE
,
86 base::Bind(&ReportFirewallStats
));
87 // TODO(vitalybuka): Switch to |ServiceDiscoveryClientMdns| after we find what
88 // to do with firewall for user-level installs. crbug.com/366408
89 if (!g_is_firewall_ready
)
90 return new ServiceDiscoveryClientUtility();
93 return new ServiceDiscoveryClientMdns();
99 scoped_refptr
<ServiceDiscoverySharedClient
>
100 ServiceDiscoverySharedClient::GetInstance() {
101 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
106 #endif // ENABLE_MDNS
108 } // namespace local_discovery