lazyboy | e464732f | 2017-06-15 21:17:27 | [diff] [blame] | 1 | // Copyright 2017 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 EXTENSIONS_BROWSER_LAZY_CONTEXT_ID_H_ |
| 6 | #define EXTENSIONS_BROWSER_LAZY_CONTEXT_ID_H_ |
| 7 | |
David Bertoni | 6478643 | 2018-12-21 23:10:12 | [diff] [blame] | 8 | #include <tuple> |
| 9 | |
lazyboy | e464732f | 2017-06-15 21:17:27 | [diff] [blame] | 10 | #include "extensions/common/extension_id.h" |
| 11 | #include "url/gurl.h" |
| 12 | |
| 13 | namespace content { |
| 14 | class BrowserContext; |
| 15 | } |
| 16 | |
| 17 | namespace extensions { |
lazyboy | 63b994a | 2017-06-30 21:20:23 | [diff] [blame] | 18 | class LazyContextTaskQueue; |
lazyboy | e464732f | 2017-06-15 21:17:27 | [diff] [blame] | 19 | |
| 20 | class LazyContextId { |
| 21 | public: |
| 22 | enum class Type { |
| 23 | kEventPage, |
lazyboy | 63b994a | 2017-06-30 21:20:23 | [diff] [blame] | 24 | kServiceWorker, |
lazyboy | e464732f | 2017-06-15 21:17:27 | [diff] [blame] | 25 | }; |
| 26 | |
| 27 | // An event page (lazy background) context. |
| 28 | LazyContextId(content::BrowserContext* context, |
| 29 | const ExtensionId& extension_id); |
lazyboy | 63b994a | 2017-06-30 21:20:23 | [diff] [blame] | 30 | |
| 31 | // An extension service worker context. |
| 32 | LazyContextId(content::BrowserContext* context, |
| 33 | const ExtensionId& extension_id, |
| 34 | const GURL& service_worker_scope); |
lazyboy | e464732f | 2017-06-15 21:17:27 | [diff] [blame] | 35 | |
David Bertoni | 6478643 | 2018-12-21 23:10:12 | [diff] [blame] | 36 | // Copy and move constructors. |
| 37 | LazyContextId(const LazyContextId& other) = default; |
| 38 | LazyContextId(LazyContextId&& other) = default; |
| 39 | |
| 40 | LazyContextId& operator=(const LazyContextId&) noexcept = default; |
| 41 | LazyContextId& operator=(LazyContextId&&) noexcept = default; |
| 42 | |
lazyboy | e464732f | 2017-06-15 21:17:27 | [diff] [blame] | 43 | bool is_for_event_page() const { return type_ == Type::kEventPage; } |
lazyboy | 63b994a | 2017-06-30 21:20:23 | [diff] [blame] | 44 | bool is_for_service_worker() const { return type_ == Type::kServiceWorker; } |
lazyboy | e464732f | 2017-06-15 21:17:27 | [diff] [blame] | 45 | |
| 46 | content::BrowserContext* browser_context() const { return context_; } |
| 47 | void set_browser_context(content::BrowserContext* context) { |
| 48 | context_ = context; |
| 49 | } |
| 50 | |
| 51 | const ExtensionId& extension_id() const { return extension_id_; } |
| 52 | |
lazyboy | 63b994a | 2017-06-30 21:20:23 | [diff] [blame] | 53 | const GURL& service_worker_scope() const { |
| 54 | DCHECK(is_for_service_worker()); |
| 55 | return service_worker_scope_; |
| 56 | } |
| 57 | |
David Bertoni | 27dfaff2 | 2018-12-12 20:05:37 | [diff] [blame] | 58 | LazyContextTaskQueue* GetTaskQueue() const; |
lazyboy | e464732f | 2017-06-15 21:17:27 | [diff] [blame] | 59 | |
David Bertoni | 6478643 | 2018-12-21 23:10:12 | [diff] [blame] | 60 | bool operator<(const LazyContextId& rhs) const { |
Istiaque Ahmed | 92ad7fc | 2019-11-18 19:02:26 | [diff] [blame] | 61 | return std::tie(type_, context_, extension_id_, service_worker_scope_) < |
| 62 | std::tie(rhs.type_, rhs.context_, rhs.extension_id_, |
| 63 | rhs.service_worker_scope_); |
David Bertoni | 6478643 | 2018-12-21 23:10:12 | [diff] [blame] | 64 | } |
lazyboy | e464732f | 2017-06-15 21:17:27 | [diff] [blame] | 65 | |
Istiaque Ahmed | 92ad7fc | 2019-11-18 19:02:26 | [diff] [blame] | 66 | bool operator==(const LazyContextId& rhs) const { |
| 67 | return std::tie(type_, context_, extension_id_, service_worker_scope_) == |
| 68 | std::tie(rhs.type_, rhs.context_, rhs.extension_id_, |
| 69 | rhs.service_worker_scope_); |
| 70 | } |
| 71 | |
| 72 | bool operator!=(const LazyContextId& rhs) const { return !(*this == rhs); } |
| 73 | |
David Bertoni | 6478643 | 2018-12-21 23:10:12 | [diff] [blame] | 74 | private: |
| 75 | Type type_; |
| 76 | content::BrowserContext* context_; |
| 77 | ExtensionId extension_id_; |
| 78 | GURL service_worker_scope_; |
lazyboy | e464732f | 2017-06-15 21:17:27 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | } // namespace extensions |
| 82 | |
| 83 | #endif // EXTENSIONS_BROWSER_LAZY_CONTEXT_ID_H_ |