| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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 "mojo/package_manager/package_manager_impl.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 | |
| 9 #include <utility> | |
| 10 | |
| 11 #include "base/bind.h" | |
| 12 #include "mojo/application/public/interfaces/content_handler.mojom.h" | |
| 13 #include "mojo/fetcher/about_fetcher.h" | |
| 14 #include "mojo/fetcher/data_fetcher.h" | |
| 15 #include "mojo/fetcher/local_fetcher.h" | |
| 16 #include "mojo/fetcher/network_fetcher.h" | |
| 17 #include "mojo/fetcher/switches.h" | |
| 18 #include "mojo/fetcher/update_fetcher.h" | |
| 19 #include "mojo/package_manager/content_handler_connection.h" | |
| 20 #include "mojo/shell/application_manager.h" | |
| 21 #include "mojo/shell/connect_util.h" | |
| 22 #include "mojo/shell/query_util.h" | |
| 23 #include "mojo/shell/switches.h" | |
| 24 #include "mojo/util/filename_util.h" | |
| 25 #include "url/gurl.h" | |
| 26 | |
| 27 namespace mojo { | |
| 28 namespace package_manager { | |
| 29 | |
| 30 PackageManagerImpl::PackageManagerImpl( | |
| 31 const base::FilePath& shell_file_root, | |
| 32 base::TaskRunner* task_runner) | |
| 33 : application_manager_(nullptr), | |
| 34 disable_cache_(base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 35 switches::kDisableCache)), | |
| 36 content_handler_id_counter_(0u), | |
| 37 task_runner_(task_runner), | |
| 38 shell_file_root_(shell_file_root) { | |
| 39 if (!shell_file_root.empty()) { | |
| 40 GURL mojo_root_file_url = | |
| 41 util::FilePathToFileURL(shell_file_root).Resolve(std::string()); | |
| 42 url_resolver_.reset(new fetcher::URLResolver(mojo_root_file_url)); | |
| 43 } | |
| 44 } | |
| 45 | |
| 46 PackageManagerImpl::~PackageManagerImpl() { | |
| 47 IdentityToContentHandlerMap identity_to_content_handler( | |
| 48 identity_to_content_handler_); | |
| 49 for (auto& pair : identity_to_content_handler) | |
| 50 pair.second->CloseConnection(); | |
| 51 } | |
| 52 | |
| 53 void PackageManagerImpl::RegisterContentHandler( | |
| 54 const std::string& mime_type, | |
| 55 const GURL& content_handler_url) { | |
| 56 DCHECK(content_handler_url.is_valid()) | |
| 57 << "Content handler URL is invalid for mime type " << mime_type; | |
| 58 mime_type_to_url_[mime_type] = content_handler_url; | |
| 59 } | |
| 60 | |
| 61 void PackageManagerImpl::RegisterApplicationPackageAlias( | |
| 62 const GURL& alias, | |
| 63 const GURL& content_handler_package, | |
| 64 const std::string& qualifier) { | |
| 65 application_package_alias_[alias] = | |
| 66 std::make_pair(content_handler_package, qualifier); | |
| 67 } | |
| 68 | |
| 69 void PackageManagerImpl::SetApplicationManager( | |
| 70 shell::ApplicationManager* manager) { | |
| 71 application_manager_ = manager; | |
| 72 } | |
| 73 | |
| 74 void PackageManagerImpl::FetchRequest( | |
| 75 URLRequestPtr request, | |
| 76 const shell::Fetcher::FetchCallback& loader_callback) { | |
| 77 GURL url(request->url); | |
| 78 if (url.SchemeIs(fetcher::AboutFetcher::kAboutScheme)) { | |
| 79 fetcher::AboutFetcher::Start(url, loader_callback); | |
| 80 return; | |
| 81 } | |
| 82 | |
| 83 if (url.SchemeIs(url::kDataScheme)) { | |
| 84 fetcher::DataFetcher::Start(url, loader_callback); | |
| 85 return; | |
| 86 } | |
| 87 | |
| 88 GURL resolved_url = ResolveURL(url); | |
| 89 if (resolved_url.SchemeIsFile()) { | |
| 90 // LocalFetcher uses the network service to infer MIME types from URLs. | |
| 91 // Skip this for mojo URLs to avoid recursively loading the network service. | |
| 92 if (!network_service_ && !url.SchemeIs("mojo") && !url.SchemeIs("exe")) { | |
| 93 shell::ConnectToService(application_manager_, | |
| 94 GURL("mojo:network_service"), &network_service_); | |
| 95 } | |
| 96 // Ownership of this object is transferred to |loader_callback|. | |
| 97 // TODO(beng): this is eff'n weird. | |
| 98 new fetcher::LocalFetcher( | |
| 99 network_service_.get(), resolved_url, | |
| 100 shell::GetBaseURLAndQuery(resolved_url, nullptr), | |
| 101 shell_file_root_, loader_callback); | |
| 102 return; | |
| 103 } | |
| 104 | |
| 105 #if 0 | |
| 106 // TODO(beng): figure out how this should be integrated now that mapped_url | |
| 107 // is toast. | |
| 108 // TODO(scottmg): to quote someone I know, if you liked this you shouldda put | |
| 109 // a test on it. | |
| 110 if (url.SchemeIs("mojo") && | |
| 111 base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 112 switches::kUseUpdater)) { | |
| 113 shell::ConnectToService(application_manager_, GURL("mojo:updater"), | |
| 114 &updater_); | |
| 115 // Ownership of this object is transferred to |loader_callback|. | |
| 116 // TODO(beng): this is eff'n weird. | |
| 117 new fetcher::UpdateFetcher(url, updater_.get(), loader_callback); | |
| 118 return; | |
| 119 } | |
| 120 #endif | |
| 121 | |
| 122 if (!url_loader_factory_) { | |
| 123 shell::ConnectToService(application_manager_, GURL("mojo:network_service"), | |
| 124 &url_loader_factory_); | |
| 125 } | |
| 126 | |
| 127 // Ownership of this object is transferred to |loader_callback|. | |
| 128 // TODO(beng): this is eff'n weird. | |
| 129 new fetcher::NetworkFetcher(disable_cache_, std::move(request), | |
| 130 url_loader_factory_.get(), loader_callback); | |
| 131 } | |
| 132 | |
| 133 uint32_t PackageManagerImpl::HandleWithContentHandler( | |
| 134 shell::Fetcher* fetcher, | |
| 135 const shell::Identity& source, | |
| 136 const GURL& target_url, | |
| 137 const shell::CapabilityFilter& target_filter, | |
| 138 InterfaceRequest<Application>* application_request) { | |
| 139 shell::Identity content_handler_identity; | |
| 140 URLResponsePtr response; | |
| 141 if (ShouldHandleWithContentHandler(fetcher, | |
| 142 target_url, | |
| 143 target_filter, | |
| 144 &content_handler_identity, | |
| 145 &response)) { | |
| 146 ContentHandlerConnection* connection = | |
| 147 GetContentHandler(content_handler_identity, source); | |
| 148 connection->StartApplication(std::move(*application_request), | |
| 149 std::move(response)); | |
| 150 return connection->id(); | |
| 151 } | |
| 152 return Shell::kInvalidContentHandlerID; | |
| 153 } | |
| 154 | |
| 155 GURL PackageManagerImpl::ResolveURL(const GURL& url) { | |
| 156 return url_resolver_.get() ? url_resolver_->ResolveMojoURL(url) : url; | |
| 157 } | |
| 158 | |
| 159 bool PackageManagerImpl::ShouldHandleWithContentHandler( | |
| 160 shell::Fetcher* fetcher, | |
| 161 const GURL& target_url, | |
| 162 const shell::CapabilityFilter& target_filter, | |
| 163 shell::Identity* content_handler_identity, | |
| 164 URLResponsePtr* response) const { | |
| 165 // TODO(beng): it seems like some delegate should/would want to have a say in | |
| 166 // configuring the qualifier also. | |
| 167 // Why can't we use the real qualifier in single process mode? Because of | |
| 168 // base::AtExitManager. If you link in ApplicationRunner into your code, and | |
| 169 // then we make initialize multiple copies of the application, we end up | |
| 170 // with multiple AtExitManagers and will check on the second one being | |
| 171 // created. | |
| 172 // | |
| 173 // Why doesn't that happen when running different apps? Because | |
| 174 // your_thing.mojo!base::AtExitManager and | |
| 175 // my_thing.mojo!base::AtExitManager are different symbols. | |
| 176 bool use_real_qualifier = base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 177 switches::kEnableMultiprocess); | |
| 178 | |
| 179 GURL content_handler_url; | |
| 180 // The response begins with a #!mojo <content-handler-url>. | |
| 181 std::string shebang; | |
| 182 if (fetcher->PeekContentHandler(&shebang, &content_handler_url)) { | |
| 183 *response = fetcher->AsURLResponse(task_runner_, | |
| 184 static_cast<int>(shebang.size())); | |
| 185 *content_handler_identity = shell::Identity( | |
| 186 content_handler_url, | |
| 187 use_real_qualifier ? (*response)->site.To<std::string>() | |
| 188 : std::string(), | |
| 189 target_filter); | |
| 190 return true; | |
| 191 } | |
| 192 | |
| 193 // The response MIME type matches a registered content handler. | |
| 194 auto iter = mime_type_to_url_.find(fetcher->MimeType()); | |
| 195 if (iter != mime_type_to_url_.end()) { | |
| 196 *response = fetcher->AsURLResponse(task_runner_, 0); | |
| 197 *content_handler_identity = shell::Identity( | |
| 198 iter->second, | |
| 199 use_real_qualifier ? (*response)->site.To<std::string>() | |
| 200 : std::string(), | |
| 201 target_filter); | |
| 202 return true; | |
| 203 } | |
| 204 | |
| 205 // The response URL matches a registered content handler. | |
| 206 auto alias_iter = application_package_alias_.find(target_url); | |
| 207 if (alias_iter != application_package_alias_.end()) { | |
| 208 // We replace the qualifier with the one our package alias requested. | |
| 209 *response = URLResponse::New(); | |
| 210 (*response)->url = target_url.spec(); | |
| 211 *content_handler_identity = shell::Identity( | |
| 212 alias_iter->second.first, | |
| 213 use_real_qualifier ? alias_iter->second.second : std::string(), | |
| 214 target_filter); | |
| 215 return true; | |
| 216 } | |
| 217 | |
| 218 return false; | |
| 219 } | |
| 220 | |
| 221 ContentHandlerConnection* PackageManagerImpl::GetContentHandler( | |
| 222 const shell::Identity& content_handler_identity, | |
| 223 const shell::Identity& source_identity) { | |
| 224 auto it = identity_to_content_handler_.find(content_handler_identity); | |
| 225 if (it != identity_to_content_handler_.end()) | |
| 226 return it->second; | |
| 227 | |
| 228 ContentHandlerConnection* connection = new ContentHandlerConnection( | |
| 229 application_manager_, source_identity, | |
| 230 content_handler_identity, | |
| 231 ++content_handler_id_counter_, | |
| 232 base::Bind(&PackageManagerImpl::OnContentHandlerConnectionClosed, | |
| 233 base::Unretained(this))); | |
| 234 identity_to_content_handler_[content_handler_identity] = connection; | |
| 235 return connection; | |
| 236 } | |
| 237 | |
| 238 void PackageManagerImpl::OnContentHandlerConnectionClosed( | |
| 239 ContentHandlerConnection* connection) { | |
| 240 // Remove the mapping. | |
| 241 auto it = identity_to_content_handler_.find(connection->identity()); | |
| 242 DCHECK(it != identity_to_content_handler_.end()); | |
| 243 identity_to_content_handler_.erase(it); | |
| 244 } | |
| 245 | |
| 246 } // namespace package_manager | |
| 247 } // namespace mojo | |
| OLD | NEW |