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 "mash/browser_driver/browser_driver_application_delegate.h" | 5 #include "mash/browser_driver/browser_driver_application_delegate.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/message_loop/message_loop.h" |
10 #include "components/mus/public/cpp/event_matcher.h" | 11 #include "components/mus/public/cpp/event_matcher.h" |
11 #include "mojo/shell/public/cpp/connection.h" | 12 #include "mojo/shell/public/cpp/connection.h" |
12 #include "mojo/shell/public/cpp/connector.h" | 13 #include "mojo/shell/public/cpp/connector.h" |
13 | 14 |
14 namespace mash { | 15 namespace mash { |
15 namespace browser_driver { | 16 namespace browser_driver { |
16 namespace { | 17 namespace { |
17 | 18 |
18 enum class Accelerator : uint32_t { | 19 enum class Accelerator : uint32_t { |
19 NewWindow, | 20 NewWindow, |
(...skipping 15 matching lines...) Expand all Loading... |
35 {Accelerator::NewTab, mus::mojom::KeyboardCode::T, | 36 {Accelerator::NewTab, mus::mojom::KeyboardCode::T, |
36 mus::mojom::kEventFlagControlDown}, | 37 mus::mojom::kEventFlagControlDown}, |
37 {Accelerator::NewIncognitoWindow, mus::mojom::KeyboardCode::N, | 38 {Accelerator::NewIncognitoWindow, mus::mojom::KeyboardCode::N, |
38 mus::mojom::kEventFlagControlDown | mus::mojom::kEventFlagShiftDown}, | 39 mus::mojom::kEventFlagControlDown | mus::mojom::kEventFlagShiftDown}, |
39 }; | 40 }; |
40 | 41 |
41 void AssertTrue(bool success) { | 42 void AssertTrue(bool success) { |
42 DCHECK(success); | 43 DCHECK(success); |
43 } | 44 } |
44 | 45 |
| 46 void DoNothing() {} |
| 47 |
45 } // namespace | 48 } // namespace |
46 | 49 |
47 BrowserDriverApplicationDelegate::BrowserDriverApplicationDelegate() | 50 BrowserDriverApplicationDelegate::BrowserDriverApplicationDelegate() |
48 : connector_(nullptr), | 51 : connector_(nullptr), |
49 binding_(this) {} | 52 binding_(this), |
| 53 weak_factory_(this) {} |
50 | 54 |
51 BrowserDriverApplicationDelegate::~BrowserDriverApplicationDelegate() {} | 55 BrowserDriverApplicationDelegate::~BrowserDriverApplicationDelegate() {} |
52 | 56 |
53 void BrowserDriverApplicationDelegate::Initialize( | 57 void BrowserDriverApplicationDelegate::Initialize( |
54 mojo::Connector* connector, | 58 mojo::Connector* connector, |
55 const mojo::Identity& identity, | 59 const mojo::Identity& identity, |
56 uint32_t id) { | 60 uint32_t id) { |
57 connector_ = connector; | 61 connector_ = connector; |
58 AddAccelerators(); | 62 AddAccelerators(); |
59 } | 63 } |
60 | 64 |
61 bool BrowserDriverApplicationDelegate::AcceptConnection( | 65 bool BrowserDriverApplicationDelegate::AcceptConnection( |
62 mojo::Connection* connection) { | 66 mojo::Connection* connection) { |
63 return true; | 67 return true; |
64 } | 68 } |
65 | 69 |
| 70 void BrowserDriverApplicationDelegate::ShellConnectionLost() { |
| 71 // Prevent the code in AddAccelerators() from keeping this app alive. |
| 72 binding_.set_connection_error_handler(base::Bind(&DoNothing)); |
| 73 base::MessageLoop::current()->QuitWhenIdle(); |
| 74 } |
| 75 |
66 void BrowserDriverApplicationDelegate::OnAccelerator( | 76 void BrowserDriverApplicationDelegate::OnAccelerator( |
67 uint32_t id, mus::mojom::EventPtr event) { | 77 uint32_t id, mus::mojom::EventPtr event) { |
68 switch (static_cast<Accelerator>(id)) { | 78 switch (static_cast<Accelerator>(id)) { |
69 case Accelerator::NewWindow: | 79 case Accelerator::NewWindow: |
70 case Accelerator::NewTab: | 80 case Accelerator::NewTab: |
71 case Accelerator::NewIncognitoWindow: | 81 case Accelerator::NewIncognitoWindow: |
72 connector_->Connect("exe:chrome"); | 82 connector_->Connect("exe:chrome"); |
73 // TODO(beng): have Chrome export a service that allows it to be driven by | 83 // TODO(beng): have Chrome export a service that allows it to be driven by |
74 // this driver, e.g. to open new tabs, incognito windows, etc. | 84 // this driver, e.g. to open new tabs, incognito windows, etc. |
75 break; | 85 break; |
76 default: | 86 default: |
77 NOTREACHED(); | 87 NOTREACHED(); |
78 break; | 88 break; |
79 } | 89 } |
80 } | 90 } |
81 | 91 |
82 void BrowserDriverApplicationDelegate::AddAccelerators() { | 92 void BrowserDriverApplicationDelegate::AddAccelerators() { |
83 // TODO(beng): find some other way to get the window manager. I don't like | 93 // TODO(beng): find some other way to get the window manager. I don't like |
84 // having to specify it by URL because it may differ per display. | 94 // having to specify it by URL because it may differ per display. |
85 mus::mojom::AcceleratorRegistrarPtr registrar; | 95 mus::mojom::AcceleratorRegistrarPtr registrar; |
86 connector_->ConnectToInterface("mojo:desktop_wm", ®istrar); | 96 connector_->ConnectToInterface("mojo:desktop_wm", ®istrar); |
87 | 97 |
88 if (binding_.is_bound()) | 98 if (binding_.is_bound()) |
89 binding_.Unbind(); | 99 binding_.Unbind(); |
90 registrar->SetHandler(binding_.CreateInterfacePtrAndBind()); | 100 registrar->SetHandler(binding_.CreateInterfacePtrAndBind()); |
91 // If the window manager restarts, the handler pipe will close and we'll need | 101 // If the window manager restarts, the handler pipe will close and we'll need |
92 // to re-add our accelerators when the window manager comes back up. | 102 // to re-add our accelerators when the window manager comes back up. |
93 binding_.set_connection_error_handler( | 103 binding_.set_connection_error_handler( |
94 base::Bind(&BrowserDriverApplicationDelegate::AddAccelerators, | 104 base::Bind(&BrowserDriverApplicationDelegate::AddAccelerators, |
95 base::Unretained(this))); | 105 weak_factory_.GetWeakPtr())); |
96 | 106 |
97 for (const AcceleratorSpec& spec : g_spec) { | 107 for (const AcceleratorSpec& spec : g_spec) { |
98 registrar->AddAccelerator( | 108 registrar->AddAccelerator( |
99 static_cast<uint32_t>(spec.id), | 109 static_cast<uint32_t>(spec.id), |
100 mus::CreateKeyMatcher(spec.keyboard_code, spec.event_flags), | 110 mus::CreateKeyMatcher(spec.keyboard_code, spec.event_flags), |
101 base::Bind(&AssertTrue)); | 111 base::Bind(&AssertTrue)); |
102 } | 112 } |
103 } | 113 } |
104 | 114 |
105 } // namespace browser_driver | 115 } // namespace browser_driver |
106 } // namespace main | 116 } // namespace main |
OLD | NEW |