Roll src/third_party/WebKit 116cf7f:79abaa8 (svn 189234:189235)
[chromium-blink-merge.git] / chrome / installer / util / firewall_manager_win.cc
blob0171a6f2d07ac0893be2b59a893197bbb4a5de05
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/l10n_string_util.h"
13 #include "chrome/installer/util/legacy_firewall_manager_win.h"
15 #include "installer_util_strings.h" // NOLINT
17 namespace installer {
19 namespace {
21 const uint16 kDefaultMdnsPort = 5353;
23 class FirewallManagerAdvancedImpl : public FirewallManager {
24 public:
25 FirewallManagerAdvancedImpl() {}
26 virtual ~FirewallManagerAdvancedImpl() {}
28 bool Init(const base::string16& app_name, const base::FilePath& app_path) {
29 return manager_.Init(app_name, app_path);
32 // FirewallManager methods.
33 virtual bool CanUseLocalPorts() override {
34 return !manager_.IsFirewallEnabled() || manager_.HasAnyRule();
37 virtual bool AddFirewallRules() override {
38 return manager_.AddUDPRule(GetMdnsRuleName(), GetMdnsRuleDescription(),
39 kDefaultMdnsPort);
42 virtual void RemoveFirewallRules() override {
43 manager_.DeleteAllRules();
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);
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);
63 AdvancedFirewallManager manager_;
64 DISALLOW_COPY_AND_ASSIGN(FirewallManagerAdvancedImpl);
67 class FirewallManagerLegacyImpl : public FirewallManager {
68 public:
69 FirewallManagerLegacyImpl() {}
70 virtual ~FirewallManagerLegacyImpl() {}
72 bool Init(const base::string16& app_name, const base::FilePath& app_path) {
73 return manager_.Init(app_name, app_path);
76 // FirewallManager methods.
77 virtual bool CanUseLocalPorts() override {
78 return !manager_.IsFirewallEnabled() ||
79 manager_.GetAllowIncomingConnection(NULL);
82 virtual bool AddFirewallRules() override {
83 // Change nothing if rule is set.
84 return manager_.GetAllowIncomingConnection(NULL) ||
85 manager_.SetAllowIncomingConnection(true);
88 virtual void RemoveFirewallRules() override {
89 manager_.DeleteRule();
92 private:
93 LegacyFirewallManager manager_;
94 DISALLOW_COPY_AND_ASSIGN(FirewallManagerLegacyImpl);
97 } // namespace
99 FirewallManager::~FirewallManager() {}
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.Pass();
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.Pass();
117 return scoped_ptr<FirewallManager>();
120 FirewallManager::FirewallManager() {
123 } // namespace installer