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 "mojo/shell/dynamic_service_loader.h" | 5 #include "mojo/shell/dynamic_service_loader.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 FROM_HERE, | 73 FROM_HERE, |
74 base::Bind(&Loader::StartService, | 74 base::Bind(&Loader::StartService, |
75 base::Unretained(this), | 75 base::Unretained(this), |
76 path, | 76 path, |
77 base::Passed(&service_handle), | 77 base::Passed(&service_handle), |
78 true)); | 78 true)); |
79 } | 79 } |
80 }; | 80 }; |
81 | 81 |
82 // For loading services via the network stack. | 82 // For loading services via the network stack. |
83 class NetworkLoader : public Loader, public URLLoaderClient { | 83 class NetworkLoader : public Loader { |
84 public: | 84 public: |
85 explicit NetworkLoader(scoped_ptr<DynamicServiceRunner> runner, | 85 explicit NetworkLoader(scoped_ptr<DynamicServiceRunner> runner, |
86 NetworkService* network_service) | 86 NetworkService* network_service) |
87 : Loader(runner.Pass()) { | 87 : Loader(runner.Pass()) { |
88 network_service->CreateURLLoader(Get(&url_loader_)); | 88 network_service->CreateURLLoader(Get(&url_loader_)); |
89 url_loader_.set_client(this); | |
90 } | 89 } |
91 | 90 |
92 virtual void Start(const GURL& url, | 91 virtual void Start(const GURL& url, |
93 ScopedMessagePipeHandle service_handle, | 92 ScopedMessagePipeHandle service_handle, |
94 Context* context) OVERRIDE { | 93 Context* context) OVERRIDE { |
95 service_handle_ = service_handle.Pass(); | 94 service_handle_ = service_handle.Pass(); |
| 95 context_ = context; |
96 | 96 |
97 URLRequestPtr request(URLRequest::New()); | 97 URLRequestPtr request(URLRequest::New()); |
98 request->url = url.spec(); | 98 request->url = url.spec(); |
99 request->auto_follow_redirects = true; | 99 request->auto_follow_redirects = true; |
100 | 100 |
101 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 101 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
102 switches::kDisableCache)) { | 102 switches::kDisableCache)) { |
103 request->bypass_cache = true; | 103 request->bypass_cache = true; |
104 } | 104 } |
105 | 105 |
106 DataPipe data_pipe; | 106 url_loader_->Start(request.Pass(), |
107 url_loader_->Start(request.Pass(), data_pipe.producer_handle.Pass()); | 107 base::Bind(&NetworkLoader::OnReceivedResponse, |
108 | 108 base::Unretained(this))); |
109 base::CreateTemporaryFile(&file_); | |
110 common::CopyToFile(data_pipe.consumer_handle.Pass(), | |
111 file_, | |
112 context->task_runners()->blocking_pool(), | |
113 base::Bind(&Loader::StartService, | |
114 base::Unretained(this), | |
115 file_, | |
116 base::Passed(&service_handle_))); | |
117 } | 109 } |
118 | 110 |
119 private: | 111 private: |
120 virtual ~NetworkLoader() { | 112 virtual ~NetworkLoader() { |
121 if (!file_.empty()) | 113 if (!file_.empty()) |
122 base::DeleteFile(file_, false); | 114 base::DeleteFile(file_, false); |
123 } | 115 } |
124 | 116 |
125 // URLLoaderClient methods: | 117 void OnReceivedResponse(URLResponsePtr response) { |
126 virtual void OnReceivedRedirect(URLResponsePtr response, | 118 if (response->error) { |
127 const String& new_url, | 119 LOG(ERROR) << "Error (" << response->error->code << ": " |
128 const String& new_method) OVERRIDE { | 120 << response->error->description << ") while fetching " |
129 // TODO(darin): Handle redirects properly! | 121 << response->url; |
| 122 } |
| 123 |
| 124 base::CreateTemporaryFile(&file_); |
| 125 common::CopyToFile(response->body.Pass(), |
| 126 file_, |
| 127 context_->task_runners()->blocking_pool(), |
| 128 base::Bind(&Loader::StartService, |
| 129 base::Unretained(this), |
| 130 file_, |
| 131 base::Passed(&service_handle_))); |
130 } | 132 } |
131 virtual void OnReceivedResponse(URLResponsePtr response) OVERRIDE {} | |
132 virtual void OnReceivedError(NetworkErrorPtr error) OVERRIDE {} | |
133 virtual void OnReceivedEndOfResponseBody() OVERRIDE {} | |
134 | 133 |
| 134 Context* context_; |
135 NetworkServicePtr network_service_; | 135 NetworkServicePtr network_service_; |
136 URLLoaderPtr url_loader_; | 136 URLLoaderPtr url_loader_; |
137 ScopedMessagePipeHandle service_handle_; | 137 ScopedMessagePipeHandle service_handle_; |
138 base::FilePath file_; | 138 base::FilePath file_; |
139 }; | 139 }; |
140 | 140 |
141 } // namespace | 141 } // namespace |
142 | 142 |
143 DynamicServiceLoader::DynamicServiceLoader( | 143 DynamicServiceLoader::DynamicServiceLoader( |
144 Context* context, | 144 Context* context, |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 } | 177 } |
178 | 178 |
179 void DynamicServiceLoader::OnServiceError(ServiceManager* manager, | 179 void DynamicServiceLoader::OnServiceError(ServiceManager* manager, |
180 const GURL& url) { | 180 const GURL& url) { |
181 // TODO(darin): What should we do about service errors? This implies that | 181 // TODO(darin): What should we do about service errors? This implies that |
182 // the app closed its handle to the service manager. Maybe we don't care? | 182 // the app closed its handle to the service manager. Maybe we don't care? |
183 } | 183 } |
184 | 184 |
185 } // namespace shell | 185 } // namespace shell |
186 } // namespace mojo | 186 } // namespace mojo |
OLD | NEW |