1 // Copyright 2013 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 CHROME_BROWSER_EXTENSIONS_API_MESSAGING_INCOGNITO_CONNECTABILITY_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_MESSAGING_INCOGNITO_CONNECTABILITY_H_
10 #include "base/memory/weak_ptr.h"
11 #include "extensions/browser/browser_context_keyed_api_factory.h"
24 namespace extensions
{
27 // Tracks the web connectability of domains to extensions in incognito mode.
29 // The most important functionality is prompting the user to allow or disallow
30 // connections from incognito tabs to extensions or apps. Even if an extension
31 // hasn't been enabled in incognito mode, it's still useful for web sites to be
32 // able to send messages to them, with user constent. For apps, it's essential
33 // we have this functionality because there is no way for them to be enabled in
35 class IncognitoConnectability
: public BrowserContextKeyedAPI
{
37 // While in scope, immediately either accepts or denies the alerts that show
38 // up, and counts the number of times it was invoked.
39 class ScopedAlertTracker
{
47 explicit ScopedAlertTracker(Mode mode
);
49 ~ScopedAlertTracker();
51 // Returns the number of times the alert has been shown since
52 // GetAndResetAlertCount was last called.
53 int GetAndResetAlertCount();
56 int last_checked_invocation_count_
;
59 // Returns the IncognitoConnectability object for |context|. |context| must
61 static IncognitoConnectability
* Get(content::BrowserContext
* context
);
63 // Passes true to the provided callback if |url| is allowed to connect from
64 // this profile, false otherwise. If unknown, the user will be prompted before
65 // an answer is returned.
66 void Query(const Extension
* extension
,
67 content::WebContents
* web_contents
,
69 const base::Callback
<void(bool)>& callback
);
76 // The infobar being shown in a given tab. The InfoBarManager maintains
77 // ownership of this object. This struct must always be destroyed before the
79 infobars::InfoBar
* infobar
;
80 // Connectability queries outstanding on this infobar.
81 std::vector
<base::Callback
<void(bool)>> callbacks
;
84 friend class BrowserContextKeyedAPIFactory
<IncognitoConnectability
>;
86 explicit IncognitoConnectability(content::BrowserContext
* context
);
87 ~IncognitoConnectability() override
;
89 typedef std::map
<std::string
, std::set
<GURL
> > ExtensionToOriginsMap
;
90 typedef std::pair
<std::string
, GURL
> ExtensionOriginPair
;
91 typedef std::map
<infobars::InfoBarManager
*, TabContext
> PendingOrigin
;
92 typedef std::map
<ExtensionOriginPair
, PendingOrigin
> PendingOriginMap
;
94 // Called with the user's selection from the infobar.
95 // |response == INTERACTIVE| indicates that the user closed the infobar
96 // without selecting allow or deny.
97 void OnInteractiveResponse(const std::string
& extension_id
,
99 infobars::InfoBarManager
* infobar_manager
,
100 ScopedAlertTracker::Mode response
);
102 // Returns true if the (|extension|, |origin|) pair appears in the map.
103 bool IsInMap(const Extension
* extension
,
105 const ExtensionToOriginsMap
& map
);
107 // BrowserContextKeyedAPI implementation.
108 static BrowserContextKeyedAPIFactory
<IncognitoConnectability
>*
109 GetFactoryInstance();
110 static const char* service_name() {
111 return "Messaging.IncognitoConnectability";
113 static const bool kServiceHasOwnInstanceInIncognito
= true;
114 static const bool kServiceIsCreatedWithBrowserContext
= false;
116 // The origins that have been prompted for and either allowed or disallowed.
117 // These are deliberately stored in-memory so that they're reset when the
118 // profile is destroyed (i.e. when the last incognito window is closed).
119 ExtensionToOriginsMap allowed_origins_
;
120 ExtensionToOriginsMap disallowed_origins_
;
122 // This maps extension/origin pairs to the tabs with an infobar prompting for
123 // incognito connectability on them. This also stores a reference to the
124 // infobar and the set of callbacks (passed to Query) that will be called when
125 // the query is resolved.
126 PendingOriginMap pending_origins_
;
128 base::WeakPtrFactory
<IncognitoConnectability
> weak_factory_
;
131 } // namespace extensions
133 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_INCOGNITO_CONNECTABILITY_H_