OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chrome_browser_main_extra_parts_exo.h" | 5 #include "chrome/browser/chrome_browser_main_extra_parts_exo.h" |
6 | 6 |
| 7 #if defined(USE_GLIB) |
| 8 #include <glib.h> |
| 9 #endif |
| 10 |
7 #include "base/command_line.h" | 11 #include "base/command_line.h" |
8 #include "base/macros.h" | 12 #include "base/macros.h" |
9 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
10 #include "chrome/browser/ui/ash/ash_util.h" | 14 #include "chrome/browser/ui/ash/ash_util.h" |
11 #include "chrome/common/chrome_switches.h" | 15 #include "chrome/common/chrome_switches.h" |
12 #include "components/exo/display.h" | 16 #include "components/exo/display.h" |
13 #include "components/exo/wayland/server.h" | 17 #include "components/exo/wayland/server.h" |
14 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
15 | 19 |
| 20 #if defined(USE_GLIB) |
| 21 namespace { |
| 22 |
| 23 struct GLibWaylandSource : public GSource { |
| 24 // Note: The GLibWaylandSource is created and destroyed by GLib. So its |
| 25 // constructor/destructor may or may not get called. |
| 26 exo::wayland::Server* server; |
| 27 GPollFD* poll_fd; |
| 28 }; |
| 29 |
| 30 gboolean WaylandSourcePrepare(GSource* source, gint* timeout_ms) { |
| 31 *timeout_ms = -1; |
| 32 return FALSE; |
| 33 } |
| 34 |
| 35 gboolean WaylandSourceCheck(GSource* source) { |
| 36 GLibWaylandSource* wayland_source = static_cast<GLibWaylandSource*>(source); |
| 37 return (wayland_source->poll_fd->revents & G_IO_IN) ? TRUE : FALSE; |
| 38 } |
| 39 |
| 40 gboolean WaylandSourceDispatch(GSource* source, |
| 41 GSourceFunc unused_func, |
| 42 gpointer data) { |
| 43 GLibWaylandSource* wayland_source = static_cast<GLibWaylandSource*>(source); |
| 44 wayland_source->server->Dispatch(base::TimeDelta()); |
| 45 wayland_source->server->Flush(); |
| 46 return TRUE; |
| 47 } |
| 48 |
| 49 GSourceFuncs g_wayland_source_funcs = {WaylandSourcePrepare, WaylandSourceCheck, |
| 50 WaylandSourceDispatch, nullptr}; |
| 51 |
| 52 } // namespace |
| 53 |
| 54 class ChromeBrowserMainExtraPartsExo::WaylandWatcher { |
| 55 public: |
| 56 explicit WaylandWatcher(exo::wayland::Server* server) |
| 57 : wayland_poll_(new GPollFD), |
| 58 wayland_source_(static_cast<GLibWaylandSource*>( |
| 59 g_source_new(&g_wayland_source_funcs, sizeof(GLibWaylandSource)))) { |
| 60 wayland_poll_->fd = server->GetFileDescriptor(); |
| 61 wayland_poll_->events = G_IO_IN; |
| 62 wayland_poll_->revents = 0; |
| 63 wayland_source_->server = server; |
| 64 wayland_source_->poll_fd = wayland_poll_.get(); |
| 65 g_source_add_poll(wayland_source_, wayland_poll_.get()); |
| 66 g_source_set_can_recurse(wayland_source_, TRUE); |
| 67 g_source_set_callback(wayland_source_, nullptr, nullptr, nullptr); |
| 68 g_source_attach(wayland_source_, g_main_context_default()); |
| 69 } |
| 70 ~WaylandWatcher() { |
| 71 g_source_destroy(wayland_source_); |
| 72 g_source_unref(wayland_source_); |
| 73 } |
| 74 |
| 75 private: |
| 76 // The poll attached to |wayland_source_|. |
| 77 scoped_ptr<GPollFD> wayland_poll_; |
| 78 |
| 79 // The GLib event source for wayland events. |
| 80 GLibWaylandSource* wayland_source_; |
| 81 |
| 82 DISALLOW_COPY_AND_ASSIGN(WaylandWatcher); |
| 83 }; |
| 84 #else |
16 class ChromeBrowserMainExtraPartsExo::WaylandWatcher | 85 class ChromeBrowserMainExtraPartsExo::WaylandWatcher |
17 : public base::MessagePumpLibevent::Watcher { | 86 : public base::MessagePumpLibevent::Watcher { |
18 public: | 87 public: |
19 explicit WaylandWatcher(exo::wayland::Server* server) : server_(server) { | 88 explicit WaylandWatcher(exo::wayland::Server* server) : server_(server) { |
20 base::MessageLoopForUI::current()->WatchFileDescriptor( | 89 base::MessageLoopForUI::current()->WatchFileDescriptor( |
21 server_->GetFileDescriptor(), | 90 server_->GetFileDescriptor(), |
22 true, // persistent | 91 true, // persistent |
23 base::MessagePumpLibevent::WATCH_READ, &controller_, this); | 92 base::MessagePumpLibevent::WATCH_READ, &controller_, this); |
24 } | 93 } |
25 | 94 |
26 // base::MessagePumpLibevent::Watcher: | 95 // base::MessagePumpLibevent::Watcher: |
27 void OnFileCanReadWithoutBlocking(int fd) override { | 96 void OnFileCanReadWithoutBlocking(int fd) override { |
28 server_->Dispatch(base::TimeDelta()); | 97 server_->Dispatch(base::TimeDelta()); |
29 server_->Flush(); | 98 server_->Flush(); |
30 } | 99 } |
31 void OnFileCanWriteWithoutBlocking(int fd) override { NOTREACHED(); } | 100 void OnFileCanWriteWithoutBlocking(int fd) override { NOTREACHED(); } |
32 | 101 |
33 private: | 102 private: |
34 base::MessagePumpLibevent::FileDescriptorWatcher controller_; | 103 base::MessagePumpLibevent::FileDescriptorWatcher controller_; |
35 exo::wayland::Server* const server_; | 104 exo::wayland::Server* const server_; |
36 | 105 |
37 DISALLOW_COPY_AND_ASSIGN(WaylandWatcher); | 106 DISALLOW_COPY_AND_ASSIGN(WaylandWatcher); |
38 }; | 107 }; |
| 108 #endif |
39 | 109 |
40 ChromeBrowserMainExtraPartsExo::ChromeBrowserMainExtraPartsExo() | 110 ChromeBrowserMainExtraPartsExo::ChromeBrowserMainExtraPartsExo() |
41 : display_(new exo::Display) {} | 111 : display_(new exo::Display) {} |
42 | 112 |
43 ChromeBrowserMainExtraPartsExo::~ChromeBrowserMainExtraPartsExo() {} | 113 ChromeBrowserMainExtraPartsExo::~ChromeBrowserMainExtraPartsExo() {} |
44 | 114 |
45 void ChromeBrowserMainExtraPartsExo::PreProfileInit() { | 115 void ChromeBrowserMainExtraPartsExo::PreProfileInit() { |
46 if (!chrome::ShouldOpenAshOnStartup()) | 116 if (!chrome::ShouldOpenAshOnStartup()) |
47 return; | 117 return; |
48 | 118 |
49 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 119 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
50 switches::kEnableWaylandServer)) { | 120 switches::kEnableWaylandServer)) { |
51 wayland_server_ = exo::wayland::Server::Create(display_.get()); | 121 wayland_server_ = exo::wayland::Server::Create(display_.get()); |
52 wayland_watcher_ = | 122 wayland_watcher_ = |
53 make_scoped_ptr(new WaylandWatcher(wayland_server_.get())); | 123 make_scoped_ptr(new WaylandWatcher(wayland_server_.get())); |
54 } | 124 } |
55 } | 125 } |
56 | 126 |
57 void ChromeBrowserMainExtraPartsExo::PostMainMessageLoopRun() { | 127 void ChromeBrowserMainExtraPartsExo::PostMainMessageLoopRun() { |
58 wayland_watcher_.reset(); | 128 wayland_watcher_.reset(); |
59 wayland_server_.reset(); | 129 wayland_server_.reset(); |
60 } | 130 } |
OLD | NEW |