OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "mash/browser_driver/browser_driver_application_delegate.h" | |
6 | |
7 #include <stdint.h> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/message_loop/message_loop.h" | |
11 #include "components/mus/common/event_matcher_util.h" | |
12 #include "mash/public/interfaces/launchable.mojom.h" | |
13 #include "services/shell/public/cpp/connection.h" | |
14 #include "services/shell/public/cpp/connector.h" | |
15 | |
16 using mash::mojom::LaunchablePtr; | |
17 using mash::mojom::LaunchMode; | |
18 | |
19 namespace mash { | |
20 namespace browser_driver { | |
21 namespace { | |
22 | |
23 enum class Accelerator : uint32_t { | |
24 NewWindow, | |
25 NewTab, | |
26 NewIncognitoWindow, | |
27 }; | |
28 | |
29 struct AcceleratorSpec { | |
30 Accelerator id; | |
31 mus::mojom::KeyboardCode keyboard_code; | |
32 // A bitfield of kEventFlag* and kMouseEventFlag* values in | |
33 // input_event_constants.mojom. | |
34 int event_flags; | |
35 }; | |
36 | |
37 AcceleratorSpec g_spec[] = { | |
38 {Accelerator::NewWindow, mus::mojom::KeyboardCode::N, | |
39 mus::mojom::kEventFlagControlDown}, | |
40 {Accelerator::NewTab, mus::mojom::KeyboardCode::T, | |
41 mus::mojom::kEventFlagControlDown}, | |
42 {Accelerator::NewIncognitoWindow, mus::mojom::KeyboardCode::N, | |
43 mus::mojom::kEventFlagControlDown | mus::mojom::kEventFlagShiftDown}, | |
44 }; | |
45 | |
46 void AssertTrue(bool success) { | |
47 DCHECK(success); | |
48 } | |
49 | |
50 void DoNothing() {} | |
51 | |
52 } // namespace | |
53 | |
54 BrowserDriverApplicationDelegate::BrowserDriverApplicationDelegate() | |
55 : connector_(nullptr), | |
56 binding_(this), | |
57 weak_factory_(this) {} | |
58 | |
59 BrowserDriverApplicationDelegate::~BrowserDriverApplicationDelegate() {} | |
60 | |
61 void BrowserDriverApplicationDelegate::OnAvailableCatalogEntries( | |
62 const mojo::Array<catalog::mojom::EntryPtr>& entries) { | |
63 if (entries.empty()) { | |
64 LOG(ERROR) << "Unable to install accelerators for launching chrome."; | |
65 return; | |
66 } | |
67 | |
68 mus::mojom::AcceleratorRegistrarPtr registrar; | |
69 connector_->ConnectToInterface(entries[0]->name, ®istrar); | |
70 | |
71 if (binding_.is_bound()) | |
72 binding_.Unbind(); | |
73 registrar->SetHandler(binding_.CreateInterfacePtrAndBind()); | |
74 // If the window manager restarts, the handler pipe will close and we'll need | |
75 // to re-add our accelerators when the window manager comes back up. | |
76 binding_.set_connection_error_handler( | |
77 base::Bind(&BrowserDriverApplicationDelegate::AddAccelerators, | |
78 weak_factory_.GetWeakPtr())); | |
79 | |
80 for (const AcceleratorSpec& spec : g_spec) { | |
81 registrar->AddAccelerator( | |
82 static_cast<uint32_t>(spec.id), | |
83 mus::CreateKeyMatcher(spec.keyboard_code, spec.event_flags), | |
84 base::Bind(&AssertTrue)); | |
85 } | |
86 } | |
87 | |
88 void BrowserDriverApplicationDelegate::Initialize( | |
89 shell::Connector* connector, | |
90 const shell::Identity& identity, | |
91 uint32_t id) { | |
92 connector_ = connector; | |
93 AddAccelerators(); | |
94 } | |
95 | |
96 bool BrowserDriverApplicationDelegate::AcceptConnection( | |
97 shell::Connection* connection) { | |
98 return true; | |
99 } | |
100 | |
101 bool BrowserDriverApplicationDelegate::ShellConnectionLost() { | |
102 // Prevent the code in AddAccelerators() from keeping this app alive. | |
103 binding_.set_connection_error_handler(base::Bind(&DoNothing)); | |
104 return true; | |
105 } | |
106 | |
107 void BrowserDriverApplicationDelegate::OnAccelerator( | |
108 uint32_t id, mus::mojom::EventPtr event) { | |
109 uint32_t option = mojom::kWindow; | |
110 switch (static_cast<Accelerator>(id)) { | |
111 case Accelerator::NewWindow: | |
112 option = mojom::kWindow; | |
113 break; | |
114 case Accelerator::NewTab: | |
115 option = mojom::kDocument; | |
116 break; | |
117 case Accelerator::NewIncognitoWindow: | |
118 option = mojom::kIncognitoWindow; | |
119 break; | |
120 default: | |
121 NOTREACHED(); | |
122 break; | |
123 } | |
124 LaunchablePtr launchable; | |
125 connector_->ConnectToInterface("exe:chrome", &launchable); | |
126 launchable->Launch(option, LaunchMode::MAKE_NEW); | |
127 } | |
128 | |
129 void BrowserDriverApplicationDelegate::AddAccelerators() { | |
130 connector_->ConnectToInterface("mojo:catalog", &catalog_); | |
131 catalog_->GetEntriesProvidingClass( | |
132 "mus:window_manager", | |
133 base::Bind(&BrowserDriverApplicationDelegate::OnAvailableCatalogEntries, | |
134 weak_factory_.GetWeakPtr())); | |
135 } | |
136 | |
137 } // namespace browser_driver | |
138 } // namespace mash | |
OLD | NEW |