OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 "mojo/services/kiosk_wm/kiosk_wm.h" |
| 6 |
| 7 #include "mojo/services/kiosk_wm/merged_service_provider.h" |
| 8 #include "mojo/services/window_manager/basic_focus_rules.h" |
| 9 |
| 10 namespace kiosk_wm { |
| 11 |
| 12 KioskWM::KioskWM() |
| 13 : window_manager_app_(new window_manager::WindowManagerApp(this, this)), |
| 14 root_(nullptr), |
| 15 content_(nullptr), |
| 16 navigator_host_(this), |
| 17 weak_factory_(this) { |
| 18 exposed_services_impl_.AddService(this); |
| 19 } |
| 20 |
| 21 KioskWM::~KioskWM() { |
| 22 } |
| 23 |
| 24 base::WeakPtr<KioskWM> KioskWM::GetWeakPtr() { |
| 25 return weak_factory_.GetWeakPtr(); |
| 26 } |
| 27 |
| 28 void KioskWM::Initialize(mojo::ApplicationImpl* app) { |
| 29 window_manager_app_->Initialize(app); |
| 30 |
| 31 // Format: --args-for="app_url default_url" |
| 32 if (app->args().size() > 1) |
| 33 default_url_ = app->args()[1]; |
| 34 } |
| 35 |
| 36 bool KioskWM::ConfigureIncomingConnection( |
| 37 mojo::ApplicationConnection* connection) { |
| 38 window_manager_app_->ConfigureIncomingConnection(connection); |
| 39 return true; |
| 40 } |
| 41 |
| 42 bool KioskWM::ConfigureOutgoingConnection( |
| 43 mojo::ApplicationConnection* connection) { |
| 44 window_manager_app_->ConfigureOutgoingConnection(connection); |
| 45 return true; |
| 46 } |
| 47 |
| 48 void KioskWM::OnEmbed( |
| 49 mojo::View* root, |
| 50 mojo::InterfaceRequest<mojo::ServiceProvider> services, |
| 51 mojo::ServiceProviderPtr exposed_services) { |
| 52 // KioskWM does not support being embedded more than once. |
| 53 CHECK(!root_); |
| 54 |
| 55 root_ = root; |
| 56 root_->AddObserver(this); |
| 57 |
| 58 // Resize to match the Nexus 5 aspect ratio: |
| 59 window_manager_app_->SetViewportSize(gfx::Size(320, 640)); |
| 60 |
| 61 content_ = root->view_manager()->CreateView(); |
| 62 content_->SetBounds(root_->bounds()); |
| 63 root_->AddChild(content_); |
| 64 content_->SetVisible(true); |
| 65 |
| 66 window_manager_app_->InitFocus( |
| 67 make_scoped_ptr(new window_manager::BasicFocusRules(root_))); |
| 68 window_manager_app_->accelerator_manager()->Register( |
| 69 ui::Accelerator(ui::VKEY_BROWSER_BACK, 0), |
| 70 ui::AcceleratorManager::kNormalPriority, this); |
| 71 |
| 72 // Now that we're ready, either load a pending url or the default url. |
| 73 if (!pending_url_.empty()) |
| 74 Embed(pending_url_, services.Pass(), exposed_services.Pass()); |
| 75 else if (!default_url_.empty()) |
| 76 Embed(default_url_, services.Pass(), exposed_services.Pass()); |
| 77 } |
| 78 |
| 79 void KioskWM::Embed(const mojo::String& url, |
| 80 mojo::InterfaceRequest<mojo::ServiceProvider> services, |
| 81 mojo::ServiceProviderPtr exposed_services) { |
| 82 // We can get Embed calls before we've actually been |
| 83 // embedded into the root view and content_ is created. |
| 84 // Just save the last url, we'll embed it when we're ready. |
| 85 if (!content_) { |
| 86 pending_url_ = url; |
| 87 return; |
| 88 } |
| 89 |
| 90 merged_service_provider_.reset( |
| 91 new MergedServiceProvider(exposed_services.Pass(), this)); |
| 92 content_->Embed(url, services.Pass(), |
| 93 merged_service_provider_->GetServiceProviderPtr().Pass()); |
| 94 |
| 95 navigator_host_.RecordNavigation(url); |
| 96 } |
| 97 |
| 98 void KioskWM::Create(mojo::ApplicationConnection* connection, |
| 99 mojo::InterfaceRequest<mojo::NavigatorHost> request) { |
| 100 navigator_host_.Bind(request.Pass()); |
| 101 } |
| 102 |
| 103 void KioskWM::OnViewManagerDisconnected( |
| 104 mojo::ViewManager* view_manager) { |
| 105 root_ = nullptr; |
| 106 } |
| 107 |
| 108 void KioskWM::OnViewDestroyed(mojo::View* view) { |
| 109 view->RemoveObserver(this); |
| 110 } |
| 111 |
| 112 void KioskWM::OnViewBoundsChanged(mojo::View* view, |
| 113 const mojo::Rect& old_bounds, |
| 114 const mojo::Rect& new_bounds) { |
| 115 content_->SetBounds(new_bounds); |
| 116 } |
| 117 |
| 118 // Convenience method: |
| 119 void KioskWM::ReplaceContentWithURL(const mojo::String& url) { |
| 120 Embed(url, nullptr, nullptr); |
| 121 } |
| 122 |
| 123 bool KioskWM::AcceleratorPressed(const ui::Accelerator& accelerator) { |
| 124 if (accelerator.key_code() != ui::VKEY_BROWSER_BACK) |
| 125 return false; |
| 126 navigator_host_.RequestNavigateHistory(-1); |
| 127 return true; |
| 128 } |
| 129 |
| 130 bool KioskWM::CanHandleAccelerators() const { |
| 131 return true; |
| 132 } |
| 133 |
| 134 } // namespace kiosk_wm |
OLD | NEW |