Roll src/third_party/WebKit 8b42d1d:744641d (svn 186770:186771)
[chromium-blink-merge.git] / chrome / browser / local_discovery / service_discovery_shared_client.cc
blob0f615523556a2067b93aa66381f89b4b9c12b47b
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"
9 #if defined(OS_WIN)
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"
16 #endif // OS_WIN
18 #if defined(OS_MACOSX)
19 #include "chrome/browser/local_discovery/service_discovery_client_mac_factory.h"
20 #endif
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"
25 #endif // ENABLE_MDNS
27 namespace {
29 #if defined(OS_WIN)
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)
36 return;
37 g_is_firewall_state_reported = true;
38 base::FilePath exe_path;
39 if (!PathService::Get(base::FILE_EXE, &exe_path))
40 return;
41 base::ElapsedTimer timer;
42 scoped_ptr<installer::FirewallManager> manager =
43 installer::FirewallManager::Create(BrowserDistribution::GetDistribution(),
44 exe_path);
45 if (!manager)
46 return;
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);
51 #endif // OS_WIN
53 } // namespace
56 namespace local_discovery {
58 using content::BrowserThread;
60 namespace {
61 ServiceDiscoverySharedClient* g_service_discovery_client = NULL;
62 } // namespace
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(BrowserThread::CurrentlyOn(BrowserThread::UI));
80 if (g_service_discovery_client)
81 return g_service_discovery_client;
83 #if defined(OS_MACOSX)
84 return ServiceDiscoveryClientMacFactory::CreateInstance();
85 #else // OS_MACOSX
87 return new ServiceDiscoveryClientMdns();
88 #endif // OS_MACOSX
91 // static
92 void ServiceDiscoverySharedClient::GetInstanceWithoutAlert(
93 const GetInstanceCallback& callback) {
94 #if !defined(OS_WIN)
96 scoped_refptr<ServiceDiscoverySharedClient> result = GetInstance();
97 return callback.Run(result);
99 #else // OS_WIN
100 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
101 // TODO(vitalybuka): Switch to |ServiceDiscoveryClientMdns| after we find what
102 // to do with firewall for user-level installs. crbug.com/366408
103 scoped_refptr<ServiceDiscoverySharedClient> result =
104 g_service_discovery_client;
105 if (result.get())
106 return callback.Run(result);
108 if (!g_is_firewall_state_reported) {
109 BrowserThread::PostTaskAndReply(
110 BrowserThread::FILE,
111 FROM_HERE,
112 base::Bind(&ReportFirewallStats),
113 base::Bind(&ServiceDiscoverySharedClient::GetInstanceWithoutAlert,
114 callback));
115 return;
118 result =
119 g_is_firewall_ready ? GetInstance() : new ServiceDiscoveryClientUtility();
120 callback.Run(result);
121 #endif // OS_WIN
124 #else
126 scoped_refptr<ServiceDiscoverySharedClient>
127 ServiceDiscoverySharedClient::GetInstance() {
128 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
129 NOTIMPLEMENTED();
130 return NULL;
133 #endif // ENABLE_MDNS
135 } // namespace local_discovery