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 "chrome/browser/extensions/api/profile_keyed_api_factory.h"
18 namespace extensions {
21 // Tracks the web connectability of domains to extensions in incognito mode.
23 // The most important functionality is prompting the user to allow or disallow
24 // connections from incognito tabs to extensions or apps. Even if an extension
25 // hasn't been enabled in incognito mode, it's still useful for web sites to be
26 // able to send messages to them, with user constent. For apps, it's essential
27 // we have this functionality because there is no way for them to be enabled in
29 class IncognitoConnectability : public ProfileKeyedAPI {
31 // While in scope, immediately either accepts or denies the alerts that show
32 // up, and counts the number of times it was invoked.
33 class ScopedAlertTracker {
41 explicit ScopedAlertTracker(Mode mode);
43 ~ScopedAlertTracker();
45 // Returns the number of times the alert has been shown since
46 // GetAndResetAlertCount was last called.
47 int GetAndResetAlertCount();
50 int last_checked_invocation_count_;
53 // Returns the IncognitoConnectability object for |profile|. |profile| must
55 static IncognitoConnectability* Get(Profile* profile);
57 // Returns true if |url| is allowed to connect from this profile, false
58 // otherwise. If unknown, this call will block and prompt the user.
59 bool Query(const Extension* extension,
60 content::WebContents* web_contents,
64 friend class ProfileKeyedAPIFactory<IncognitoConnectability>;
66 explicit IncognitoConnectability(Profile* profile);
67 virtual ~IncognitoConnectability();
69 typedef std::map<std::string, std::set<GURL> > ExtensionToOriginsMap;
71 // Returns true if the (|extension|, |origin|) pair appears in the map.
72 bool IsInMap(const Extension* extension,
74 const ExtensionToOriginsMap& map);
76 // ProfileKeyedAPI implementation.
77 static ProfileKeyedAPIFactory<IncognitoConnectability>* GetFactoryInstance();
78 static const char* service_name() {
79 return "Messaging.IncognitoConnectability";
81 static const bool kServiceHasOwnInstanceInIncognito = true;
82 static const bool kServiceIsCreatedWithBrowserContext = false;
84 // The origins that have been prompted for and either allowed or disallowed.
85 // These are deliberately stored in-memory so that they're reset when the
86 // profile is destroyed (i.e. when the last incognito window is closed).
87 ExtensionToOriginsMap allowed_origins_;
88 ExtensionToOriginsMap disallowed_origins_;
91 } // namespace extensions
93 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_INCOGNITO_CONNECTABILITY_H_