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_application_loader.h" | 5 #include "mojo/shell/dynamic_application_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" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
13 #include "mojo/common/common_type_converters.h" | 13 #include "mojo/common/common_type_converters.h" |
14 #include "mojo/common/data_pipe_utils.h" | 14 #include "mojo/common/data_pipe_utils.h" |
15 #include "mojo/services/public/interfaces/network/url_loader.mojom.h" | 15 #include "mojo/services/public/interfaces/network/url_loader.mojom.h" |
16 #include "mojo/shell/context.h" | 16 #include "mojo/shell/context.h" |
17 #include "mojo/shell/switches.h" | 17 #include "mojo/shell/switches.h" |
18 #include "net/base/filename_util.h" | 18 #include "net/base/filename_util.h" |
19 | 19 |
20 namespace mojo { | 20 namespace mojo { |
21 namespace shell { | 21 namespace shell { |
22 | 22 |
| 23 namespace { |
| 24 |
| 25 class Loader { |
| 26 public: |
| 27 Loader(scoped_refptr<ApplicationLoader::LoadCallbacks> callbacks, |
| 28 scoped_ptr<DynamicServiceRunner> runner) |
| 29 : callbacks_(callbacks), |
| 30 runner_(runner.Pass()) { |
| 31 } |
| 32 |
| 33 virtual ~Loader() { |
| 34 } |
| 35 |
| 36 protected: |
| 37 void RunLibrary(const base::FilePath& path, bool path_exists) { |
| 38 ScopedMessagePipeHandle shell_handle = callbacks_->RegisterApplication(); |
| 39 if (!shell_handle.is_valid()) |
| 40 return; |
| 41 |
| 42 if (!path_exists) { |
| 43 DVLOG(1) << "Library not started because library path '" |
| 44 << path.value() << "' does not exist."; |
| 45 return; |
| 46 } |
| 47 |
| 48 runner_->Start(path, |
| 49 shell_handle.Pass(), |
| 50 base::Bind(&Loader::AppCompleted, |
| 51 base::Unretained(this))); |
| 52 } |
| 53 |
| 54 void AppCompleted() { |
| 55 delete this; |
| 56 } |
| 57 |
| 58 scoped_refptr<ApplicationLoader::LoadCallbacks> callbacks_; |
| 59 scoped_ptr<DynamicServiceRunner> runner_; |
| 60 }; |
| 61 |
| 62 class LocalLoader : public Loader { |
| 63 public: |
| 64 LocalLoader(const GURL& url, |
| 65 scoped_refptr<ApplicationLoader::LoadCallbacks> callbacks, |
| 66 scoped_ptr<DynamicServiceRunner> runner) |
| 67 : Loader(callbacks, runner.Pass()) { |
| 68 base::FilePath path; |
| 69 net::FileURLToFilePath(url, &path); |
| 70 |
| 71 // Async for consistency with network case. |
| 72 base::MessageLoop::current()->PostTask( |
| 73 FROM_HERE, |
| 74 base::Bind(&LocalLoader::RunLibrary, |
| 75 base::Unretained(this), |
| 76 path, |
| 77 base::PathExists(path))); |
| 78 } |
| 79 }; |
| 80 |
| 81 class NetworkLoader : public Loader { |
| 82 public: |
| 83 NetworkLoader(NetworkService* network_service, |
| 84 const GURL& url, |
| 85 DynamicApplicationLoader::MimeTypeToURLMap mime_type_to_url, |
| 86 scoped_refptr<ApplicationLoader::LoadCallbacks> callbacks, |
| 87 scoped_refptr<base::SequencedWorkerPool> blocking_pool, |
| 88 scoped_ptr<DynamicServiceRunner> runner) |
| 89 : Loader(callbacks, runner.Pass()), |
| 90 mime_type_to_url_(mime_type_to_url), |
| 91 blocking_pool_(blocking_pool) { |
| 92 URLRequestPtr request(URLRequest::New()); |
| 93 request->url = String::From(url); |
| 94 request->auto_follow_redirects = true; |
| 95 |
| 96 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 97 switches::kDisableCache)) { |
| 98 request->bypass_cache = true; |
| 99 } |
| 100 |
| 101 network_service->CreateURLLoader(Get(&url_loader_)); |
| 102 url_loader_->Start(request.Pass(), |
| 103 base::Bind(&NetworkLoader::OnLoadComplete, |
| 104 base::Unretained(this))); // Passed() ? |
| 105 } |
| 106 |
| 107 virtual ~NetworkLoader() { |
| 108 if (!file_.empty()) |
| 109 base::DeleteFile(file_, false); |
| 110 } |
| 111 |
| 112 private: |
| 113 void OnLoadComplete(URLResponsePtr response) { |
| 114 if (response->error) { |
| 115 LOG(ERROR) << "Error (" << response->error->code << ": " |
| 116 << response->error->description << ") while fetching " |
| 117 << response->url; |
| 118 } |
| 119 |
| 120 DynamicApplicationLoader::MimeTypeToURLMap::iterator iter = |
| 121 mime_type_to_url_.find(response->mime_type); |
| 122 if (iter != mime_type_to_url_.end()) { |
| 123 callbacks_->LoadWithContentHandler(iter->second, |
| 124 response.Pass(), |
| 125 url_loader_.Pass()); |
| 126 delete this; |
| 127 return; |
| 128 } |
| 129 |
| 130 base::CreateTemporaryFile(&file_); |
| 131 common::CopyToFile(response->body.Pass(), |
| 132 file_, |
| 133 blocking_pool_, |
| 134 base::Bind(&NetworkLoader::RunLibrary, |
| 135 base::Unretained(this), |
| 136 file_)); |
| 137 } |
| 138 |
| 139 DynamicApplicationLoader::MimeTypeToURLMap mime_type_to_url_; |
| 140 URLLoaderPtr url_loader_; |
| 141 base::FilePath file_; |
| 142 scoped_refptr<base::SequencedWorkerPool> blocking_pool_; |
| 143 }; |
| 144 |
| 145 } // namespace |
| 146 |
23 DynamicApplicationLoader::DynamicApplicationLoader( | 147 DynamicApplicationLoader::DynamicApplicationLoader( |
24 Context* context, | 148 Context* context, |
25 scoped_ptr<DynamicServiceRunnerFactory> runner_factory) | 149 scoped_ptr<DynamicServiceRunnerFactory> runner_factory) |
26 : context_(context), | 150 : context_(context), |
27 runner_factory_(runner_factory.Pass()), | 151 runner_factory_(runner_factory.Pass()), |
28 weak_ptr_factory_(this) { | 152 weak_ptr_factory_(this) { |
29 } | 153 } |
30 | 154 |
31 DynamicApplicationLoader::~DynamicApplicationLoader() { | 155 DynamicApplicationLoader::~DynamicApplicationLoader() { |
32 } | 156 } |
(...skipping 16 matching lines...) Expand all Loading... |
49 | 173 |
50 if (resolved_url.SchemeIsFile()) | 174 if (resolved_url.SchemeIsFile()) |
51 LoadLocalService(resolved_url, callbacks); | 175 LoadLocalService(resolved_url, callbacks); |
52 else | 176 else |
53 LoadNetworkService(resolved_url, callbacks); | 177 LoadNetworkService(resolved_url, callbacks); |
54 } | 178 } |
55 | 179 |
56 void DynamicApplicationLoader::LoadLocalService( | 180 void DynamicApplicationLoader::LoadLocalService( |
57 const GURL& resolved_url, | 181 const GURL& resolved_url, |
58 scoped_refptr<LoadCallbacks> callbacks) { | 182 scoped_refptr<LoadCallbacks> callbacks) { |
59 base::FilePath path; | 183 new LocalLoader(resolved_url, callbacks, runner_factory_->Create(context_)); |
60 net::FileURLToFilePath(resolved_url, &path); | |
61 const bool kDeleteFileAfter = false; | |
62 | |
63 // Async for consistency with network case. | |
64 base::MessageLoop::current()->PostTask( | |
65 FROM_HERE, | |
66 base::Bind(&DynamicApplicationLoader::RunLibrary, | |
67 weak_ptr_factory_.GetWeakPtr(), | |
68 path, | |
69 callbacks, | |
70 kDeleteFileAfter, | |
71 base::PathExists(path))); | |
72 } | 184 } |
73 | 185 |
74 void DynamicApplicationLoader::LoadNetworkService( | 186 void DynamicApplicationLoader::LoadNetworkService( |
75 const GURL& resolved_url, | 187 const GURL& resolved_url, |
76 scoped_refptr<LoadCallbacks> callbacks) { | 188 scoped_refptr<LoadCallbacks> callbacks) { |
77 if (!network_service_) { | 189 if (!network_service_) { |
78 context_->application_manager()->ConnectToService( | 190 context_->application_manager()->ConnectToService( |
79 GURL("mojo:mojo_network_service"), &network_service_); | 191 GURL("mojo:mojo_network_service"), &network_service_); |
80 } | 192 } |
81 if (!url_loader_) | |
82 network_service_->CreateURLLoader(Get(&url_loader_)); | |
83 | 193 |
84 URLRequestPtr request(URLRequest::New()); | 194 new NetworkLoader(network_service_.get(), |
85 request->url = String::From(resolved_url); | 195 resolved_url, |
86 request->auto_follow_redirects = true; | 196 mime_type_to_url_, |
87 | 197 callbacks, |
88 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 198 context_->task_runners()->blocking_pool(), |
89 switches::kDisableCache)) { | 199 runner_factory_->Create(context_)); |
90 request->bypass_cache = true; | |
91 } | |
92 | |
93 url_loader_->Start( | |
94 request.Pass(), | |
95 base::Bind(&DynamicApplicationLoader::OnLoadNetworkServiceComplete, | |
96 weak_ptr_factory_.GetWeakPtr(), | |
97 callbacks)); | |
98 } | |
99 | |
100 void DynamicApplicationLoader::OnLoadNetworkServiceComplete( | |
101 scoped_refptr<LoadCallbacks> callbacks, | |
102 URLResponsePtr response) { | |
103 if (response->error) { | |
104 LOG(ERROR) << "Error (" << response->error->code << ": " | |
105 << response->error->description << ") while fetching " | |
106 << response->url; | |
107 } | |
108 | |
109 MimeTypeToURLMap::iterator iter = mime_type_to_url_.find(response->mime_type); | |
110 if (iter != mime_type_to_url_.end()) { | |
111 callbacks->LoadWithContentHandler(iter->second, response.Pass()); | |
112 return; | |
113 } | |
114 | |
115 base::FilePath file; | |
116 base::CreateTemporaryFile(&file); | |
117 | |
118 const bool kDeleteFileAfter = true; | |
119 common::CopyToFile(response->body.Pass(), | |
120 file, | |
121 context_->task_runners()->blocking_pool(), | |
122 base::Bind(&DynamicApplicationLoader::RunLibrary, | |
123 weak_ptr_factory_.GetWeakPtr(), | |
124 file, | |
125 callbacks, | |
126 kDeleteFileAfter)); | |
127 } | |
128 | |
129 void DynamicApplicationLoader::RunLibrary( | |
130 const base::FilePath& path, | |
131 scoped_refptr<LoadCallbacks> callbacks, | |
132 bool delete_file_after, | |
133 bool path_exists) { | |
134 | |
135 ScopedMessagePipeHandle shell_handle = callbacks->RegisterApplication(); | |
136 if (!shell_handle.is_valid()) | |
137 return; | |
138 | |
139 if (!path_exists) { | |
140 DVLOG(1) << "Library not started because library path '" | |
141 << path.value() << "' does not exist."; | |
142 return; | |
143 } | |
144 | |
145 scoped_ptr<DynamicServiceRunner> runner = | |
146 runner_factory_->Create(context_).Pass(); | |
147 runner->Start(path, | |
148 shell_handle.Pass(), | |
149 base::Bind(&DynamicApplicationLoader::OnRunLibraryComplete, | |
150 weak_ptr_factory_.GetWeakPtr(), | |
151 base::Unretained(runner.get()), | |
152 delete_file_after ? path : base::FilePath())); | |
153 runners_.push_back(runner.release()); | |
154 } | |
155 | |
156 void DynamicApplicationLoader::OnRunLibraryComplete( | |
157 DynamicServiceRunner* runner, | |
158 const base::FilePath& temp_file) { | |
159 for (ScopedVector<DynamicServiceRunner>::iterator it = runners_.begin(); | |
160 it != runners_.end(); ++it) { | |
161 if (*it == runner) { | |
162 runners_.erase(it); | |
163 if (!temp_file.empty()) | |
164 base::DeleteFile(temp_file, false); | |
165 return; | |
166 } | |
167 } | |
168 } | 200 } |
169 | 201 |
170 void DynamicApplicationLoader::OnServiceError(ApplicationManager* manager, | 202 void DynamicApplicationLoader::OnServiceError(ApplicationManager* manager, |
171 const GURL& url) { | 203 const GURL& url) { |
172 // TODO(darin): What should we do about service errors? This implies that | 204 // TODO(darin): What should we do about service errors? This implies that |
173 // the app closed its handle to the service manager. Maybe we don't care? | 205 // the app closed its handle to the service manager. Maybe we don't care? |
174 } | 206 } |
175 | 207 |
176 } // namespace shell | 208 } // namespace shell |
177 } // namespace mojo | 209 } // namespace mojo |
OLD | NEW |