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 EXTENSIONS_COMMON_API_MESSAGING_PORT_CONTEXT_H_
6 #define EXTENSIONS_COMMON_API_MESSAGING_PORT_CONTEXT_H_
7 
8 #include <stddef.h>
9 
10 #include <string>
11 
12 #include "base/optional.h"
13 
14 namespace extensions {
15 
16 // Specifies the renderer context that is tied to a message Port.
17 // A port can refer to a RenderFrame (FrameContext) or a Service Worker
18 // (WorkerContext).
19 struct PortContext {
20  public:
21   PortContext();
22   ~PortContext();
23   PortContext(const PortContext& other);
24 
25   struct FrameContext {
26     FrameContext();
27     explicit FrameContext(int routing_id);
28 
29     // The routing id of the frame context.
30     // This may be MSG_ROUTING_NONE if the context is process-wide and isn't
31     // tied to a specific RenderFrame.
32     int routing_id;
33   };
34 
35   struct WorkerContext {
36     WorkerContext();
37     WorkerContext(int thread_id,
38                   int64_t version_id,
39                   const std::string& extension_id);
40 
41     int thread_id;
42     int64_t version_id;
43     std::string extension_id;
44   };
45 
46   static PortContext ForFrame(int routing_id);
47   static PortContext ForWorker(int thread_id,
48                                int64_t version_id,
49                                const std::string& extension_id);
50 
is_for_render_framePortContext51   bool is_for_render_frame() const { return frame.has_value(); }
is_for_service_workerPortContext52   bool is_for_service_worker() const { return worker.has_value(); }
53 
54   base::Optional<FrameContext> frame;
55   base::Optional<WorkerContext> worker;
56 };
57 
58 }  // namespace extensions
59 
60 #endif  // EXTENSIONS_COMMON_API_MESSAGING_PORT_CONTEXT_H_
61