Remove the RenderProcessHost observer and attach the WebContentsObserver earlier...
[chromium-blink-merge.git] / mojo / examples / embedded_app / embedded_app.cc
blob98c418e93842e4c8a7359ff1ebf2cb635dc9c664
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.
5 #include "base/bind.h"
6 #include "base/logging.h"
7 #include "base/macros.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "mojo/application/application_runner_chromium.h"
11 #include "mojo/examples/bitmap_uploader/bitmap_uploader.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/interface_factory_impl.h"
18 #include "mojo/services/public/cpp/view_manager/view.h"
19 #include "mojo/services/public/cpp/view_manager/view_manager.h"
20 #include "mojo/services/public/cpp/view_manager/view_manager_client_factory.h"
21 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h"
22 #include "mojo/services/public/cpp/view_manager/view_observer.h"
23 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h"
24 #include "ui/events/event_constants.h"
25 #include "url/gurl.h"
26 #include "url/url_util.h"
28 namespace mojo {
29 namespace examples {
31 const SkColor kColors[] = {SK_ColorYELLOW, SK_ColorRED, SK_ColorGREEN,
32 SK_ColorMAGENTA};
34 struct Window {
35 Window(View* root,
36 scoped_ptr<ServiceProvider> embedder_service_provider,
37 Shell* shell)
38 : root(root),
39 embedder_service_provider(embedder_service_provider.Pass()),
40 bitmap_uploader(root) {
41 bitmap_uploader.Init(shell);
44 View* root;
45 scoped_ptr<ServiceProvider> embedder_service_provider;
46 BitmapUploader bitmap_uploader;
49 class EmbeddedApp
50 : public ApplicationDelegate,
51 public ViewManagerDelegate,
52 public ViewObserver {
53 public:
54 EmbeddedApp() : shell_(nullptr) { url::AddStandardScheme("mojo"); }
55 virtual ~EmbeddedApp() {}
57 private:
59 // Overridden from ApplicationDelegate:
60 virtual void Initialize(ApplicationImpl* app) override {
61 shell_ = app->shell();
62 view_manager_client_factory_.reset(
63 new ViewManagerClientFactory(app->shell(), this));
66 virtual bool ConfigureIncomingConnection(
67 ApplicationConnection* connection) override {
68 connection->AddService(view_manager_client_factory_.get());
69 return true;
72 // Overridden from ViewManagerDelegate:
73 virtual void OnEmbed(ViewManager* view_manager,
74 View* root,
75 ServiceProviderImpl* exported_services,
76 scoped_ptr<ServiceProvider> imported_services) override {
77 root->AddObserver(this);
78 Window* window = new Window(root, imported_services.Pass(), shell_);
79 windows_[root->id()] = window;
80 window->bitmap_uploader.SetColor(
81 kColors[next_color_++ % arraysize(kColors)]);
83 virtual void OnViewManagerDisconnected(ViewManager* view_manager) override {
84 base::MessageLoop::current()->Quit();
87 // Overridden from ViewObserver:
88 virtual void OnViewDestroyed(View* view) override {
89 DCHECK(windows_.find(view->id()) != windows_.end());
90 windows_.erase(view->id());
92 virtual void OnViewInputEvent(View* view, const EventPtr& event) override {
93 if (event->action == EVENT_TYPE_MOUSE_RELEASED) {
94 if (event->flags & EVENT_FLAGS_LEFT_MOUSE_BUTTON) {
95 URLRequestPtr request(URLRequest::New());
96 request->url = "http://www.aaronboodman.com/z_dropbox/test.html";
97 NavigatorHostPtr navigator_host;
98 ConnectToService(windows_[view->id()]->embedder_service_provider.get(),
99 &navigator_host);
100 navigator_host->RequestNavigate(TARGET_SOURCE_NODE, request.Pass());
105 Shell* shell_;
106 scoped_ptr<ViewManagerClientFactory> view_manager_client_factory_;
108 typedef std::map<Id, Window*> WindowMap;
109 WindowMap windows_;
111 int next_color_;
113 DISALLOW_COPY_AND_ASSIGN(EmbeddedApp);
116 } // namespace examples
117 } // namespace mojo
119 MojoResult MojoMain(MojoHandle shell_handle) {
120 mojo::ApplicationRunnerChromium runner(new mojo::examples::EmbeddedApp);
121 return runner.Run(shell_handle);