Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / installer / util / firewall_manager_win.cc
blobdf756a591da6f88f2db9f788f76ddee03d946a82
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.
5 #include "chrome/installer/util/firewall_manager_win.h"
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/installer_util_strings.h"
13 #include "chrome/installer/util/l10n_string_util.h"
14 #include "chrome/installer/util/legacy_firewall_manager_win.h"
16 namespace installer {
18 namespace {
20 const uint16 kDefaultMdnsPort = 5353;
22 class FirewallManagerAdvancedImpl : public FirewallManager {
23 public:
24 FirewallManagerAdvancedImpl() {}
25 ~FirewallManagerAdvancedImpl() override {}
27 bool Init(const base::string16& app_name, const base::FilePath& app_path) {
28 return manager_.Init(app_name, app_path);
31 // FirewallManager methods.
32 bool CanUseLocalPorts() override {
33 return !manager_.IsFirewallEnabled() || manager_.HasAnyRule();
36 bool AddFirewallRules() override {
37 return manager_.AddUDPRule(GetMdnsRuleName(), GetMdnsRuleDescription(),
38 kDefaultMdnsPort);
41 void RemoveFirewallRules() override {
42 manager_.DeleteAllRules();
45 private:
46 static base::string16 GetMdnsRuleName() {
47 #if defined(GOOGLE_CHROME_BUILD)
48 if (InstallUtil::IsChromeSxSProcess())
49 return GetLocalizedString(IDS_INBOUND_MDNS_RULE_NAME_CANARY_BASE);
50 #endif
51 return GetLocalizedString(IDS_INBOUND_MDNS_RULE_NAME_BASE);
54 static base::string16 GetMdnsRuleDescription() {
55 #if defined(GOOGLE_CHROME_BUILD)
56 if (InstallUtil::IsChromeSxSProcess())
57 return GetLocalizedString(IDS_INBOUND_MDNS_RULE_DESCRIPTION_CANARY_BASE);
58 #endif
59 return GetLocalizedString(IDS_INBOUND_MDNS_RULE_DESCRIPTION_BASE);
62 AdvancedFirewallManager manager_;
63 DISALLOW_COPY_AND_ASSIGN(FirewallManagerAdvancedImpl);
66 class FirewallManagerLegacyImpl : public FirewallManager {
67 public:
68 FirewallManagerLegacyImpl() {}
69 ~FirewallManagerLegacyImpl() override {}
71 bool Init(const base::string16& app_name, const base::FilePath& app_path) {
72 return manager_.Init(app_name, app_path);
75 // FirewallManager methods.
76 bool CanUseLocalPorts() override {
77 return !manager_.IsFirewallEnabled() ||
78 manager_.GetAllowIncomingConnection(NULL);
81 bool AddFirewallRules() override {
82 // Change nothing if rule is set.
83 return manager_.GetAllowIncomingConnection(NULL) ||
84 manager_.SetAllowIncomingConnection(true);
87 void RemoveFirewallRules() override {
88 manager_.DeleteRule();
91 private:
92 LegacyFirewallManager manager_;
93 DISALLOW_COPY_AND_ASSIGN(FirewallManagerLegacyImpl);
96 } // namespace
98 FirewallManager::~FirewallManager() {}
100 // static
101 scoped_ptr<FirewallManager> FirewallManager::Create(
102 BrowserDistribution* dist,
103 const base::FilePath& chrome_path) {
104 // First try to connect to "Windows Firewall with Advanced Security" (Vista+).
105 scoped_ptr<FirewallManagerAdvancedImpl> manager(
106 new FirewallManagerAdvancedImpl());
107 if (manager->Init(dist->GetDisplayName(), chrome_path))
108 return manager.Pass();
110 // Next try to connect to "Windows Firewall for Windows XP with SP2".
111 scoped_ptr<FirewallManagerLegacyImpl> legacy_manager(
112 new FirewallManagerLegacyImpl());
113 if (legacy_manager->Init(dist->GetDisplayName(), chrome_path))
114 return legacy_manager.Pass();
116 return scoped_ptr<FirewallManager>();
119 FirewallManager::FirewallManager() {
122 } // namespace installer