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

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

Issue 373373002: Mojo: Refactor URLLoader interface. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fixes per review Created 6 years, 5 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 | Annotate | Revision Log
« no previous file with comments | « mojo/examples/wget/wget.cc ('k') | mojo/services/network/url_loader_impl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/compiler_specific.h" 6 #include "base/compiler_specific.h"
6 #include "base/message_loop/message_loop.h" 7 #include "base/message_loop/message_loop.h"
7 #include "base/strings/string_tokenizer.h" 8 #include "base/strings/string_tokenizer.h"
9 #include "base/strings/string_util.h"
8 #include "mojo/public/cpp/application/application_connection.h" 10 #include "mojo/public/cpp/application/application_connection.h"
9 #include "mojo/public/cpp/application/application_delegate.h" 11 #include "mojo/public/cpp/application/application_delegate.h"
10 #include "mojo/public/cpp/application/application_impl.h" 12 #include "mojo/public/cpp/application/application_impl.h"
11 #include "mojo/services/public/cpp/view_manager/types.h" 13 #include "mojo/services/public/cpp/view_manager/types.h"
12 #include "mojo/services/public/interfaces/launcher/launcher.mojom.h" 14 #include "mojo/services/public/interfaces/launcher/launcher.mojom.h"
13 #include "mojo/services/public/interfaces/network/network_service.mojom.h" 15 #include "mojo/services/public/interfaces/network/network_service.mojom.h"
14 #include "mojo/services/public/interfaces/network/url_loader.mojom.h" 16 #include "mojo/services/public/interfaces/network/url_loader.mojom.h"
15 #include "url/gurl.h" 17 #include "url/gurl.h"
16 18
17 namespace mojo { 19 namespace mojo {
(...skipping 17 matching lines...) Expand all
35 private: 37 private:
36 // Overridden from Launcher: 38 // Overridden from Launcher:
37 virtual void Launch(const String& url, 39 virtual void Launch(const String& url,
38 const LaunchCallback& callback) OVERRIDE; 40 const LaunchCallback& callback) OVERRIDE;
39 41
40 LauncherApp* app_; 42 LauncherApp* app_;
41 43
42 DISALLOW_COPY_AND_ASSIGN(LauncherConnection); 44 DISALLOW_COPY_AND_ASSIGN(LauncherConnection);
43 }; 45 };
44 46
45 class LaunchInstance : public URLLoaderClient { 47 class LaunchInstance {
46 public: 48 public:
47 LaunchInstance(LauncherApp* app, 49 LaunchInstance(LauncherApp* app,
48 const LaunchCallback& callback, 50 const LaunchCallback& callback,
49 const String& url); 51 const String& url);
50 virtual ~LaunchInstance() {} 52 virtual ~LaunchInstance() {}
51 53
52 private: 54 private:
53 // Overridden from URLLoaderClient: 55 void OnReceivedResponse(URLResponsePtr response);
54 virtual void OnReceivedRedirect(URLResponsePtr response,
55 const String& new_url,
56 const String& new_method) OVERRIDE {
57 }
58 virtual void OnReceivedResponse(URLResponsePtr response) OVERRIDE;
59 virtual void OnReceivedError(NetworkErrorPtr error) OVERRIDE {
60 ScheduleDestroy();
61 }
62 virtual void OnReceivedEndOfResponseBody() OVERRIDE {
63 ScheduleDestroy();
64 }
65 56
66 std::string GetContentType(const Array<String>& headers) { 57 std::string GetContentType(const Array<String>& headers) {
67 for (size_t i = 0; i < headers.size(); ++i) { 58 for (size_t i = 0; i < headers.size(); ++i) {
68 base::StringTokenizer t(headers[i], ": ;="); 59 base::StringTokenizer t(headers[i], ": ;=");
69 while (t.GetNext()) { 60 while (t.GetNext()) {
70 if (!t.token_is_delim() && t.token() == "Content-Type") { 61 if (!t.token_is_delim() &&
62 LowerCaseEqualsASCII(t.token(), "content-type")) {
71 while (t.GetNext()) { 63 while (t.GetNext()) {
72 if (!t.token_is_delim()) 64 if (!t.token_is_delim())
73 return t.token(); 65 return t.token();
74 } 66 }
75 } 67 }
76 } 68 }
77 } 69 }
78 return ""; 70 return "";
79 } 71 }
80 72
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 } 139 }
148 new LaunchInstance(app_, callback, url_string); 140 new LaunchInstance(app_, callback, url_string);
149 } 141 }
150 142
151 LaunchInstance::LaunchInstance(LauncherApp* app, 143 LaunchInstance::LaunchInstance(LauncherApp* app,
152 const LaunchCallback& callback, 144 const LaunchCallback& callback,
153 const String& url) 145 const String& url)
154 : app_(app), 146 : app_(app),
155 destroy_scheduled_(false), 147 destroy_scheduled_(false),
156 callback_(callback) { 148 callback_(callback) {
157 url_loader_ = app_->CreateURLLoader();
158 url_loader_.set_client(this);
159
160 URLRequestPtr request(URLRequest::New()); 149 URLRequestPtr request(URLRequest::New());
161 request->url = url; 150 request->url = url;
162 request->method = "GET"; 151 request->method = "GET";
163 request->auto_follow_redirects = true; 152 request->auto_follow_redirects = true;
164 153
165 DataPipe data_pipe; 154 url_loader_ = app_->CreateURLLoader();
166 response_body_stream_ = data_pipe.consumer_handle.Pass(); 155 url_loader_->Start(request.Pass(),
167 156 base::Bind(&LaunchInstance::OnReceivedResponse,
168 url_loader_->Start(request.Pass(), data_pipe.producer_handle.Pass()); 157 base::Unretained(this)));
169 } 158 }
170 159
171 void LaunchInstance::OnReceivedResponse(URLResponsePtr response) { 160 void LaunchInstance::OnReceivedResponse(URLResponsePtr response) {
172 std::string content_type = GetContentType(response->headers); 161 if (!response->error) {
173 std::string handler_url = app_->GetHandlerForContentType(content_type); 162 std::string content_type = GetContentType(response->headers);
174 if (!handler_url.empty()) { 163 std::string handler_url = app_->GetHandlerForContentType(content_type);
175 navigation::ResponseDetailsPtr nav_response( 164 if (handler_url.empty()) {
176 navigation::ResponseDetails::New()); 165 DLOG(WARNING) << "No handler for content type: " << content_type;
177 nav_response->response = response.Pass(); 166 } else {
178 nav_response->response_body_stream = response_body_stream_.Pass(); 167 navigation::ResponseDetailsPtr nav_response(
179 String response_url = nav_response->response->url; 168 navigation::ResponseDetails::New());
180 callback_.Run(handler_url, response_url, nav_response.Pass()); 169 nav_response->loader_handle = url_loader_.PassMessagePipe();
170 nav_response->response = response.Pass();
171 String response_url = nav_response->response->url;
172 callback_.Run(handler_url, response_url, nav_response.Pass());
173 }
181 } 174 }
175 ScheduleDestroy();
182 } 176 }
183 177
184 } // namespace launcher 178 } // namespace launcher
185 179
186 // static 180 // static
187 ApplicationDelegate* ApplicationDelegate::Create() { 181 ApplicationDelegate* ApplicationDelegate::Create() {
188 return new launcher::LauncherApp; 182 return new launcher::LauncherApp;
189 } 183 }
190 184
191 } // namespace mojo 185 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/examples/wget/wget.cc ('k') | mojo/services/network/url_loader_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698