Reland: Plugin Placeholders: Refactor for platforms that don't support plugins
[chromium-blink-merge.git] / content / browser / browser_io_surface_manager_mac.h
blob6b9b5eaced6300865cb20092faaf133237963865
1 // Copyright 2015 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 CONTENT_BROWSER_BROWSER_IO_SURFACE_MANAGER_MAC_H_
6 #define CONTENT_BROWSER_BROWSER_IO_SURFACE_MANAGER_MAC_H_
8 #include <mach/mach.h>
10 #include <map>
11 #include <utility>
13 #include "base/containers/scoped_ptr_hash_map.h"
14 #include "base/mac/dispatch_source_mach.h"
15 #include "base/mac/scoped_mach_port.h"
16 #include "base/macros.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/memory/singleton.h"
19 #include "base/synchronization/lock.h"
20 #include "content/common/content_export.h"
21 #include "content/common/mac/io_surface_manager.h"
22 #include "content/common/mac/io_surface_manager_messages.h"
23 #include "content/common/mac/io_surface_manager_token.h"
25 namespace content {
27 // Implementation of IOSurfaceManager that provides a mechanism for child
28 // processes to register and acquire IOSurfaces through a Mach service.
29 class CONTENT_EXPORT BrowserIOSurfaceManager : public IOSurfaceManager {
30 public:
31 // Returns the global BrowserIOSurfaceManager.
32 static BrowserIOSurfaceManager* GetInstance();
34 // Look up the IOSurfaceManager service port that's been registered with
35 // the bootstrap server. |pid| is the process ID of the service.
36 static base::mac::ScopedMachSendRight LookupServicePort(pid_t pid);
38 // Overridden from IOSurfaceManager:
39 bool RegisterIOSurface(int io_surface_id,
40 int client_id,
41 IOSurfaceRef io_surface) override;
42 void UnregisterIOSurface(int io_surface_id, int client_id) override;
43 IOSurfaceRef AcquireIOSurface(int io_surface_id) override;
45 // Performs any necessary setup that cannot happen in the constructor.
46 void EnsureRunning();
48 // Get the unique unguessable token that the GPU process can use to
49 // register/unregister IOSurface for use by clients.
50 IOSurfaceManagerToken GetGpuProcessToken() const;
52 // Generate a unique unguessable token that the child process associated
53 // |child_process_id| can use to acquire IOSurface references.
54 IOSurfaceManagerToken GenerateChildProcessToken(int child_process_id);
56 // Invalidate a previously generated token.
57 void InvalidateChildProcessToken(const IOSurfaceManagerToken& token);
59 private:
60 friend class BrowserIOSurfaceManagerTest;
61 friend struct DefaultSingletonTraits<BrowserIOSurfaceManager>;
63 BrowserIOSurfaceManager();
64 ~BrowserIOSurfaceManager() override;
66 // Performs any initialization work.
67 bool Initialize();
69 // Message handler that is invoked on |dispatch_source_| when an
70 // incoming message needs to be received.
71 void HandleRequest();
73 // Message handlers that are invoked from HandleRequest.
74 bool HandleRegisterIOSurfaceRequest(
75 const IOSurfaceManagerHostMsg_RegisterIOSurface& request,
76 IOSurfaceManagerMsg_RegisterIOSurfaceReply* reply);
77 bool HandleUnregisterIOSurfaceRequest(
78 const IOSurfaceManagerHostMsg_UnregisterIOSurface& request);
79 bool HandleAcquireIOSurfaceRequest(
80 const IOSurfaceManagerHostMsg_AcquireIOSurface& request,
81 IOSurfaceManagerMsg_AcquireIOSurfaceReply* reply);
83 // Whether or not the class has been initialized.
84 bool initialized_;
86 // The Mach port on which the server listens.
87 base::mac::ScopedMachReceiveRight server_port_;
89 // The dispatch source and queue on which Mach messages will be received.
90 scoped_ptr<base::DispatchSourceMach> dispatch_source_;
92 // Stores the IOSurfaces for all GPU clients. The key contains the IOSurface
93 // id and the Child process unique id of the owner.
94 using IOSurfaceMapKey = std::pair<int, int>;
95 using IOSurfaceMap =
96 base::ScopedPtrHashMap<IOSurfaceMapKey,
97 scoped_ptr<base::mac::ScopedMachSendRight>>;
98 IOSurfaceMap io_surfaces_;
100 // Stores the Child process unique id (RenderProcessHost ID) for every
101 // token.
102 using ChildProcessIdMap = std::map<IOSurfaceManagerToken, int>;
103 ChildProcessIdMap child_process_ids_;
105 // Stores the GPU process token.
106 const IOSurfaceManagerToken gpu_process_token_;
108 // Mutex that guards |initialized_|, |io_surfaces_| and |child_process_ids_|.
109 mutable base::Lock lock_;
111 DISALLOW_COPY_AND_ASSIGN(BrowserIOSurfaceManager);
114 } // namespace content
116 #endif // CONTENT_BROWSER_BROWSER_IO_SURFACE_MANAGER_MAC_H_