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/browser/local_discovery/service_discovery_client_utility.h"
15 #include "chrome/installer/util/browser_distribution.h"
16 #include "chrome/installer/util/firewall_manager_win.h"
19 #if defined(OS_MACOSX)
20 #include "chrome/browser/local_discovery/service_discovery_client_mac_factory.h"
23 #if defined(ENABLE_MDNS)
24 #include "chrome/browser/local_discovery/service_discovery_client_mdns.h"
30 bool IsFirewallReady() {
31 base::FilePath exe_path
;
32 if (!PathService::Get(base::FILE_EXE
, &exe_path
))
34 base::ElapsedTimer timer
;
35 scoped_ptr
<installer::FirewallManager
> manager
=
36 installer::FirewallManager::Create(BrowserDistribution::GetDistribution(),
40 bool is_ready
= manager
->CanUseLocalPorts();
41 UMA_HISTOGRAM_TIMES("LocalDiscovery.FirewallAccessTime", timer
.Elapsed());
42 UMA_HISTOGRAM_BOOLEAN("LocalDiscovery.IsFirewallReady", is_ready
);
50 namespace local_discovery
{
52 using content::BrowserThread
;
55 ServiceDiscoverySharedClient
* g_service_discovery_client
= NULL
;
58 ServiceDiscoverySharedClient::ServiceDiscoverySharedClient() {
59 DCHECK(!g_service_discovery_client
);
60 g_service_discovery_client
= this;
63 ServiceDiscoverySharedClient::~ServiceDiscoverySharedClient() {
64 DCHECK_EQ(g_service_discovery_client
, this);
65 g_service_discovery_client
= NULL
;
68 #if defined(ENABLE_MDNS) || defined(OS_MACOSX)
70 scoped_refptr
<ServiceDiscoverySharedClient
>
71 ServiceDiscoverySharedClient::GetInstance() {
72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
74 if (g_service_discovery_client
)
75 return g_service_discovery_client
;
77 #if defined(OS_MACOSX)
78 return ServiceDiscoveryClientMacFactory::CreateInstance();
82 static bool is_firewall_ready
= IsFirewallReady();
83 if (!is_firewall_ready
) {
84 // TODO(vitalybuka): Remove after we find what to do with firewall for
85 // user-level installs. crbug.com/366408
86 return new ServiceDiscoveryClientUtility();
89 return new ServiceDiscoveryClientMdns();
95 scoped_refptr
<ServiceDiscoverySharedClient
>
96 ServiceDiscoverySharedClient::GetInstance() {
97 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
102 #endif // ENABLE_MDNS
104 } // namespace local_discovery