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.h" | 8 #include "mojo/public/cpp/application/application.h" |
| 9 #include "mojo/services/public/cpp/view_manager/view_manager_types.h" |
9 #include "mojo/services/public/interfaces/launcher/launcher.mojom.h" | 10 #include "mojo/services/public/interfaces/launcher/launcher.mojom.h" |
10 #include "mojo/services/public/interfaces/network/network_service.mojom.h" | 11 #include "mojo/services/public/interfaces/network/network_service.mojom.h" |
11 #include "mojo/services/public/interfaces/network/url_loader.mojom.h" | 12 #include "mojo/services/public/interfaces/network/url_loader.mojom.h" |
12 | 13 |
13 namespace mojo { | 14 namespace mojo { |
14 namespace launcher { | 15 namespace launcher { |
15 | 16 |
16 class LauncherApp; | 17 class LauncherApp; |
17 | 18 |
18 class LauncherConnection : public InterfaceImpl<Launcher> { | 19 class LauncherConnection : public InterfaceImpl<Launcher> { |
19 public: | 20 public: |
20 explicit LauncherConnection(LauncherApp* app) : app_(app) {} | 21 explicit LauncherConnection(LauncherApp* app) : app_(app) {} |
21 virtual ~LauncherConnection() {} | 22 virtual ~LauncherConnection() {} |
22 | 23 |
23 private: | 24 private: |
24 // Overridden from Launcher: | 25 // Overridden from Launcher: |
25 virtual void Launch(const String& url) OVERRIDE; | 26 virtual void Launch(const String& url) OVERRIDE; |
26 | 27 |
27 LauncherApp* app_; | 28 LauncherApp* app_; |
28 | 29 |
29 DISALLOW_COPY_AND_ASSIGN(LauncherConnection); | 30 DISALLOW_COPY_AND_ASSIGN(LauncherConnection); |
30 }; | 31 }; |
31 | 32 |
32 class Launch : public URLLoaderClient { | 33 class LaunchInstance : public URLLoaderClient { |
33 public: | 34 public: |
34 Launch(LauncherApp* app, const String& url); | 35 LaunchInstance(LauncherApp* app, |
35 virtual ~Launch() {} | 36 LauncherClient* client, |
| 37 const String& url); |
| 38 virtual ~LaunchInstance() {} |
36 | 39 |
37 private: | 40 private: |
38 // Overridden from URLLoaderClient: | 41 // Overridden from URLLoaderClient: |
39 virtual void OnReceivedRedirect(URLResponsePtr response, | 42 virtual void OnReceivedRedirect(URLResponsePtr response, |
40 const String& new_url, | 43 const String& new_url, |
41 const String& new_method) OVERRIDE { | 44 const String& new_method) OVERRIDE { |
42 } | 45 } |
43 virtual void OnReceivedResponse(URLResponsePtr response) OVERRIDE; | 46 virtual void OnReceivedResponse(URLResponsePtr response) OVERRIDE; |
44 virtual void OnReceivedError(NetworkErrorPtr error) OVERRIDE { | 47 virtual void OnReceivedError(NetworkErrorPtr error) OVERRIDE { |
45 ScheduleDestroy(); | 48 ScheduleDestroy(); |
(...skipping 19 matching lines...) Expand all Loading... |
65 | 68 |
66 void ScheduleDestroy() { | 69 void ScheduleDestroy() { |
67 if (destroy_scheduled_) | 70 if (destroy_scheduled_) |
68 return; | 71 return; |
69 destroy_scheduled_ = true; | 72 destroy_scheduled_ = true; |
70 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | 73 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
71 } | 74 } |
72 | 75 |
73 LauncherApp* app_; | 76 LauncherApp* app_; |
74 bool destroy_scheduled_; | 77 bool destroy_scheduled_; |
| 78 LauncherClient* client_; |
75 URLLoaderPtr url_loader_; | 79 URLLoaderPtr url_loader_; |
76 ScopedDataPipeConsumerHandle response_body_stream_; | 80 ScopedDataPipeConsumerHandle response_body_stream_; |
77 LaunchablePtr launchable_; | |
78 | 81 |
79 DISALLOW_COPY_AND_ASSIGN(Launch); | 82 DISALLOW_COPY_AND_ASSIGN(LaunchInstance); |
80 }; | 83 }; |
81 | 84 |
82 class LauncherApp : public Application { | 85 class LauncherApp : public Application { |
83 public: | 86 public: |
84 LauncherApp() { | 87 LauncherApp() { |
85 handler_map_["text/html"] = "mojo:mojo_html_viewer"; | 88 handler_map_["text/html"] = "mojo:mojo_html_viewer"; |
86 handler_map_["image/png"] = "mojo:mojo_image_viewer"; | 89 handler_map_["image/png"] = "mojo:mojo_image_viewer"; |
87 } | 90 } |
88 virtual ~LauncherApp() {} | 91 virtual ~LauncherApp() {} |
89 | 92 |
90 void LaunchURL(const String& url) { | |
91 new Launch(this, url); | |
92 } | |
93 | |
94 URLLoaderPtr CreateURLLoader() { | 93 URLLoaderPtr CreateURLLoader() { |
95 URLLoaderPtr loader; | 94 URLLoaderPtr loader; |
96 network_service_->CreateURLLoader(Get(&loader)); | 95 network_service_->CreateURLLoader(Get(&loader)); |
97 return loader.Pass(); | 96 return loader.Pass(); |
98 } | 97 } |
99 | 98 |
100 std::string GetHandlerForContentType(const std::string& content_type) { | 99 std::string GetHandlerForContentType(const std::string& content_type) { |
101 HandlerMap::const_iterator it = handler_map_.find(content_type); | 100 HandlerMap::const_iterator it = handler_map_.find(content_type); |
102 return it != handler_map_.end() ? it->second : ""; | 101 return it != handler_map_.end() ? it->second : ""; |
103 } | 102 } |
104 | 103 |
105 private: | 104 private: |
106 typedef std::map<std::string, std::string> HandlerMap; | 105 typedef std::map<std::string, std::string> HandlerMap; |
107 | 106 |
108 // Overridden from Application: | 107 // Overridden from Application: |
109 virtual void Initialize() OVERRIDE { | 108 virtual void Initialize() OVERRIDE { |
110 AddService<LauncherConnection>(this); | 109 AddService<LauncherConnection>(this); |
111 ConnectTo("mojo:mojo_network_service", &network_service_); | 110 ConnectTo("mojo:mojo_network_service", &network_service_); |
112 } | 111 } |
113 | 112 |
114 HandlerMap handler_map_; | 113 HandlerMap handler_map_; |
115 | 114 |
116 NetworkServicePtr network_service_; | 115 NetworkServicePtr network_service_; |
117 | 116 |
118 DISALLOW_COPY_AND_ASSIGN(LauncherApp); | 117 DISALLOW_COPY_AND_ASSIGN(LauncherApp); |
119 }; | 118 }; |
120 | 119 |
121 void LauncherConnection::Launch(const String& url) { | 120 void LauncherConnection::Launch(const String& url) { |
122 app_->LaunchURL(url); | 121 new LaunchInstance(app_, client(), url); |
123 } | 122 } |
124 | 123 |
125 Launch::Launch(LauncherApp* app, const String& url) | 124 LaunchInstance::LaunchInstance(LauncherApp* app, |
| 125 LauncherClient* client, |
| 126 const String& url) |
126 : app_(app), | 127 : app_(app), |
127 destroy_scheduled_(false) { | 128 destroy_scheduled_(false), |
| 129 client_(client) { |
128 url_loader_ = app_->CreateURLLoader(); | 130 url_loader_ = app_->CreateURLLoader(); |
129 url_loader_.set_client(this); | 131 url_loader_.set_client(this); |
130 | 132 |
131 URLRequestPtr request(URLRequest::New()); | 133 URLRequestPtr request(URLRequest::New()); |
132 request->url = url; | 134 request->url = url; |
133 request->method = "GET"; | 135 request->method = "GET"; |
134 request->auto_follow_redirects = true; | 136 request->auto_follow_redirects = true; |
135 | 137 |
136 DataPipe data_pipe; | 138 DataPipe data_pipe; |
137 response_body_stream_ = data_pipe.consumer_handle.Pass(); | 139 response_body_stream_ = data_pipe.consumer_handle.Pass(); |
138 | 140 |
139 url_loader_->Start(request.Pass(), data_pipe.producer_handle.Pass()); | 141 url_loader_->Start(request.Pass(), data_pipe.producer_handle.Pass()); |
140 } | 142 } |
141 | 143 |
142 void Launch::OnReceivedResponse(URLResponsePtr response) { | 144 void LaunchInstance::OnReceivedResponse(URLResponsePtr response) { |
143 std::string content_type = GetContentType(response->headers); | 145 std::string content_type = GetContentType(response->headers); |
144 std::string handler_url = app_->GetHandlerForContentType(content_type); | 146 std::string handler_url = app_->GetHandlerForContentType(content_type); |
145 if (!handler_url.empty()) { | 147 if (!handler_url.empty()) { |
146 app_->ConnectTo(handler_url, &launchable_); | 148 client_->OnLaunch(handler_url, |
147 launchable_->OnLaunch(response.Pass(), response_body_stream_.Pass()); | 149 response.Pass(), |
| 150 response_body_stream_.Pass()); |
148 } | 151 } |
149 ScheduleDestroy(); | |
150 } | 152 } |
151 | 153 |
152 } // namespace launcher | 154 } // namespace launcher |
153 | 155 |
154 // static | 156 // static |
155 Application* Application::Create() { | 157 Application* Application::Create() { |
156 return new launcher::LauncherApp; | 158 return new launcher::LauncherApp; |
157 } | 159 } |
158 | 160 |
159 } // namespace mojo | 161 } // namespace mojo |
OLD | NEW |