Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(904)

Side by Side Diff: mojo/services/launcher/launcher.cc

Issue 549273002: Various mojom cleanup from ContentHandler changes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "base/bind.h"
6 #include "base/compiler_specific.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/strings/string_tokenizer.h"
9 #include "base/strings/string_util.h"
10 #include "mojo/public/c/system/main.h"
11 #include "mojo/public/cpp/application/application_connection.h"
12 #include "mojo/public/cpp/application/application_delegate.h"
13 #include "mojo/public/cpp/application/application_impl.h"
14 #include "mojo/public/cpp/application/application_runner_chromium.h"
15 #include "mojo/public/cpp/application/interface_factory_impl.h"
16 #include "mojo/services/public/cpp/view_manager/types.h"
17 #include "mojo/services/public/interfaces/launcher/launcher.mojom.h"
18 #include "mojo/services/public/interfaces/network/network_service.mojom.h"
19 #include "mojo/services/public/interfaces/network/url_loader.mojom.h"
20 #include "url/gurl.h"
21
22 namespace mojo {
23
24 namespace {
25
26 typedef mojo::Callback<void(String handler_url, String view_url,
27 ResponseDetailsPtr response)> LaunchCallback;
28
29 }
30
31 class LauncherApp;
32
33 class LauncherConnection : public InterfaceImpl<Launcher> {
34 public:
35 explicit LauncherConnection(LauncherApp* app) : app_(app) {}
36 virtual ~LauncherConnection() {}
37
38 private:
39 // Overridden from Launcher:
40 virtual void Launch(NavigationDetailsPtr nav_details,
41 const LaunchCallback& callback) OVERRIDE;
42
43 LauncherApp* app_;
44
45 DISALLOW_COPY_AND_ASSIGN(LauncherConnection);
46 };
47
48 class LaunchInstance {
49 public:
50 LaunchInstance(LauncherApp* app,
51 const LaunchCallback& callback,
52 NavigationDetailsPtr nav_details);
53 virtual ~LaunchInstance() {}
54
55 private:
56 void OnReceivedResponse(URLResponsePtr response);
57
58 std::string GetContentType(const Array<String>& headers) {
59 for (size_t i = 0; i < headers.size(); ++i) {
60 base::StringTokenizer t(headers[i], ": ;=");
61 while (t.GetNext()) {
62 if (!t.token_is_delim() &&
63 LowerCaseEqualsASCII(t.token(), "content-type")) {
64 while (t.GetNext()) {
65 if (!t.token_is_delim())
66 return t.token();
67 }
68 }
69 }
70 }
71 return "";
72 }
73
74 void ScheduleDestroy() {
75 if (destroy_scheduled_)
76 return;
77 destroy_scheduled_ = true;
78 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
79 }
80
81 LauncherApp* app_;
82 bool destroy_scheduled_;
83 const LaunchCallback callback_;
84 URLLoaderPtr url_loader_;
85 ScopedDataPipeConsumerHandle response_body_stream_;
86
87 DISALLOW_COPY_AND_ASSIGN(LaunchInstance);
88 };
89
90 class LauncherApp : public ApplicationDelegate {
91 public:
92 LauncherApp() : launcher_connection_factory_(this) {
93 handler_map_["text/html"] = "mojo:mojo_html_viewer";
94 handler_map_["image/png"] = "mojo:mojo_media_viewer";
95 }
96 virtual ~LauncherApp() {}
97
98 URLLoaderPtr CreateURLLoader() {
99 URLLoaderPtr loader;
100 network_service_->CreateURLLoader(Get(&loader));
101 return loader.Pass();
102 }
103
104 std::string GetHandlerForContentType(const std::string& content_type) {
105 HandlerMap::const_iterator it = handler_map_.find(content_type);
106 return it != handler_map_.end() ? it->second : "";
107 }
108
109 private:
110 typedef std::map<std::string, std::string> HandlerMap;
111
112 // Overridden from ApplicationDelegate:
113 virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE {
114 app->ConnectToService("mojo:mojo_network_service", &network_service_);
115 }
116
117 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection)
118 MOJO_OVERRIDE {
119 connection->AddService(&launcher_connection_factory_);
120 return true;
121 }
122
123 InterfaceFactoryImplWithContext<LauncherConnection, LauncherApp>
124 launcher_connection_factory_;
125
126 HandlerMap handler_map_;
127 NetworkServicePtr network_service_;
128
129 DISALLOW_COPY_AND_ASSIGN(LauncherApp);
130 };
131
132 void LauncherConnection::Launch(NavigationDetailsPtr nav_details,
133 const LaunchCallback& callback) {
134 GURL url(nav_details->request->url.To<std::string>());
135
136 // For Mojo URLs, the handler can always be found at the origin.
137 // TODO(aa): Return error for invalid URL?
138 if (url.is_valid() && url.SchemeIs("mojo")) {
139 callback.Run(url.GetOrigin().spec(),
140 nav_details->request->url,
141 ResponseDetailsPtr());
142 return;
143 }
144 new LaunchInstance(app_, callback, nav_details.Pass());
145 }
146
147 LaunchInstance::LaunchInstance(LauncherApp* app,
148 const LaunchCallback& callback,
149 NavigationDetailsPtr nav_details)
150 : app_(app),
151 destroy_scheduled_(false),
152 callback_(callback) {
153 URLRequestPtr request = nav_details->request.Pass();
154 request->auto_follow_redirects = true;
155
156 url_loader_ = app_->CreateURLLoader();
157 url_loader_->Start(request.Pass(),
158 base::Bind(&LaunchInstance::OnReceivedResponse,
159 base::Unretained(this)));
160 }
161
162 void LaunchInstance::OnReceivedResponse(URLResponsePtr response) {
163 if (!response->error) {
164 std::string content_type = GetContentType(response->headers);
165 std::string handler_url = app_->GetHandlerForContentType(content_type);
166 if (handler_url.empty()) {
167 DLOG(WARNING) << "No handler for content type: " << content_type;
168 } else {
169 ResponseDetailsPtr nav_response(ResponseDetails::New());
170 nav_response->loader = url_loader_.Pass();
171 nav_response->response = response.Pass();
172 String response_url = nav_response->response->url;
173 callback_.Run(handler_url, response_url, nav_response.Pass());
174 }
175 }
176 ScheduleDestroy();
177 }
178
179 } // namespace mojo
180
181 MojoResult MojoMain(MojoHandle shell_handle) {
182 mojo::ApplicationRunnerChromium runner(new mojo::LauncherApp);
183 return runner.Run(shell_handle);
184 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698