Add ICU message format support
[chromium-blink-merge.git] / extensions / browser / api / networking_config / networking_config_service.h
blob41cc15a94b937a26ced9cf393edee53736c00ce6
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_NETWORKING_CONFIG_NETWORKING_CONFIG_SERVICE_H_
6 #define EXTENSIONS_BROWSER_API_NETWORKING_CONFIG_NETWORKING_CONFIG_SERVICE_H_
8 #include <map>
9 #include <string>
11 #include "base/macros.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/scoped_observer.h"
15 #include "base/values.h"
16 #include "components/keyed_service/core/keyed_service.h"
17 #include "content/public/browser/browser_context.h"
18 #include "extensions/browser/event_router.h"
19 #include "extensions/browser/extension_registry.h"
20 #include "extensions/browser/extension_registry_observer.h"
22 namespace extensions {
24 // This class provides the session-scoped storage backing the networking config
25 // extension API. Currently only the parts relevant for captive portal handling
26 // are implemented.
27 class NetworkingConfigService : public ExtensionRegistryObserver,
28 public KeyedService {
29 public:
30 class EventDelegate {
31 public:
32 EventDelegate() {}
33 virtual ~EventDelegate() {}
34 virtual bool HasExtensionRegisteredForEvent(
35 const std::string& extension_id) const = 0;
37 private:
38 DISALLOW_COPY_AND_ASSIGN(EventDelegate);
41 // Indicates the authentication state of the portal.
42 enum AuthenticationState { NOTRY, TRYING, SUCCESS, REJECTED, FAILED };
44 // Provides information about the current authentication state of the portal.
45 struct AuthenticationResult {
46 AuthenticationResult();
47 AuthenticationResult(ExtensionId extension_id,
48 std::string guid,
49 AuthenticationState authentication_state);
50 ExtensionId extension_id;
51 std::string guid;
52 AuthenticationState authentication_state;
55 // Note: |extension_registry| must outlive this class.
56 NetworkingConfigService(content::BrowserContext* browser_context,
57 scoped_ptr<EventDelegate> event_delegate,
58 ExtensionRegistry* extension_registry);
59 ~NetworkingConfigService() override;
61 // ExtensionRegistryObserver
62 void OnExtensionUnloaded(content::BrowserContext* browser_context,
63 const Extension* extension,
64 UnloadedExtensionInfo::Reason reason) override;
66 // Returns the extension id registered for |hex_ssid|. If no extension is
67 // registered for this |hex_ssid|, the function returns an empty string.
68 // |hex_ssid|: SSID in hex encoding.
69 std::string LookupExtensionIdForHexSsid(std::string hex_ssid) const;
71 // Returns true if the extension with id |extension_id| registered for
72 // |onCaptivePortalDetected| events, otherwise false.
73 bool IsRegisteredForCaptivePortalEvent(std::string extension_id) const;
75 // Registers |hex_ssid| as being handled by the extension with extension ID
76 // |extension_id|. Returns true on success and false if another extension
77 // already registered for |hex_ssid|.
78 // |hex_ssid|: SSID in hex encoding of the network to be registered.
79 // |extension_id|: Extension ID of the extension handling the network
80 // configuration for this network.
81 bool RegisterHexSsid(std::string hex_ssid, const std::string& extension_id);
83 // Unregisters extension with the ID |extension_id| removing all associated
84 // HexSSIDs from the map.
85 // |extension_id|: ID identifying the extenion to be removed
86 void UnregisterExtension(const std::string& extensionId);
88 // Returns the current AuthenticationResult.
89 const AuthenticationResult& GetAuthenticationResult() const;
91 // Sets the authentication_state to NOTRY and clears all other fields.
92 void ResetAuthenticationResult();
94 // Sets the current AuthenticationResult.
95 void SetAuthenticationResult(
96 const AuthenticationResult& authentication_result);
98 // Sends a PortalDetected event for the network with |guid| to the extension
99 // with |extension_id|.
100 // |authentication_callback| is stored and called if the extension finishes
101 // authentication succesfully. This Service handles at most one portal
102 // detection at a time, i.e. another call to this function or a not successful
103 // authentication will drop a previously provided |authentication_callback|.
104 void DispatchPortalDetectedEvent(
105 const std::string& extension_id,
106 const std::string& guid,
107 const base::Closure& authentication_callback);
109 private:
110 void OnGotProperties(const std::string& extension_id,
111 const std::string& guid,
112 const base::Closure& authentication_callback,
113 const std::string& service_path,
114 const base::DictionaryValue& onc_network_config);
116 void OnGetPropertiesFailed(const std::string& extension_id,
117 const std::string& guid,
118 const std::string& error_name,
119 scoped_ptr<base::DictionaryValue> error_data);
121 // Creates the captive portal event about the network with guid |guid| that is
122 // to be dispatched to the extension identified by |extension_id|. |bssid|
123 // contains a human readable, hex-encoded version of the BSSID with bytes
124 // separated by colons, e.g. 45:67:89:ab:cd:ef.
125 scoped_ptr<Event> CreatePortalDetectedEventAndDispatch(
126 const std::string& extension_id,
127 const std::string& guid,
128 const std::string* bssid);
130 content::BrowserContext* const browser_context_;
132 AuthenticationResult authentication_result_;
133 base::Closure authentication_callback_;
135 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
136 registry_observer_;
138 scoped_ptr<EventDelegate> event_delegate_;
140 // This map associates a given hex encoded SSID to an extension entry.
141 std::map<std::string, std::string> hex_ssid_to_extension_id_;
143 base::WeakPtrFactory<NetworkingConfigService> weak_factory_;
146 } // namespace extensions
148 #endif // EXTENSIONS_BROWSER_API_NETWORKING_CONFIG_NETWORKING_CONFIG_SERVICE_H_