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

Side by Side Diff: mojo/shell/dynamic_service_loader.cc

Issue 423963004: First cut at "content handling" support in Mojo. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: blech Created 6 years, 4 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 "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"
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/keep_alive.h" 17 #include "mojo/shell/keep_alive.h"
18 #include "mojo/shell/switches.h" 18 #include "mojo/shell/switches.h"
19 #include "net/base/filename_util.h" 19 #include "net/base/filename_util.h"
20 20
21 namespace mojo { 21 namespace mojo {
22 namespace shell { 22 namespace shell {
23
23 namespace { 24 namespace {
24 25
25 class Loader { 26 void RunLibraryComplete(DynamicServiceRunner* runner,
26 public: 27 const base::FilePath& temp_file) {
27 explicit Loader(scoped_ptr<DynamicServiceRunner> runner) 28 delete runner;
28 : runner_(runner.Pass()) { 29 if (!temp_file.empty())
29 } 30 base::DeleteFile(temp_file, false);
30 31 }
31 virtual void Start(const GURL& url,
32 ScopedMessagePipeHandle service_handle,
33 Context* context) = 0;
34
35 void StartService(const base::FilePath& path,
36 ScopedMessagePipeHandle service_handle,
37 bool path_is_valid) {
38 if (path_is_valid) {
39 runner_->Start(path, service_handle.Pass(),
40 base::Bind(&Loader::AppCompleted, base::Unretained(this)));
41 } else {
42 AppCompleted();
43 }
44 }
45
46 protected:
47 virtual ~Loader() {}
48
49 private:
50 void AppCompleted() {
51 delete this;
52 }
53
54 scoped_ptr<DynamicServiceRunner> runner_;
55 };
56
57 // For loading services via file:// URLs.
58 class LocalLoader : public Loader {
59 public:
60 explicit LocalLoader(scoped_ptr<DynamicServiceRunner> runner)
61 : Loader(runner.Pass()) {
62 }
63
64 virtual void Start(const GURL& url,
65 ScopedMessagePipeHandle service_handle,
66 Context* context) OVERRIDE {
67 base::FilePath path;
68 net::FileURLToFilePath(url, &path);
69
70 // Complete asynchronously for consistency with NetworkServiceLoader.
71 base::MessageLoop::current()->PostTask(
72 FROM_HERE,
73 base::Bind(&Loader::StartService,
74 base::Unretained(this),
75 path,
76 base::Passed(&service_handle),
77 base::PathExists(path)));
78 }
79 };
80
81 // For loading services via the network stack.
82 class NetworkLoader : public Loader {
83 public:
84 explicit NetworkLoader(scoped_ptr<DynamicServiceRunner> runner,
85 NetworkService* network_service)
86 : Loader(runner.Pass()) {
87 network_service->CreateURLLoader(Get(&url_loader_));
88 }
89
90 virtual void Start(const GURL& url,
91 ScopedMessagePipeHandle service_handle,
92 Context* context) OVERRIDE {
93 service_handle_ = service_handle.Pass();
94 context_ = context;
95
96 URLRequestPtr request(URLRequest::New());
97 request->url = String::From(url);
98 request->auto_follow_redirects = true;
99
100 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
101 switches::kDisableCache)) {
102 request->bypass_cache = true;
103 }
104
105 url_loader_->Start(request.Pass(),
106 base::Bind(&NetworkLoader::OnReceivedResponse,
107 base::Unretained(this)));
108 }
109
110 private:
111 virtual ~NetworkLoader() {
112 if (!file_.empty())
113 base::DeleteFile(file_, false);
114 }
115
116 void OnReceivedResponse(URLResponsePtr response) {
117 if (response->error) {
118 LOG(ERROR) << "Error (" << response->error->code << ": "
119 << response->error->description << ") while fetching "
120 << response->url;
121 }
122
123 base::CreateTemporaryFile(&file_);
124 common::CopyToFile(response->body.Pass(),
125 file_,
126 context_->task_runners()->blocking_pool(),
127 base::Bind(&Loader::StartService,
128 base::Unretained(this),
129 file_,
130 base::Passed(&service_handle_)));
131 }
132
133 Context* context_;
134 NetworkServicePtr network_service_;
135 URLLoaderPtr url_loader_;
136 ScopedMessagePipeHandle service_handle_;
137 base::FilePath file_;
138 };
139 32
140 } // namespace 33 } // namespace
141 34
142 DynamicServiceLoader::DynamicServiceLoader( 35 DynamicServiceLoader::DynamicServiceLoader(
143 Context* context, 36 Context* context,
144 scoped_ptr<DynamicServiceRunnerFactory> runner_factory) 37 scoped_ptr<DynamicServiceRunnerFactory> runner_factory)
145 : context_(context), 38 : context_(context),
146 runner_factory_(runner_factory.Pass()) { 39 runner_factory_(runner_factory.Pass()),
40 weak_ptr_factory_(this) {
147 } 41 }
148 42
149 DynamicServiceLoader::~DynamicServiceLoader() { 43 DynamicServiceLoader::~DynamicServiceLoader() {
150 } 44 }
151 45
152 void DynamicServiceLoader::LoadService(ServiceManager* manager, 46 void DynamicServiceLoader::set_content_handler(
darin (slow to review) 2014/08/06 23:10:02 nit: perhaps this method should be called Register
Aaron Boodman 2014/08/07 00:03:12 Fair enough. Done.
153 const GURL& url, 47 const std::string& mime_type,
154 ScopedMessagePipeHandle shell_handle) { 48 const GURL& content_handler_url) {
155 scoped_ptr<DynamicServiceRunner> runner = runner_factory_->Create(context_); 49 mime_type_to_url_[mime_type] = content_handler_url;
50 }
156 51
52 void DynamicServiceLoader::LoadService(
53 ServiceManager* manager,
54 const GURL& url,
55 scoped_refptr<LoadServiceCallbacks> callbacks) {
157 GURL resolved_url; 56 GURL resolved_url;
158 if (url.SchemeIs("mojo")) { 57 if (url.SchemeIs("mojo")) {
159 resolved_url = context_->mojo_url_resolver()->Resolve(url); 58 resolved_url = context_->mojo_url_resolver()->Resolve(url);
160 } else { 59 } else {
161 resolved_url = url; 60 resolved_url = url;
162 } 61 }
163 62
164 Loader* loader; 63 if (resolved_url.SchemeIsFile())
165 if (resolved_url.SchemeIsFile()) { 64 LoadLocalService(resolved_url, callbacks);
166 loader = new LocalLoader(runner.Pass()); 65 else
167 } else { 66 LoadNetworkService(resolved_url, callbacks);
168 if (!network_service_) { 67 }
169 context_->service_manager()->ConnectToService( 68
170 GURL("mojo:mojo_network_service"), 69 void DynamicServiceLoader::LoadLocalService(
171 &network_service_); 70 const GURL& resolved_url,
172 } 71 scoped_refptr<LoadServiceCallbacks> callbacks) {
173 loader = new NetworkLoader(runner.Pass(), network_service_.get()); 72 base::FilePath path;
73 net::FileURLToFilePath(resolved_url, &path);
74 const bool kDeleteFileAfter = false;
75
76 // Async for consistency with network case.
77 base::MessageLoop::current()->PostTask(
78 FROM_HERE,
79 base::Bind(&DynamicServiceLoader::RunLibrary,
80 weak_ptr_factory_.GetWeakPtr(),
81 path,
82 callbacks,
83 kDeleteFileAfter,
84 base::PathExists(path)));
85 }
86
87 void DynamicServiceLoader::LoadNetworkService(
88 const GURL& resolved_url,
89 scoped_refptr<LoadServiceCallbacks> callbacks) {
90 if (!network_service_) {
91 context_->service_manager()->ConnectToService(
92 GURL("mojo:mojo_network_service"),
93 &network_service_);
174 } 94 }
175 loader->Start(resolved_url, shell_handle.Pass(), context_); 95 if (!url_loader_)
96 network_service_->CreateURLLoader(Get(&url_loader_));
97
98 URLRequestPtr request(URLRequest::New());
99 request->url = String::From(resolved_url);
100 request->auto_follow_redirects = true;
101
102 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
103 switches::kDisableCache)) {
104 request->bypass_cache = true;
105 }
106
107 url_loader_->Start(
108 request.Pass(),
109 base::Bind(&DynamicServiceLoader::OnLoadNetworkServiceComplete,
110 weak_ptr_factory_.GetWeakPtr(),
111 callbacks));
112 }
113
114 void DynamicServiceLoader::OnLoadNetworkServiceComplete(
115 scoped_refptr<LoadServiceCallbacks> callbacks, URLResponsePtr response) {
116 if (response->error) {
117 LOG(ERROR) << "Error (" << response->error->code << ": "
118 << response->error->description << ") while fetching "
119 << response->url;
120 }
121
122 MimeTypeToURLMap::iterator iter =
123 mime_type_to_url_.find(response->mime_type);
124 if (iter != mime_type_to_url_.end()) {
125 callbacks->LoadWithContentHandler(iter->second, response.Pass());
126 return;
127 }
128
129 base::FilePath file;
130 base::CreateTemporaryFile(&file);
131
132 const bool kDeleteFileAfter = true;
133 common::CopyToFile(response->body.Pass(),
134 file,
135 context_->task_runners()->blocking_pool(),
136 base::Bind(&DynamicServiceLoader::RunLibrary,
137 weak_ptr_factory_.GetWeakPtr(),
138 file,
139 callbacks,
140 kDeleteFileAfter));
141 }
142
143 void DynamicServiceLoader::RunLibrary(
144 const base::FilePath& path,
145 scoped_refptr<LoadServiceCallbacks> callbacks,
146 bool delete_file_after,
147 bool path_exists) {
148 // TODO(aa): We need to create a runner, even if we're not going to use it,
149 // because it getting destroyed is what causes shell to shut down. If we don't
150 // create this, in the case where shell never successfully creates even one
151 // app, then shell will never shut down, because no runners are ever
152 // destroyed.
153 scoped_ptr<DynamicServiceRunner> runner(runner_factory_->Create(context_));
154 if (!path_exists)
155 return;
156
157 ScopedMessagePipeHandle shell_handle = callbacks->RegisterApplication();
158 if (!shell_handle.is_valid())
159 return;
160
161 DynamicServiceRunner* runner_raw = runner.release();
162 runner_raw->Start(path,
163 shell_handle.Pass(),
164 base::Bind(&RunLibraryComplete,
165 base::Unretained(runner_raw),
166 delete_file_after ? path : base::FilePath()));
176 } 167 }
177 168
178 void DynamicServiceLoader::OnServiceError(ServiceManager* manager, 169 void DynamicServiceLoader::OnServiceError(ServiceManager* manager,
179 const GURL& url) { 170 const GURL& url) {
180 // TODO(darin): What should we do about service errors? This implies that 171 // TODO(darin): What should we do about service errors? This implies that
181 // the app closed its handle to the service manager. Maybe we don't care? 172 // the app closed its handle to the service manager. Maybe we don't care?
182 } 173 }
183 174
184 } // namespace shell 175 } // namespace shell
185 } // namespace mojo 176 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698