Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / chrome / browser / local_discovery / service_discovery_shared_client.cc
blob121de207b5af4c2fd3854fa4680585191d4a5a19
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_CURRENTLY_ON(BrowserThread::UI);
80 if (g_service_discovery_client)
81 return g_service_discovery_client;
83 #if defined(OS_WIN)
84 if (!g_is_firewall_state_reported) {
85 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
86 base::Bind(&ReportFirewallStats));
88 #endif // OS_WIN
90 #if defined(OS_MACOSX)
91 return ServiceDiscoveryClientMacFactory::CreateInstance();
92 #else // OS_MACOSX
94 return new ServiceDiscoveryClientMdns();
95 #endif // OS_MACOSX
98 // static
99 void ServiceDiscoverySharedClient::GetInstanceWithoutAlert(
100 const GetInstanceCallback& callback) {
101 #if !defined(OS_WIN)
103 scoped_refptr<ServiceDiscoverySharedClient> result = GetInstance();
104 return callback.Run(result);
106 #else // OS_WIN
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;
112 if (result.get())
113 return callback.Run(result);
115 if (!g_is_firewall_state_reported) {
116 BrowserThread::PostTaskAndReply(
117 BrowserThread::FILE,
118 FROM_HERE,
119 base::Bind(&ReportFirewallStats),
120 base::Bind(&ServiceDiscoverySharedClient::GetInstanceWithoutAlert,
121 callback));
122 return;
125 result =
126 g_is_firewall_ready ? GetInstance() : new ServiceDiscoveryClientUtility();
127 callback.Run(result);
128 #endif // OS_WIN
131 #else
133 scoped_refptr<ServiceDiscoverySharedClient>
134 ServiceDiscoverySharedClient::GetInstance() {
135 DCHECK_CURRENTLY_ON(BrowserThread::UI);
136 NOTIMPLEMENTED();
137 return NULL;
140 #endif // ENABLE_MDNS
142 } // namespace local_discovery