OLD | NEW |
| (Empty) |
1 // Copyright 2013 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/shell/loader.h" | |
6 | |
7 #include "base/command_line.h" | |
8 #include "base/file_util.h" | |
9 #include "base/message_loop/message_loop.h" | |
10 #include "mojo/shell/switches.h" | |
11 #include "net/base/load_flags.h" | |
12 #include "net/base/network_delegate.h" | |
13 #include "net/http/http_response_headers.h" | |
14 #include "net/url_request/url_fetcher.h" | |
15 #include "net/url_request/url_request_status.h" | |
16 | |
17 namespace mojo { | |
18 namespace shell { | |
19 | |
20 Loader::Delegate::~Delegate() { | |
21 } | |
22 | |
23 Loader::Job::Job(const GURL& app_url, Delegate* delegate) | |
24 : delegate_(delegate) { | |
25 fetcher_.reset(net::URLFetcher::Create(app_url, net::URLFetcher::GET, this)); | |
26 } | |
27 | |
28 Loader::Job::~Job() { | |
29 } | |
30 | |
31 void Loader::Job::OnURLFetchComplete(const net::URLFetcher* source) { | |
32 net::URLRequestStatus status = source->GetStatus(); | |
33 if (!status.is_success()) { | |
34 LOG(ERROR) << "URL fetch didn't succeed: status = " << status.status() | |
35 << ", error = " << status.error(); | |
36 } else if (source->GetResponseCode() != 200) { | |
37 // Note: We may not have a response code (e.g., if it wasn't an http: URL). | |
38 LOG(WARNING) << "HTTP response not OK: code = " | |
39 << source->GetResponseCode(); | |
40 } | |
41 // TODO: Do something else in the error cases? | |
42 | |
43 base::FilePath app_path; | |
44 source->GetResponseAsFilePath(true, &app_path); | |
45 std::string mime_type; | |
46 // We may not have response headers (e.g., if it was a file: URL). | |
47 if (source->GetResponseHeaders()) | |
48 source->GetResponseHeaders()->GetMimeType(&mime_type); | |
49 delegate_->DidCompleteLoad(source->GetURL(), | |
50 app_path, | |
51 mime_type.empty() ? NULL : &mime_type); | |
52 } | |
53 | |
54 Loader::Loader(base::SingleThreadTaskRunner* network_runner, | |
55 base::SingleThreadTaskRunner* file_runner, | |
56 base::MessageLoopProxy* cache_runner, | |
57 scoped_ptr<net::NetworkDelegate> network_delegate, | |
58 base::FilePath base_path) | |
59 : file_runner_(file_runner), | |
60 url_request_context_getter_(new URLRequestContextGetter( | |
61 base_path, | |
62 network_runner, | |
63 file_runner, | |
64 cache_runner, | |
65 network_delegate.Pass())) { | |
66 } | |
67 | |
68 Loader::~Loader() { | |
69 } | |
70 | |
71 scoped_ptr<Loader::Job> Loader::Load(const GURL& app_url, Delegate* delegate) { | |
72 scoped_ptr<Job> job(new Job(app_url, delegate)); | |
73 job->fetcher_->SetRequestContext(url_request_context_getter_.get()); | |
74 #if defined(MOJO_SHELL_DEBUG) | |
75 base::FilePath tmp_dir; | |
76 base::GetTempDir(&tmp_dir); | |
77 // If MOJO_SHELL_DEBUG is set we want to dowload to a well known location. | |
78 // This makes it easier to do the necessary links so that symbols are found. | |
79 job->fetcher_->SaveResponseToFileAtPath( | |
80 tmp_dir.Append("link-me"), | |
81 file_runner_.get()); | |
82 #else | |
83 job->fetcher_->SaveResponseToTemporaryFile(file_runner_.get()); | |
84 #endif | |
85 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
86 switches::kDisableCache)) | |
87 job->fetcher_->SetLoadFlags(net::LOAD_DISABLE_CACHE); | |
88 job->fetcher_->Start(); | |
89 return job.Pass(); | |
90 } | |
91 | |
92 } // namespace shell | |
93 } // namespace mojo | |
OLD | NEW |