OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <utility> | 5 #include <utility> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "mojo/application/application_runner_chromium.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "mojo/common/binding_set.h" | 10 #include "mojo/common/binding_set.h" |
11 #include "mojo/public/c/system/main.h" | 11 #include "mojo/public/c/system/main.h" |
12 #include "mojo/public/cpp/application/application_delegate.h" | 12 #include "mojo/public/cpp/application/application_impl_base.h" |
13 #include "mojo/public/cpp/application/application_impl.h" | |
14 #include "mojo/public/cpp/application/connect.h" | 13 #include "mojo/public/cpp/application/connect.h" |
| 14 #include "mojo/public/cpp/application/run_application.h" |
15 #include "mojo/services/notifications/interfaces/notifications.mojom.h" | 15 #include "mojo/services/notifications/interfaces/notifications.mojom.h" |
16 | 16 |
17 namespace examples { | 17 namespace examples { |
18 | 18 |
19 static const base::TimeDelta kDefaultMessageDelay = | 19 static const base::TimeDelta kDefaultMessageDelay = |
20 base::TimeDelta::FromMilliseconds(3000); | 20 base::TimeDelta::FromMilliseconds(3000); |
21 | 21 |
22 class NotificationGeneratorDelegate : public mojo::ApplicationDelegate, | 22 class NotificationGeneratorApp : public mojo::ApplicationImplBase, |
23 public notifications::NotificationClient { | 23 public notifications::NotificationClient { |
24 public: | 24 public: |
25 NotificationGeneratorDelegate() {} | 25 NotificationGeneratorApp() {} |
26 | 26 |
27 ~NotificationGeneratorDelegate() override {} | 27 ~NotificationGeneratorApp() override {} |
28 | 28 |
29 // mojo::ApplicationDelegate implementation. | 29 // mojo::ApplicationImplBase implementation. |
30 void Initialize(mojo::ApplicationImpl* app) override { | 30 void OnInitialize() override { |
31 mojo::ConnectToService(app->shell(), "mojo:notifications", | 31 mojo::ConnectToService(shell(), "mojo:notifications", |
32 GetProxy(¬ification_service_)); | 32 GetProxy(¬ification_service_)); |
33 PostFirstNotification(); | 33 PostFirstNotification(); |
34 } | 34 } |
35 | 35 |
36 // notifications::NotificationClient implementation. | 36 // notifications::NotificationClient implementation. |
37 void OnSelected() override { | 37 void OnSelected() override { |
38 PostNotification("You selected a notification!", | 38 PostNotification("You selected a notification!", |
39 "Have you dismissed one yet?", &select_notification_); | 39 "Have you dismissed one yet?", &select_notification_); |
40 } | 40 } |
41 | 41 |
42 void OnDismissed() override { | 42 void OnDismissed() override { |
43 PostNotification("You dismissed a notification!", | 43 PostNotification("You dismissed a notification!", |
44 "Have you selected one yet?", &dismissed_notification_); | 44 "Have you selected one yet?", &dismissed_notification_); |
45 } | 45 } |
46 | 46 |
47 void PostFirstNotification() { | 47 void PostFirstNotification() { |
48 PostNotification("First notification", "Next: Second will be created", | 48 PostNotification("First notification", "Next: Second will be created", |
49 &first_notification_); | 49 &first_notification_); |
50 PostDelayed( | 50 PostDelayed(base::Bind(&NotificationGeneratorApp::PostSecondNotification, |
51 base::Bind(&NotificationGeneratorDelegate::PostSecondNotification, | 51 base::Unretained(this))); |
52 base::Unretained(this))); | |
53 } | 52 } |
54 | 53 |
55 void PostSecondNotification() { | 54 void PostSecondNotification() { |
56 PostNotification("Second notification", "Next: First will be updated", | 55 PostNotification("Second notification", "Next: First will be updated", |
57 &second_notification_); | 56 &second_notification_); |
58 PostDelayed( | 57 PostDelayed(base::Bind(&NotificationGeneratorApp::UpdateFirstNotification, |
59 base::Bind(&NotificationGeneratorDelegate::UpdateFirstNotification, | 58 base::Unretained(this))); |
60 base::Unretained(this))); | |
61 } | 59 } |
62 | 60 |
63 void PostNotification(const char* title, | 61 void PostNotification(const char* title, |
64 const char* text, | 62 const char* text, |
65 notifications::NotificationPtr* notification) { | 63 notifications::NotificationPtr* notification) { |
66 mojo::InterfaceHandle<notifications::NotificationClient> | 64 mojo::InterfaceHandle<notifications::NotificationClient> |
67 notification_client; | 65 notification_client; |
68 auto request = mojo::GetProxy(¬ification_client); | 66 auto request = mojo::GetProxy(¬ification_client); |
69 client_bindings_.AddBinding(this, request.Pass()); | 67 client_bindings_.AddBinding(this, request.Pass()); |
70 notification_service_->Post(CreateNotificationData(title, text).Pass(), | 68 notification_service_->Post(CreateNotificationData(title, text).Pass(), |
71 std::move(notification_client), | 69 std::move(notification_client), |
72 GetProxy(notification)); | 70 GetProxy(notification)); |
73 } | 71 } |
74 | 72 |
75 void UpdateFirstNotification() { | 73 void UpdateFirstNotification() { |
76 first_notification_->Update( | 74 first_notification_->Update( |
77 CreateNotificationData("First notification updated", | 75 CreateNotificationData("First notification updated", |
78 "Next: both cancelled; repeat").Pass()); | 76 "Next: both cancelled; repeat").Pass()); |
79 PostDelayed( | 77 PostDelayed(base::Bind(&NotificationGeneratorApp::CancelSecondNotification, |
80 base::Bind(&NotificationGeneratorDelegate::CancelSecondNotification, | 78 base::Unretained(this))); |
81 base::Unretained(this))); | |
82 } | 79 } |
83 | 80 |
84 void CancelSecondNotification() { | 81 void CancelSecondNotification() { |
85 second_notification_->Cancel(); | 82 second_notification_->Cancel(); |
86 PostDelayed( | 83 PostDelayed(base::Bind(&NotificationGeneratorApp::CancelFirstNotification, |
87 base::Bind(&NotificationGeneratorDelegate::CancelFirstNotification, | 84 base::Unretained(this))); |
88 base::Unretained(this))); | |
89 } | 85 } |
90 | 86 |
91 void CancelFirstNotification() { | 87 void CancelFirstNotification() { |
92 first_notification_->Cancel(); | 88 first_notification_->Cancel(); |
93 PostDelayed( | 89 PostDelayed(base::Bind(&NotificationGeneratorApp::PostFirstNotification, |
94 base::Bind(&NotificationGeneratorDelegate::PostFirstNotification, | 90 base::Unretained(this))); |
95 base::Unretained(this))); | |
96 } | 91 } |
97 | 92 |
98 notifications::NotificationDataPtr CreateNotificationData(const char* title, | 93 notifications::NotificationDataPtr CreateNotificationData(const char* title, |
99 const char* text) { | 94 const char* text) { |
100 notifications::NotificationDataPtr notification_data = | 95 notifications::NotificationDataPtr notification_data = |
101 notifications::NotificationData::New(); | 96 notifications::NotificationData::New(); |
102 notification_data->title = mojo::String(title); | 97 notification_data->title = mojo::String(title); |
103 notification_data->text = mojo::String(text); | 98 notification_data->text = mojo::String(text); |
104 return notification_data; | 99 return notification_data; |
105 } | 100 } |
106 | 101 |
107 void PostDelayed(base::Closure closure) { | 102 void PostDelayed(base::Closure closure) { |
108 base::MessageLoop::current()->PostDelayedTask(FROM_HERE, closure, | 103 base::MessageLoop::current()->PostDelayedTask(FROM_HERE, closure, |
109 kDefaultMessageDelay); | 104 kDefaultMessageDelay); |
110 } | 105 } |
111 | 106 |
112 private: | 107 private: |
113 notifications::NotificationServicePtr notification_service_; | 108 notifications::NotificationServicePtr notification_service_; |
114 mojo::BindingSet<notifications::NotificationClient> client_bindings_; | 109 mojo::BindingSet<notifications::NotificationClient> client_bindings_; |
115 notifications::NotificationPtr first_notification_; | 110 notifications::NotificationPtr first_notification_; |
116 notifications::NotificationPtr second_notification_; | 111 notifications::NotificationPtr second_notification_; |
117 notifications::NotificationPtr dismissed_notification_; | 112 notifications::NotificationPtr dismissed_notification_; |
118 notifications::NotificationPtr select_notification_; | 113 notifications::NotificationPtr select_notification_; |
119 | 114 |
120 DISALLOW_COPY_AND_ASSIGN(NotificationGeneratorDelegate); | 115 DISALLOW_COPY_AND_ASSIGN(NotificationGeneratorApp); |
121 }; | 116 }; |
122 | 117 |
123 } // namespace examples | 118 } // namespace examples |
124 | 119 |
125 MojoResult MojoMain(MojoHandle application_request) { | 120 MojoResult MojoMain(MojoHandle application_request) { |
126 mojo::ApplicationRunnerChromium runner( | 121 examples::NotificationGeneratorApp notification_generator_app; |
127 new examples::NotificationGeneratorDelegate); | 122 return mojo::RunMainApplication(application_request, |
128 return runner.Run(application_request); | 123 ¬ification_generator_app); |
129 } | 124 } |
OLD | NEW |