Home | History | Annotate | Download | only in local_discovery
      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.
      4 
      5 #include "chrome/browser/local_discovery/service_discovery_shared_client.h"
      6 
      7 #include "content/public/browser/browser_thread.h"
      8 
      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
     17 
     18 #if defined(OS_MACOSX)
     19 #include "chrome/browser/local_discovery/service_discovery_client_mac_factory.h"
     20 #endif
     21 
     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
     26 
     27 namespace {
     28 
     29 #if defined(OS_WIN)
     30 
     31 bool g_is_firewall_ready = false;
     32 bool g_is_firewall_state_reported = false;
     33 
     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);
     50 }
     51 #endif  // OS_WIN
     52 
     53 }  // namespace
     54 
     55 
     56 namespace local_discovery {
     57 
     58 using content::BrowserThread;
     59 
     60 namespace {
     61 ServiceDiscoverySharedClient* g_service_discovery_client = NULL;
     62 }  // namespace
     63 
     64 ServiceDiscoverySharedClient::ServiceDiscoverySharedClient() {
     65   DCHECK(!g_service_discovery_client);
     66   g_service_discovery_client = this;
     67 }
     68 
     69 ServiceDiscoverySharedClient::~ServiceDiscoverySharedClient() {
     70   DCHECK_EQ(g_service_discovery_client, this);
     71   g_service_discovery_client = NULL;
     72 }
     73 
     74 #if defined(ENABLE_MDNS) || defined(OS_MACOSX)
     75 
     76 scoped_refptr<ServiceDiscoverySharedClient>
     77     ServiceDiscoverySharedClient::GetInstance() {
     78   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     79 
     80   if (g_service_discovery_client)
     81     return g_service_discovery_client;
     82 
     83 #if defined(OS_MACOSX)
     84   return ServiceDiscoveryClientMacFactory::CreateInstance();
     85 #else  // OS_MACOSX
     86 
     87   return new ServiceDiscoveryClientMdns();
     88 #endif  // OS_MACOSX
     89 }
     90 
     91 // static
     92 void ServiceDiscoverySharedClient::GetInstanceWithoutAlert(
     93     const GetInstanceCallback& callback) {
     94 #if !defined(OS_WIN)
     95 
     96   scoped_refptr<ServiceDiscoverySharedClient> result = GetInstance();
     97   return callback.Run(result);
     98 
     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)
    106     return callback.Run(result);
    107 
    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;
    116   }
    117 
    118   result =
    119       g_is_firewall_ready ? GetInstance() : new ServiceDiscoveryClientUtility();
    120   callback.Run(result);
    121 #endif  // OS_WIN
    122 }
    123 
    124 #else
    125 
    126 scoped_refptr<ServiceDiscoverySharedClient>
    127     ServiceDiscoverySharedClient::GetInstance() {
    128   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
    129   NOTIMPLEMENTED();
    130   return NULL;
    131 }
    132 
    133 #endif  // ENABLE_MDNS
    134 
    135 }  // namespace local_discovery
    136