Alex Moshchuk | 8e5c195 | 2019-01-15 03:39:50 | [diff] [blame] | 1 | // Copyright 2019 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. |
| 4 | |
| 5 | #ifndef CONTENT_BROWSER_ISOLATION_CONTEXT_H_ |
| 6 | #define CONTENT_BROWSER_ISOLATION_CONTEXT_H_ |
| 7 | |
| 8 | #include "base/optional.h" |
Maciej Pawlowski | a37782a0 | 2019-05-16 06:40:28 | [diff] [blame] | 9 | #include "base/util/type_safety/id_type.h" |
Alex Moshchuk | 8e5c195 | 2019-01-15 03:39:50 | [diff] [blame] | 10 | #include "content/common/content_export.h" |
Alex Moshchuk | 99b79542 | 2019-03-07 00:27:32 | [diff] [blame] | 11 | #include "content/public/browser/browser_or_resource_context.h" |
Alex Moshchuk | 8e5c195 | 2019-01-15 03:39:50 | [diff] [blame] | 12 | |
| 13 | namespace content { |
| 14 | |
| 15 | class BrowsingInstance; |
Maciej Pawlowski | 4a3ac685 | 2019-05-09 17:06:14 | [diff] [blame] | 16 | using BrowsingInstanceId = util::IdType32<BrowsingInstance>; |
Alex Moshchuk | 8e5c195 | 2019-01-15 03:39:50 | [diff] [blame] | 17 | |
| 18 | // This class is used to specify the context in which process model decisions |
| 19 | // need to be made. For example, dynamically added isolated origins only take |
| 20 | // effect in future BrowsingInstances, and this class can be used to specify |
| 21 | // that a process model decision is being made from a specific |
| 22 | // BrowsingInstance, so that only isolated origins that are applicable to that |
| 23 | // BrowsingInstance are used. This object may be used on UI or IO threads. |
| 24 | class CONTENT_EXPORT IsolationContext { |
| 25 | public: |
Alex Moshchuk | 99b79542 | 2019-03-07 00:27:32 | [diff] [blame] | 26 | // Normal use cases should create an IsolationContext associated with both a |
| 27 | // BrowsingInstance and a BrowserContext (profile). The constructor that |
| 28 | // takes in a BrowserContext* may only be used on the UI thread; when |
| 29 | // creating this object on the IO thread, the BrowserOrResourceContext |
| 30 | // version should be used instead. |
| 31 | IsolationContext(BrowsingInstanceId browsing_instance_id, |
| 32 | BrowserContext* browser_context); |
| 33 | IsolationContext(BrowsingInstanceId browsing_instance_id, |
| 34 | BrowserOrResourceContext browser_or_resource_context); |
| 35 | |
| 36 | // Also temporarily allow constructing an IsolationContext not associated |
| 37 | // with a specific BrowsingInstance. Callers can use this when they don't |
| 38 | // know the current BrowsingInstance, or aren't associated with one. |
Alex Moshchuk | 8e5c195 | 2019-01-15 03:39:50 | [diff] [blame] | 39 | // |
| 40 | // TODO(alexmos): This is primarily used in tests, as well as in call sites |
| 41 | // which do not yet plumb proper BrowsingInstance information. Once the |
| 42 | // remaining non-test call sites are removed or updated, this should become a |
| 43 | // test-only API. |
Alex Moshchuk | 99b79542 | 2019-03-07 00:27:32 | [diff] [blame] | 44 | explicit IsolationContext(BrowserContext* browser_context); |
Alex Moshchuk | 8e5c195 | 2019-01-15 03:39:50 | [diff] [blame] | 45 | |
Alex Moshchuk | 8e5c195 | 2019-01-15 03:39:50 | [diff] [blame] | 46 | ~IsolationContext() = default; |
| 47 | |
| 48 | // Returns the BrowsingInstance ID associated with this isolation context. |
| 49 | // BrowsingInstance IDs are ordered such that BrowsingInstances with lower |
| 50 | // IDs were created earlier than BrowsingInstances with higher IDs. |
| 51 | // |
| 52 | // If this is not specified (i.e., |browsing_instance_id().is_null()| is |
| 53 | // true), then this IsolationContext isn't restricted to any particular |
| 54 | // BrowsingInstance. Asking for isolated origins from an IsolationContext |
| 55 | // with a null |browsing_instance_id()| will return the latest available |
| 56 | // isolated origins. |
| 57 | BrowsingInstanceId browsing_instance_id() const { |
| 58 | return browsing_instance_id_; |
| 59 | } |
| 60 | |
Alex Moshchuk | 99b79542 | 2019-03-07 00:27:32 | [diff] [blame] | 61 | // Return the BrowserOrResourceContext associated with this IsolationContext. |
| 62 | // This represents the profile associated with this IsolationContext, and can |
| 63 | // be used on both UI and IO threads. |
| 64 | const BrowserOrResourceContext& browser_or_resource_context() const { |
| 65 | return browser_or_resource_context_; |
| 66 | } |
| 67 | |
Alex Moshchuk | 8e5c195 | 2019-01-15 03:39:50 | [diff] [blame] | 68 | private: |
| 69 | // When non-null, associates this context with a particular BrowsingInstance. |
| 70 | BrowsingInstanceId browsing_instance_id_; |
| 71 | |
Alex Moshchuk | 99b79542 | 2019-03-07 00:27:32 | [diff] [blame] | 72 | BrowserOrResourceContext browser_or_resource_context_; |
Alex Moshchuk | 8e5c195 | 2019-01-15 03:39:50 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | } // namespace content |
| 76 | |
| 77 | #endif // CONTENT_BROWSER_ISOLATION_CONTEXT_H_ |