1 // Copyright 2015 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 #ifndef EXTENSIONS_BROWSER_API_SOCKET_APP_FIREWALL_HOLE_MANAGER_H_
6 #define EXTENSIONS_BROWSER_API_SOCKET_APP_FIREWALL_HOLE_MANAGER_H_
10 #include "base/scoped_observer.h"
11 #include "chromeos/network/firewall_hole.h"
12 #include "extensions/browser/app_window/app_window_registry.h"
18 namespace extensions
{
20 class AppFirewallHoleManager
;
22 // Represents an open port in the system firewall that will be opened and closed
23 // automatically when the application has a visible window or not. The hole is
24 // closed on destruction.
25 class AppFirewallHole
{
27 typedef chromeos::FirewallHole::PortType PortType
;
31 PortType
type() const { return type_
; }
32 uint16_t port() const { return port_
; }
33 const std::string
& extension_id() const { return extension_id_
; }
36 friend class AppFirewallHoleManager
;
38 AppFirewallHole(AppFirewallHoleManager
* manager
,
41 const std::string
& extension_id
);
43 void SetVisible(bool app_visible
);
44 void OnFirewallHoleOpened(scoped_ptr
<chromeos::FirewallHole
> firewall_hole
);
48 std::string extension_id_
;
49 bool app_visible_
= false;
51 // This object is destroyed when the AppFirewallHoleManager that owns it is
52 // destroyed and so a raw pointer is okay here.
53 AppFirewallHoleManager
* manager_
;
55 // This will hold the FirewallHole object if one is opened.
56 scoped_ptr
<chromeos::FirewallHole
> firewall_hole_
;
58 base::WeakPtrFactory
<AppFirewallHole
> weak_factory_
;
61 // Tracks ports in the system firewall opened by an application so that they
62 // may be automatically opened and closed only when the application has a
64 class AppFirewallHoleManager
: public KeyedService
,
65 public AppWindowRegistry::Observer
{
67 explicit AppFirewallHoleManager(content::BrowserContext
* context
);
68 ~AppFirewallHoleManager() override
;
70 // Returns the instance for a given browser context, or NULL if none.
71 static AppFirewallHoleManager
* Get(content::BrowserContext
* context
);
73 // Takes ownership of the AppFirewallHole and will open a port on the system
74 // firewall if the associated application is currently visible.
75 scoped_ptr
<AppFirewallHole
> Open(AppFirewallHole::PortType type
,
77 const std::string
& extension_id
);
80 friend class AppFirewallHole
;
82 void Close(AppFirewallHole
* hole
);
84 // AppWindowRegistry::Observer
85 void OnAppWindowRemoved(AppWindow
* app_window
) override
;
86 void OnAppWindowHidden(AppWindow
* app_window
) override
;
87 void OnAppWindowShown(AppWindow
* app_window
, bool was_hidden
) override
;
89 content::BrowserContext
* context_
;
90 ScopedObserver
<AppWindowRegistry
, AppWindowRegistry::Observer
> observer_
;
91 std::multimap
<std::string
, AppFirewallHole
*> tracked_holes_
;
94 } // namespace extensions
96 #endif // EXTENSIONS_BROWSER_API_SOCKET_APP_FIREWALL_HOLE_MANAGER_H_