OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "base/compiler_specific.h" | 5 #include "base/compiler_specific.h" |
6 #include "base/message_loop/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
7 #include "base/strings/string_tokenizer.h" | 7 #include "base/strings/string_tokenizer.h" |
8 #include "mojo/public/cpp/application/application_connection.h" | 8 #include "mojo/public/cpp/application/application_connection.h" |
9 #include "mojo/public/cpp/application/application_delegate.h" | 9 #include "mojo/public/cpp/application/application_delegate.h" |
10 #include "mojo/public/cpp/application/application_impl.h" | 10 #include "mojo/public/cpp/application/application_impl.h" |
11 #include "mojo/services/public/cpp/view_manager/types.h" | 11 #include "mojo/services/public/cpp/view_manager/types.h" |
12 #include "mojo/services/public/interfaces/launcher/launcher.mojom.h" | 12 #include "mojo/services/public/interfaces/launcher/launcher.mojom.h" |
13 #include "mojo/services/public/interfaces/network/network_service.mojom.h" | 13 #include "mojo/services/public/interfaces/network/network_service.mojom.h" |
14 #include "mojo/services/public/interfaces/network/url_loader.mojom.h" | 14 #include "mojo/services/public/interfaces/network/url_loader.mojom.h" |
15 #include "url/gurl.h" | 15 #include "url/gurl.h" |
16 | 16 |
17 namespace mojo { | 17 namespace mojo { |
18 namespace launcher { | 18 namespace launcher { |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 typedef mojo::Callback<void(String handler_url, String view_url, | 22 typedef mojo::Callback<void(String handler_url, String view_url, |
23 navigation::ResponseDetailsPtr response)> LaunchCallback; | 23 navigation::ResponseDetailsPtr response)> LaunchCallback; |
24 | 24 |
25 } | 25 } |
26 | 26 |
27 class LauncherApp; | 27 class LauncherApp; |
28 | 28 |
29 class LauncherConnection : public InterfaceImpl<Launcher> { | 29 class LauncherConnection : public InterfaceImplDeleteOnError<Launcher> { |
30 public: | 30 public: |
31 LauncherConnection(ApplicationConnection* connection, LauncherApp* app) | 31 explicit LauncherConnection(LauncherApp* app) : app_(app) {} |
32 : app_(app) {} | |
33 virtual ~LauncherConnection() {} | 32 virtual ~LauncherConnection() {} |
34 | 33 |
35 private: | 34 private: |
36 // Overridden from Launcher: | 35 // Overridden from Launcher: |
37 virtual void Launch(const String& url, | 36 virtual void Launch(const String& url, |
38 const LaunchCallback& callback) OVERRIDE; | 37 const LaunchCallback& callback) OVERRIDE; |
39 | 38 |
40 LauncherApp* app_; | 39 LauncherApp* app_; |
41 | 40 |
42 DISALLOW_COPY_AND_ASSIGN(LauncherConnection); | 41 DISALLOW_COPY_AND_ASSIGN(LauncherConnection); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 | 86 |
88 LauncherApp* app_; | 87 LauncherApp* app_; |
89 bool destroy_scheduled_; | 88 bool destroy_scheduled_; |
90 const LaunchCallback callback_; | 89 const LaunchCallback callback_; |
91 URLLoaderPtr url_loader_; | 90 URLLoaderPtr url_loader_; |
92 ScopedDataPipeConsumerHandle response_body_stream_; | 91 ScopedDataPipeConsumerHandle response_body_stream_; |
93 | 92 |
94 DISALLOW_COPY_AND_ASSIGN(LaunchInstance); | 93 DISALLOW_COPY_AND_ASSIGN(LaunchInstance); |
95 }; | 94 }; |
96 | 95 |
97 class LauncherApp : public ApplicationDelegate { | 96 class LauncherApp : public ApplicationDelegate, |
| 97 public InterfaceProvider<Launcher> { |
98 public: | 98 public: |
99 LauncherApp() { | 99 LauncherApp() { |
100 handler_map_["text/html"] = "mojo:mojo_html_viewer"; | 100 handler_map_["text/html"] = "mojo:mojo_html_viewer"; |
101 handler_map_["image/png"] = "mojo:mojo_media_viewer"; | 101 handler_map_["image/png"] = "mojo:mojo_media_viewer"; |
102 } | 102 } |
103 virtual ~LauncherApp() {} | 103 virtual ~LauncherApp() {} |
104 | 104 |
105 URLLoaderPtr CreateURLLoader() { | 105 URLLoaderPtr CreateURLLoader() { |
106 URLLoaderPtr loader; | 106 URLLoaderPtr loader; |
107 network_service_->CreateURLLoader(Get(&loader)); | 107 network_service_->CreateURLLoader(Get(&loader)); |
108 return loader.Pass(); | 108 return loader.Pass(); |
109 } | 109 } |
110 | 110 |
111 std::string GetHandlerForContentType(const std::string& content_type) { | 111 std::string GetHandlerForContentType(const std::string& content_type) { |
112 HandlerMap::const_iterator it = handler_map_.find(content_type); | 112 HandlerMap::const_iterator it = handler_map_.find(content_type); |
113 return it != handler_map_.end() ? it->second : ""; | 113 return it != handler_map_.end() ? it->second : ""; |
114 } | 114 } |
115 | 115 |
116 private: | 116 private: |
117 typedef std::map<std::string, std::string> HandlerMap; | 117 typedef std::map<std::string, std::string> HandlerMap; |
118 | 118 |
119 // Overridden from ApplicationDelegate: | 119 // Overridden from ApplicationDelegate: |
120 virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE { | 120 virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE { |
121 app->ConnectToService("mojo:mojo_network_service", &network_service_); | 121 app->ConnectToService("mojo:mojo_network_service", &network_service_); |
122 } | 122 } |
123 | 123 |
124 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) | 124 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) |
125 MOJO_OVERRIDE { | 125 MOJO_OVERRIDE { |
126 connection->AddService<LauncherConnection>(this); | 126 connection->AddServiceProvider(this); |
127 return true; | 127 return true; |
128 } | 128 } |
129 | 129 |
| 130 // Overridden from InterfaceProvider<Launcher> |
| 131 virtual void BindToRequest(ApplicationConnection* connection, |
| 132 InterfaceRequest<Launcher> request) OVERRIDE { |
| 133 mojo::BindToRequest(new LauncherConnection(this), &request); |
| 134 } |
| 135 |
130 HandlerMap handler_map_; | 136 HandlerMap handler_map_; |
131 | 137 |
132 NetworkServicePtr network_service_; | 138 NetworkServicePtr network_service_; |
133 | 139 |
134 DISALLOW_COPY_AND_ASSIGN(LauncherApp); | 140 DISALLOW_COPY_AND_ASSIGN(LauncherApp); |
135 }; | 141 }; |
136 | 142 |
137 void LauncherConnection::Launch(const String& url_string, | 143 void LauncherConnection::Launch(const String& url_string, |
138 const LaunchCallback& callback) { | 144 const LaunchCallback& callback) { |
139 GURL url(url_string.To<std::string>()); | 145 GURL url(url_string.To<std::string>()); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 } | 188 } |
183 | 189 |
184 } // namespace launcher | 190 } // namespace launcher |
185 | 191 |
186 // static | 192 // static |
187 ApplicationDelegate* ApplicationDelegate::Create() { | 193 ApplicationDelegate* ApplicationDelegate::Create() { |
188 return new launcher::LauncherApp; | 194 return new launcher::LauncherApp; |
189 } | 195 } |
190 | 196 |
191 } // namespace mojo | 197 } // namespace mojo |
OLD | NEW |