1 // Copyright 2014 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/installer/util/firewall_manager_win.h" 6 7 #include "base/files/file_path.h" 8 #include "base/strings/string16.h" 9 #include "chrome/installer/util/advanced_firewall_manager_win.h" 10 #include "chrome/installer/util/browser_distribution.h" 11 #include "chrome/installer/util/install_util.h" 12 #include "chrome/installer/util/l10n_string_util.h" 13 #include "chrome/installer/util/legacy_firewall_manager_win.h" 14 15 #include "installer_util_strings.h" // NOLINT 16 17 namespace installer { 18 19 namespace { 20 21 const uint16 kDefaultMdnsPort = 5353; 22 23 class FirewallManagerAdvancedImpl : public FirewallManager { 24 public: 25 FirewallManagerAdvancedImpl() {} 26 virtual ~FirewallManagerAdvancedImpl() {} 27 28 bool Init(const base::string16& app_name, const base::FilePath& app_path) { 29 return manager_.Init(app_name, app_path); 30 } 31 32 // FirewallManager methods. 33 virtual bool CanUseLocalPorts() OVERRIDE { 34 return !manager_.IsFirewallEnabled() || manager_.HasAnyRule(); 35 }; 36 37 virtual bool AddFirewallRules() OVERRIDE { 38 return manager_.AddUDPRule(GetMdnsRuleName(), GetMdnsRuleDescription(), 39 kDefaultMdnsPort); 40 } 41 42 virtual void RemoveFirewallRules() OVERRIDE { 43 manager_.DeleteAllRules(); 44 } 45 46 private: 47 static base::string16 GetMdnsRuleName() { 48 #if defined(GOOGLE_CHROME_BUILD) 49 if (InstallUtil::IsChromeSxSProcess()) 50 return GetLocalizedString(IDS_INBOUND_MDNS_RULE_NAME_CANARY_BASE); 51 #endif 52 return GetLocalizedString(IDS_INBOUND_MDNS_RULE_NAME_BASE); 53 } 54 55 static base::string16 GetMdnsRuleDescription() { 56 #if defined(GOOGLE_CHROME_BUILD) 57 if (InstallUtil::IsChromeSxSProcess()) 58 return GetLocalizedString(IDS_INBOUND_MDNS_RULE_DESCRIPTION_CANARY_BASE); 59 #endif 60 return GetLocalizedString(IDS_INBOUND_MDNS_RULE_DESCRIPTION_BASE); 61 } 62 63 AdvancedFirewallManager manager_; 64 DISALLOW_COPY_AND_ASSIGN(FirewallManagerAdvancedImpl); 65 }; 66 67 class FirewallManagerLegacyImpl : public FirewallManager { 68 public: 69 FirewallManagerLegacyImpl() {} 70 virtual ~FirewallManagerLegacyImpl() {} 71 72 bool Init(const base::string16& app_name, const base::FilePath& app_path) { 73 return manager_.Init(app_name, app_path); 74 } 75 76 // FirewallManager methods. 77 virtual bool CanUseLocalPorts() OVERRIDE { 78 return !manager_.IsFirewallEnabled() || 79 manager_.GetAllowIncomingConnection(NULL); 80 }; 81 82 virtual bool AddFirewallRules() OVERRIDE { 83 // Change nothing if rule is set. 84 return manager_.GetAllowIncomingConnection(NULL) || 85 manager_.SetAllowIncomingConnection(true); 86 } 87 88 virtual void RemoveFirewallRules() OVERRIDE { 89 manager_.DeleteRule(); 90 } 91 92 private: 93 LegacyFirewallManager manager_; 94 DISALLOW_COPY_AND_ASSIGN(FirewallManagerLegacyImpl); 95 }; 96 97 } // namespace 98 99 FirewallManager::~FirewallManager() {} 100 101 // static 102 scoped_ptr<FirewallManager> FirewallManager::Create( 103 BrowserDistribution* dist, 104 const base::FilePath& chrome_path) { 105 // First try to connect to "Windows Firewall with Advanced Security" (Vista+). 106 scoped_ptr<FirewallManagerAdvancedImpl> manager( 107 new FirewallManagerAdvancedImpl()); 108 if (manager->Init(dist->GetDisplayName(), chrome_path)) 109 return manager.PassAs<FirewallManager>(); 110 111 // Next try to connect to "Windows Firewall for Windows XP with SP2". 112 scoped_ptr<FirewallManagerLegacyImpl> legacy_manager( 113 new FirewallManagerLegacyImpl()); 114 if (legacy_manager->Init(dist->GetDisplayName(), chrome_path)) 115 return legacy_manager.PassAs<FirewallManager>(); 116 117 return scoped_ptr<FirewallManager>(); 118 } 119 120 FirewallManager::FirewallManager() { 121 } 122 123 } // namespace installer 124