| 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 "base/macros.h" | |
| 6 #include "base/strings/string_util.h" | |
| 7 #include "base/strings/utf_string_conversions.h" | |
| 8 #include "examples/browser/browser_host.mojom.h" | |
| 9 #include "examples/window_manager/window_manager.mojom.h" | |
| 10 #include "mojo/application/application_runner_chromium.h" | |
| 11 #include "mojo/converters/geometry/geometry_type_converters.h" | |
| 12 #include "mojo/public/c/system/main.h" | |
| 13 #include "mojo/public/cpp/application/application_connection.h" | |
| 14 #include "mojo/public/cpp/application/application_delegate.h" | |
| 15 #include "mojo/public/cpp/application/application_impl.h" | |
| 16 #include "mojo/public/cpp/application/connect.h" | |
| 17 #include "mojo/public/cpp/application/service_provider_impl.h" | |
| 18 #include "mojo/services/navigation/interfaces/navigation.mojom.h" | |
| 19 #include "mojo/services/view_manager/cpp/view.h" | |
| 20 #include "mojo/services/view_manager/cpp/view_manager.h" | |
| 21 #include "mojo/services/view_manager/cpp/view_manager_client_factory.h" | |
| 22 #include "mojo/services/view_manager/cpp/view_manager_delegate.h" | |
| 23 #include "mojo/services/view_manager/cpp/view_observer.h" | |
| 24 #include "url/gurl.h" | |
| 25 | |
| 26 namespace mojo { | |
| 27 namespace examples { | |
| 28 | |
| 29 // This is the basics of creating a views widget with a textfield. | |
| 30 // TODO: cleanup! | |
| 31 class Browser : public ApplicationDelegate, | |
| 32 public ViewManagerDelegate, | |
| 33 public ViewObserver, | |
| 34 public examples::BrowserHost, | |
| 35 public mojo::InterfaceFactory<examples::BrowserHost> { | |
| 36 public: | |
| 37 Browser() : shell_(nullptr), root_(NULL), binding_(this) { | |
| 38 browser_host_services_impl_.AddService(this); | |
| 39 } | |
| 40 | |
| 41 ~Browser() override { | |
| 42 if (root_) | |
| 43 root_->RemoveObserver(this); | |
| 44 } | |
| 45 | |
| 46 private: | |
| 47 // Overridden from ApplicationDelegate: | |
| 48 void Initialize(ApplicationImpl* app) override { | |
| 49 shell_ = app->shell(); | |
| 50 view_manager_client_factory_.reset( | |
| 51 new ViewManagerClientFactory(shell_, this)); | |
| 52 app->ConnectToService("mojo:window_manager", &window_manager_); | |
| 53 | |
| 54 // FIXME: Mojo applications don't know their URLs yet: | |
| 55 // https://docs.google.com/a/chromium.org/document/d/1AQ2y6ekzvbdaMF5WrUQmne
yXJnke-MnYYL4Gz1AKDos | |
| 56 url_ = GURL(app->args()[1]); | |
| 57 } | |
| 58 | |
| 59 bool ConfigureIncomingConnection(ApplicationConnection* connection) override { | |
| 60 connection->AddService(view_manager_client_factory_.get()); | |
| 61 return true; | |
| 62 } | |
| 63 | |
| 64 // ViewManagerDelegate: | |
| 65 void OnEmbed(View* root, | |
| 66 InterfaceRequest<ServiceProvider> services, | |
| 67 ServiceProviderPtr exposed_services) override { | |
| 68 // TODO: deal with OnEmbed() being invoked multiple times. | |
| 69 ConnectToService(exposed_services.get(), &navigator_host_); | |
| 70 root_ = root; | |
| 71 root_->AddObserver(this); | |
| 72 root_->SetFocus(); | |
| 73 | |
| 74 // Create a child view for our sky document. | |
| 75 View* view = root->view_manager()->CreateView(); | |
| 76 root->AddChild(view); | |
| 77 Rect bounds; | |
| 78 bounds.x = 0; | |
| 79 bounds.y = 0; | |
| 80 bounds.width = root->bounds().width; | |
| 81 bounds.height = root->bounds().height; | |
| 82 view->SetBounds(bounds); | |
| 83 view->SetVisible(true); | |
| 84 root->SetVisible(true); | |
| 85 | |
| 86 ServiceProviderPtr browser_host_services; | |
| 87 browser_host_services_impl_.Bind(GetProxy(&browser_host_services)); | |
| 88 | |
| 89 GURL frame_url = url_.Resolve("/examples/browser/browser.sky"); | |
| 90 view->Embed(frame_url.spec(), nullptr, browser_host_services.Pass()); | |
| 91 } | |
| 92 | |
| 93 void OnViewManagerDisconnected(ViewManager* view_manager) override { | |
| 94 base::MessageLoop::current()->Quit(); | |
| 95 } | |
| 96 | |
| 97 // ViewObserver: | |
| 98 void OnViewDestroyed(View* view) override { | |
| 99 DCHECK_EQ(root_, view); | |
| 100 view->RemoveObserver(this); | |
| 101 root_ = NULL; | |
| 102 } | |
| 103 | |
| 104 // examples::BrowserHost: | |
| 105 void NavigateTo(const mojo::String& url) override { | |
| 106 URLRequestPtr request(URLRequest::New()); | |
| 107 request->url = url; | |
| 108 navigator_host_->RequestNavigate(Target::NEW_NODE, request.Pass()); | |
| 109 } | |
| 110 | |
| 111 // mojo::InterfaceFactory<examples::BrowserHost> implementation. | |
| 112 void Create(mojo::ApplicationConnection* connection, | |
| 113 mojo::InterfaceRequest<examples::BrowserHost> request) override { | |
| 114 binding_.Bind(request.Pass()); | |
| 115 } | |
| 116 | |
| 117 Shell* shell_; | |
| 118 | |
| 119 scoped_ptr<ViewManagerClientFactory> view_manager_client_factory_; | |
| 120 View* root_; | |
| 121 NavigatorHostPtr navigator_host_; | |
| 122 ::examples::IWindowManagerPtr window_manager_; | |
| 123 ServiceProviderImpl browser_host_services_impl_; | |
| 124 | |
| 125 GURL url_; | |
| 126 | |
| 127 mojo::Binding<examples::BrowserHost> binding_; | |
| 128 | |
| 129 DISALLOW_COPY_AND_ASSIGN(Browser); | |
| 130 }; | |
| 131 | |
| 132 } // namespace examples | |
| 133 } // namespace mojo | |
| 134 | |
| 135 MojoResult MojoMain(MojoHandle application_request) { | |
| 136 mojo::ApplicationRunnerChromium runner(new mojo::examples::Browser); | |
| 137 return runner.Run(application_request); | |
| 138 } | |
| OLD | NEW |