| 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/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/compiler_specific.h" | 6 #include "base/compiler_specific.h" |
| 7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 8 #include "base/strings/string_tokenizer.h" | 8 #include "base/strings/string_tokenizer.h" |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "mojo/public/cpp/application/application_connection.h" | 10 #include "mojo/public/cpp/application/application_connection.h" |
| 11 #include "mojo/public/cpp/application/application_delegate.h" | 11 #include "mojo/public/cpp/application/application_delegate.h" |
| 12 #include "mojo/public/cpp/application/application_impl.h" | 12 #include "mojo/public/cpp/application/application_impl.h" |
| 13 #include "mojo/public/cpp/application/interface_factory_with_context.h" | 13 #include "mojo/public/cpp/application/interface_factory_with_context.h" |
| 14 #include "mojo/services/public/cpp/view_manager/types.h" | 14 #include "mojo/services/public/cpp/view_manager/types.h" |
| 15 #include "mojo/services/public/interfaces/launcher/launcher.mojom.h" | 15 #include "mojo/services/public/interfaces/launcher/launcher.mojom.h" |
| 16 #include "mojo/services/public/interfaces/network/network_service.mojom.h" | 16 #include "mojo/services/public/interfaces/network/network_service.mojom.h" |
| 17 #include "mojo/services/public/interfaces/network/url_loader.mojom.h" | 17 #include "mojo/services/public/interfaces/network/url_loader.mojom.h" |
| 18 #include "url/gurl.h" | 18 #include "url/gurl.h" |
| 19 | 19 |
| 20 namespace mojo { | 20 namespace mojo { |
| 21 namespace launcher { | |
| 22 | 21 |
| 23 namespace { | 22 namespace { |
| 24 | 23 |
| 25 typedef mojo::Callback<void(String handler_url, String view_url, | 24 typedef mojo::Callback<void(String handler_url, String view_url, |
| 26 navigation::ResponseDetailsPtr response)> LaunchCallback; | 25 ResponseDetailsPtr response)> LaunchCallback; |
| 27 | 26 |
| 28 } | 27 } |
| 29 | 28 |
| 30 class LauncherApp; | 29 class LauncherApp; |
| 31 | 30 |
| 32 class LauncherConnection : public InterfaceImpl<Launcher> { | 31 class LauncherConnection : public InterfaceImpl<Launcher> { |
| 33 public: | 32 public: |
| 34 explicit LauncherConnection(LauncherApp* app) : app_(app) {} | 33 explicit LauncherConnection(LauncherApp* app) : app_(app) {} |
| 35 virtual ~LauncherConnection() {} | 34 virtual ~LauncherConnection() {} |
| 36 | 35 |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 DISALLOW_COPY_AND_ASSIGN(LauncherApp); | 127 DISALLOW_COPY_AND_ASSIGN(LauncherApp); |
| 129 }; | 128 }; |
| 130 | 129 |
| 131 void LauncherConnection::Launch(const String& url_string, | 130 void LauncherConnection::Launch(const String& url_string, |
| 132 const LaunchCallback& callback) { | 131 const LaunchCallback& callback) { |
| 133 GURL url(url_string.To<std::string>()); | 132 GURL url(url_string.To<std::string>()); |
| 134 | 133 |
| 135 // For Mojo URLs, the handler can always be found at the origin. | 134 // For Mojo URLs, the handler can always be found at the origin. |
| 136 // TODO(aa): Return error for invalid URL? | 135 // TODO(aa): Return error for invalid URL? |
| 137 if (url.is_valid() && url.SchemeIs("mojo")) { | 136 if (url.is_valid() && url.SchemeIs("mojo")) { |
| 138 callback.Run(url.GetOrigin().spec(), url_string, | 137 callback.Run(url.GetOrigin().spec(), url_string, ResponseDetailsPtr()); |
| 139 navigation::ResponseDetailsPtr()); | |
| 140 return; | 138 return; |
| 141 } | 139 } |
| 142 new LaunchInstance(app_, callback, url_string); | 140 new LaunchInstance(app_, callback, url_string); |
| 143 } | 141 } |
| 144 | 142 |
| 145 LaunchInstance::LaunchInstance(LauncherApp* app, | 143 LaunchInstance::LaunchInstance(LauncherApp* app, |
| 146 const LaunchCallback& callback, | 144 const LaunchCallback& callback, |
| 147 const String& url) | 145 const String& url) |
| 148 : app_(app), | 146 : app_(app), |
| 149 destroy_scheduled_(false), | 147 destroy_scheduled_(false), |
| 150 callback_(callback) { | 148 callback_(callback) { |
| 151 URLRequestPtr request(URLRequest::New()); | 149 URLRequestPtr request(URLRequest::New()); |
| 152 request->url = url; | 150 request->url = url; |
| 153 request->method = "GET"; | 151 request->method = "GET"; |
| 154 request->auto_follow_redirects = true; | 152 request->auto_follow_redirects = true; |
| 155 | 153 |
| 156 url_loader_ = app_->CreateURLLoader(); | 154 url_loader_ = app_->CreateURLLoader(); |
| 157 url_loader_->Start(request.Pass(), | 155 url_loader_->Start(request.Pass(), |
| 158 base::Bind(&LaunchInstance::OnReceivedResponse, | 156 base::Bind(&LaunchInstance::OnReceivedResponse, |
| 159 base::Unretained(this))); | 157 base::Unretained(this))); |
| 160 } | 158 } |
| 161 | 159 |
| 162 void LaunchInstance::OnReceivedResponse(URLResponsePtr response) { | 160 void LaunchInstance::OnReceivedResponse(URLResponsePtr response) { |
| 163 if (!response->error) { | 161 if (!response->error) { |
| 164 std::string content_type = GetContentType(response->headers); | 162 std::string content_type = GetContentType(response->headers); |
| 165 std::string handler_url = app_->GetHandlerForContentType(content_type); | 163 std::string handler_url = app_->GetHandlerForContentType(content_type); |
| 166 if (handler_url.empty()) { | 164 if (handler_url.empty()) { |
| 167 DLOG(WARNING) << "No handler for content type: " << content_type; | 165 DLOG(WARNING) << "No handler for content type: " << content_type; |
| 168 } else { | 166 } else { |
| 169 navigation::ResponseDetailsPtr nav_response( | 167 ResponseDetailsPtr nav_response(ResponseDetails::New()); |
| 170 navigation::ResponseDetails::New()); | |
| 171 nav_response->loader = url_loader_.Pass(); | 168 nav_response->loader = url_loader_.Pass(); |
| 172 nav_response->response = response.Pass(); | 169 nav_response->response = response.Pass(); |
| 173 String response_url = nav_response->response->url; | 170 String response_url = nav_response->response->url; |
| 174 callback_.Run(handler_url, response_url, nav_response.Pass()); | 171 callback_.Run(handler_url, response_url, nav_response.Pass()); |
| 175 } | 172 } |
| 176 } | 173 } |
| 177 ScheduleDestroy(); | 174 ScheduleDestroy(); |
| 178 } | 175 } |
| 179 | 176 |
| 180 } // namespace launcher | |
| 181 | |
| 182 // static | 177 // static |
| 183 ApplicationDelegate* ApplicationDelegate::Create() { | 178 ApplicationDelegate* ApplicationDelegate::Create() { |
| 184 return new launcher::LauncherApp; | 179 return new LauncherApp; |
| 185 } | 180 } |
| 186 | 181 |
| 187 } // namespace mojo | 182 } // namespace mojo |
| OLD | NEW |