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