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