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

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: snapshot 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
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"
8 #include "mojo/public/cpp/application/application_connection.h" 9 #include "mojo/public/cpp/application/application_connection.h"
9 #include "mojo/public/cpp/application/application_delegate.h" 10 #include "mojo/public/cpp/application/application_delegate.h"
10 #include "mojo/public/cpp/application/application_impl.h" 11 #include "mojo/public/cpp/application/application_impl.h"
11 #include "mojo/services/public/cpp/view_manager/types.h" 12 #include "mojo/services/public/cpp/view_manager/types.h"
12 #include "mojo/services/public/interfaces/launcher/launcher.mojom.h" 13 #include "mojo/services/public/interfaces/launcher/launcher.mojom.h"
13 #include "mojo/services/public/interfaces/network/network_service.mojom.h" 14 #include "mojo/services/public/interfaces/network/network_service.mojom.h"
14 #include "mojo/services/public/interfaces/network/url_loader.mojom.h" 15 #include "mojo/services/public/interfaces/network/url_loader.mojom.h"
(...skipping 20 matching lines...) Expand all
35 private: 36 private:
36 // Overridden from Launcher: 37 // Overridden from Launcher:
37 virtual void Launch(const String& url, 38 virtual void Launch(const String& url,
38 const LaunchCallback& callback) OVERRIDE; 39 const LaunchCallback& callback) OVERRIDE;
39 40
40 LauncherApp* app_; 41 LauncherApp* app_;
41 42
42 DISALLOW_COPY_AND_ASSIGN(LauncherConnection); 43 DISALLOW_COPY_AND_ASSIGN(LauncherConnection);
43 }; 44 };
44 45
45 class LaunchInstance : public URLLoaderClient { 46 class LaunchInstance {
46 public: 47 public:
47 LaunchInstance(LauncherApp* app, 48 LaunchInstance(LauncherApp* app,
48 const LaunchCallback& callback, 49 const LaunchCallback& callback,
49 const String& url); 50 const String& url);
50 virtual ~LaunchInstance() {} 51 virtual ~LaunchInstance() {}
51 52
52 private: 53 private:
53 // Overridden from URLLoaderClient: 54 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 55
66 std::string GetContentType(const Array<String>& headers) { 56 std::string GetContentType(const Array<String>& headers) {
67 for (size_t i = 0; i < headers.size(); ++i) { 57 for (size_t i = 0; i < headers.size(); ++i) {
68 base::StringTokenizer t(headers[i], ": ;="); 58 base::StringTokenizer t(headers[i], ": ;=");
69 while (t.GetNext()) { 59 while (t.GetNext()) {
70 if (!t.token_is_delim() && t.token() == "Content-Type") { 60 if (!t.token_is_delim() && t.token() == "Content-Type") {
71 while (t.GetNext()) { 61 while (t.GetNext()) {
72 if (!t.token_is_delim()) 62 if (!t.token_is_delim())
73 return t.token(); 63 return t.token();
74 } 64 }
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 } 137 }
148 new LaunchInstance(app_, callback, url_string); 138 new LaunchInstance(app_, callback, url_string);
149 } 139 }
150 140
151 LaunchInstance::LaunchInstance(LauncherApp* app, 141 LaunchInstance::LaunchInstance(LauncherApp* app,
152 const LaunchCallback& callback, 142 const LaunchCallback& callback,
153 const String& url) 143 const String& url)
154 : app_(app), 144 : app_(app),
155 destroy_scheduled_(false), 145 destroy_scheduled_(false),
156 callback_(callback) { 146 callback_(callback) {
157 url_loader_ = app_->CreateURLLoader();
158 url_loader_.set_client(this);
159
160 URLRequestPtr request(URLRequest::New()); 147 URLRequestPtr request(URLRequest::New());
161 request->url = url; 148 request->url = url;
162 request->method = "GET"; 149 request->method = "GET";
163 request->auto_follow_redirects = true; 150 request->auto_follow_redirects = true;
164 151
165 DataPipe data_pipe; 152 url_loader_ = app_->CreateURLLoader();
166 response_body_stream_ = data_pipe.consumer_handle.Pass(); 153 url_loader_->Start(request.Pass(),
167 154 base::Bind(&LaunchInstance::OnReceivedResponse,
168 url_loader_->Start(request.Pass(), data_pipe.producer_handle.Pass()); 155 base::Unretained(this)));
169 } 156 }
170 157
171 void LaunchInstance::OnReceivedResponse(URLResponsePtr response) { 158 void LaunchInstance::OnReceivedResponse(URLResponsePtr response) {
172 std::string content_type = GetContentType(response->headers); 159 if (!response->error) {
173 std::string handler_url = app_->GetHandlerForContentType(content_type); 160 std::string content_type = GetContentType(response->headers);
174 if (!handler_url.empty()) { 161 std::string handler_url = app_->GetHandlerForContentType(content_type);
175 navigation::ResponseDetailsPtr nav_response( 162 if (!handler_url.empty()) {
Aaron Boodman 2014/07/10 20:05:00 Maybe print out an error or something for now?
176 navigation::ResponseDetails::New()); 163 navigation::ResponseDetailsPtr nav_response(
177 nav_response->response = response.Pass(); 164 navigation::ResponseDetails::New());
178 nav_response->response_body_stream = response_body_stream_.Pass(); 165 nav_response->loader_handle = url_loader_.PassMessagePipe();
179 String response_url = nav_response->response->url; 166 nav_response->response = response.Pass();
180 callback_.Run(handler_url, response_url, nav_response.Pass()); 167 String response_url = nav_response->response->url;
168 callback_.Run(handler_url, response_url, nav_response.Pass());
169 }
181 } 170 }
171 ScheduleDestroy();
182 } 172 }
183 173
184 } // namespace launcher 174 } // namespace launcher
185 175
186 // static 176 // static
187 ApplicationDelegate* ApplicationDelegate::Create() { 177 ApplicationDelegate* ApplicationDelegate::Create() {
188 return new launcher::LauncherApp; 178 return new launcher::LauncherApp;
189 } 179 }
190 180
191 } // namespace mojo 181 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698