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.
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"
26 #include "url/url_util.h"
31 const SkColor kColors
[] = {SK_ColorYELLOW
, SK_ColorRED
, SK_ColorGREEN
,
36 scoped_ptr
<ServiceProvider
> embedder_service_provider
,
39 embedder_service_provider(embedder_service_provider
.Pass()),
40 bitmap_uploader(root
) {
41 bitmap_uploader
.Init(shell
);
45 scoped_ptr
<ServiceProvider
> embedder_service_provider
;
46 BitmapUploader bitmap_uploader
;
50 : public ApplicationDelegate
,
51 public ViewManagerDelegate
,
54 EmbeddedApp() : shell_(nullptr) { url::AddStandardScheme("mojo"); }
55 virtual ~EmbeddedApp() {}
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());
72 // Overridden from ViewManagerDelegate:
73 virtual void OnEmbed(ViewManager
* view_manager
,
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(),
100 navigator_host
->RequestNavigate(TARGET_SOURCE_NODE
, request
.Pass());
106 scoped_ptr
<ViewManagerClientFactory
> view_manager_client_factory_
;
108 typedef std::map
<Id
, Window
*> WindowMap
;
113 DISALLOW_COPY_AND_ASSIGN(EmbeddedApp
);
116 } // namespace examples
119 MojoResult
MojoMain(MojoHandle shell_handle
) {
120 mojo::ApplicationRunnerChromium
runner(new mojo::examples::EmbeddedApp
);
121 return runner
.Run(shell_handle
);