summaryrefslogtreecommitdiffstats
path: root/mojo/shell/package_manager/package_manager_impl.cc
blob: 93bbe85e75e7169162b785efdbccb61e1aa7e0a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "mojo/shell/package_manager/package_manager_impl.h"

#include <stdint.h>

#include <utility>

#include "base/bind.h"
#include "mojo/shell/application_manager.h"
#include "mojo/shell/connect_util.h"
#include "mojo/shell/fetcher/about_fetcher.h"
#include "mojo/shell/fetcher/data_fetcher.h"
#include "mojo/shell/fetcher/local_fetcher.h"
#include "mojo/shell/fetcher/network_fetcher.h"
#include "mojo/shell/fetcher/switches.h"
#include "mojo/shell/package_manager/content_handler_connection.h"
#include "mojo/shell/public/interfaces/content_handler.mojom.h"
#include "mojo/shell/query_util.h"
#include "mojo/shell/switches.h"
#include "mojo/util/filename_util.h"
#include "url/gurl.h"

namespace mojo {
namespace shell {

PackageManagerImpl::PackageManagerImpl(
    const base::FilePath& shell_file_root,
    base::TaskRunner* task_runner)
    : application_manager_(nullptr),
      disable_cache_(base::CommandLine::ForCurrentProcess()->HasSwitch(
          switches::kDisableCache)),
      content_handler_id_counter_(0u),
      task_runner_(task_runner),
      shell_file_root_(shell_file_root) {
  if (!shell_file_root.empty()) {
    GURL mojo_root_file_url =
        util::FilePathToFileURL(shell_file_root).Resolve(std::string());
    url_resolver_.reset(new URLResolver(mojo_root_file_url));
  }
}

PackageManagerImpl::~PackageManagerImpl() {
  IdentityToContentHandlerMap identity_to_content_handler(
      identity_to_content_handler_);
  for (auto& pair : identity_to_content_handler)
    pair.second->CloseConnection();
}

void PackageManagerImpl::RegisterContentHandler(
    const std::string& mime_type,
    const GURL& content_handler_url) {
  DCHECK(content_handler_url.is_valid())
      << "Content handler URL is invalid for mime type " << mime_type;
  mime_type_to_url_[mime_type] = content_handler_url;
}

void PackageManagerImpl::RegisterApplicationPackageAlias(
    const GURL& alias,
    const GURL& content_handler_package,
    const std::string& qualifier) {
  application_package_alias_[alias] =
      std::make_pair(content_handler_package, qualifier);
}

void PackageManagerImpl::SetApplicationManager(ApplicationManager* manager) {
  application_manager_ = manager;
}

void PackageManagerImpl::FetchRequest(
    URLRequestPtr request,
    const Fetcher::FetchCallback& loader_callback) {
  GURL url(request->url);
  if (url.SchemeIs(AboutFetcher::kAboutScheme)) {
    AboutFetcher::Start(url, loader_callback);
    return;
  }

  if (url.SchemeIs(url::kDataScheme)) {
    DataFetcher::Start(url, loader_callback);
    return;
  }

  GURL resolved_url = ResolveURL(url);
  if (resolved_url.SchemeIsFile()) {
    // LocalFetcher uses the network service to infer MIME types from URLs.
    // Skip this for mojo URLs to avoid recursively loading the network service.
    if (!network_service_ && !url.SchemeIs("mojo") && !url.SchemeIs("exe")) {
      ConnectToService(application_manager_, GURL("mojo:network_service"),
                       &network_service_);
    }
    // Ownership of this object is transferred to |loader_callback|.
    // TODO(beng): this is eff'n weird.
    new LocalFetcher(network_service_.get(), resolved_url,
                     GetBaseURLAndQuery(resolved_url, nullptr),
                     shell_file_root_, loader_callback);
    return;
  }

  if (!url_loader_factory_) {
    ConnectToService(application_manager_, GURL("mojo:network_service"),
                     &url_loader_factory_);
  }

  // Ownership of this object is transferred to |loader_callback|.
  // TODO(beng): this is eff'n weird.
  new NetworkFetcher(disable_cache_, std::move(request),
                     url_loader_factory_.get(), loader_callback);
}

uint32_t PackageManagerImpl::HandleWithContentHandler(
    Fetcher* fetcher,
    const Identity& source,
    const GURL& target_url,
    const CapabilityFilter& target_filter,
    InterfaceRequest<Application>* application_request) {
  Identity content_handler_identity;
  URLResponsePtr response;
  if (ShouldHandleWithContentHandler(fetcher,
                                     target_url,
                                     target_filter,
                                     &content_handler_identity,
                                     &response)) {
    ContentHandlerConnection* connection =
        GetContentHandler(content_handler_identity, source);
    connection->StartApplication(std::move(*application_request),
                                 std::move(response));
    return connection->id();
  }
  return Shell::kInvalidContentHandlerID;
}

GURL PackageManagerImpl::ResolveURL(const GURL& url) {
  return url_resolver_.get() ? url_resolver_->ResolveMojoURL(url) : url;
}

bool PackageManagerImpl::ShouldHandleWithContentHandler(
  Fetcher* fetcher,
  const GURL& target_url,
  const CapabilityFilter& target_filter,
  Identity* content_handler_identity,
  URLResponsePtr* response) const {
  // TODO(beng): it seems like some delegate should/would want to have a say in
  //             configuring the qualifier also.
  // Why can't we use the real qualifier in single process mode? Because of
  // base::AtExitManager. If you link in ApplicationRunner into your code, and
  // then we make initialize multiple copies of the application, we end up
  // with multiple AtExitManagers and will check on the second one being
  // created.
  //
  // Why doesn't that happen when running different apps? Because
  // your_thing.mojo!base::AtExitManager and
  // my_thing.mojo!base::AtExitManager are different symbols.
  bool use_real_qualifier = base::CommandLine::ForCurrentProcess()->HasSwitch(
      switches::kEnableMultiprocess);

  GURL content_handler_url;
  // The response begins with a #!mojo <content-handler-url>.
  std::string shebang;
  if (fetcher->PeekContentHandler(&shebang, &content_handler_url)) {
    *response = fetcher->AsURLResponse(task_runner_,
                                       static_cast<int>(shebang.size()));
    *content_handler_identity = Identity(
        content_handler_url,
        use_real_qualifier ? (*response)->site.To<std::string>()
                           : std::string(),
        target_filter);
    return true;
  }

  // The response MIME type matches a registered content handler.
  auto iter = mime_type_to_url_.find(fetcher->MimeType());
  if (iter != mime_type_to_url_.end()) {
    *response = fetcher->AsURLResponse(task_runner_, 0);
    *content_handler_identity = Identity(
        iter->second,
        use_real_qualifier ? (*response)->site.To<std::string>()
                           : std::string(),
        target_filter);
    return true;
  }

  // The response URL matches a registered content handler.
  auto alias_iter = application_package_alias_.find(target_url);
  if (alias_iter != application_package_alias_.end()) {
    // We replace the qualifier with the one our package alias requested.
    *response = URLResponse::New();
    (*response)->url = target_url.spec();
    *content_handler_identity = Identity(
        alias_iter->second.first,
        use_real_qualifier ? alias_iter->second.second : std::string(),
        target_filter);
    return true;
  }

  return false;
}

ContentHandlerConnection* PackageManagerImpl::GetContentHandler(
    const Identity& content_handler_identity,
    const Identity& source_identity) {
  auto it = identity_to_content_handler_.find(content_handler_identity);
  if (it != identity_to_content_handler_.end())
    return it->second;

  ContentHandlerConnection* connection = new ContentHandlerConnection(
      application_manager_, source_identity,
      content_handler_identity,
      ++content_handler_id_counter_,
      base::Bind(&PackageManagerImpl::OnContentHandlerConnectionClosed,
                 base::Unretained(this)));
  identity_to_content_handler_[content_handler_identity] = connection;
  return connection;
}

void PackageManagerImpl::OnContentHandlerConnectionClosed(
    ContentHandlerConnection* connection) {
  // Remove the mapping.
  auto it = identity_to_content_handler_.find(connection->identity());
  DCHECK(it != identity_to_content_handler_.end());
  identity_to_content_handler_.erase(it);
}

}  // namespace shell
}  // namespace mojo