OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 "chrome/browser/ui/webui/vr_shell/vr_shell_ui_ui.h" | |
6 | |
7 #include "chrome/browser/profiles/profile.h" | |
8 #include "chrome/browser/ui/webui/vr_shell/vr_shell_ui_message_handler.h" | |
9 #include "chrome/common/url_constants.h" | |
10 #include "content/public/browser/web_ui.h" | |
11 #if !defined(ENABLE_VR_SHELL_UI_DEV) | |
xiyuan
2016/09/15 22:58:47
nit: insert an empty line before to make it easier
bshe
2016/09/16 18:27:53
Done.
| |
12 #include "chrome/grit/browser_resources.h" | |
13 #include "content/public/browser/web_ui_data_source.h" | |
14 #else | |
15 #include <map> | |
16 #include "base/macros.h" | |
17 #include "base/memory/ref_counted_memory.h" | |
18 #include "base/strings/string_util.h" | |
19 #include "content/public/browser/url_data_source.h" | |
20 #include "net/url_request/url_fetcher.h" | |
21 #include "net/url_request/url_fetcher_delegate.h" | |
22 #include "net/url_request/url_request_context_getter.h" | |
23 #endif | |
24 | |
25 namespace { | |
26 | |
27 #if defined(ENABLE_VR_SHELL_UI_DEV) | |
28 std::string PathWithoutParams(const std::string& path) { | |
29 return GURL(std::string("chrome://vr-shell-ui/") + path).path().substr(1); | |
30 } | |
31 | |
32 const char kRemoteBase[] = "http://localhost:8080/"; | |
33 const char kRemoteBaseAlt[] = "https://jcarpenter.github.io/hoverboard-ui/"; | |
34 const char kRemoteDefaultPath[] = "vr_shell_ui.html"; | |
35 const char kHttpNotFound[] = "HTTP/1.1 404 Not Found\n\n"; | |
36 | |
37 // RemoteDataSource --------------------------------------------------------- | |
38 | |
39 std::string GetMimeTypeForPath(const std::string& path) { | |
40 std::string filename = PathWithoutParams(path); | |
41 if (base::EndsWith(filename, ".html", base::CompareCase::INSENSITIVE_ASCII)) { | |
42 return "text/html"; | |
43 } else if (base::EndsWith(filename, ".css", | |
44 base::CompareCase::INSENSITIVE_ASCII)) { | |
45 return "text/css"; | |
46 } else if (base::EndsWith(filename, ".js", | |
47 base::CompareCase::INSENSITIVE_ASCII)) { | |
48 return "application/javascript"; | |
49 } else if (base::EndsWith(filename, ".png", | |
50 base::CompareCase::INSENSITIVE_ASCII)) { | |
51 return "image/png"; | |
52 } else if (base::EndsWith(filename, ".gif", | |
53 base::CompareCase::INSENSITIVE_ASCII)) { | |
54 return "image/gif"; | |
55 } else if (base::EndsWith(filename, ".svg", | |
56 base::CompareCase::INSENSITIVE_ASCII)) { | |
57 return "image/svg+xml"; | |
58 } else if (base::EndsWith(filename, ".manifest", | |
59 base::CompareCase::INSENSITIVE_ASCII)) { | |
60 return "text/cache-manifest"; | |
61 } | |
62 return "text/html"; | |
63 } | |
64 | |
65 class RemoteDataSource : public content::URLDataSource, | |
66 public net::URLFetcherDelegate { | |
67 public: | |
68 using GotDataCallback = content::URLDataSource::GotDataCallback; | |
69 | |
70 explicit RemoteDataSource(net::URLRequestContextGetter* request_context); | |
71 | |
72 // content::URLDataSource implementation. | |
73 std::string GetSource() const override; | |
74 void StartDataRequest(const std::string& path, | |
75 int render_process_id, | |
76 int render_frame_id, | |
77 const GotDataCallback& callback) override; | |
78 | |
79 private: | |
80 // content::URLDataSource overrides. | |
81 std::string GetMimeType(const std::string& path) const override; | |
82 bool ShouldAddContentSecurityPolicy() const override; | |
83 bool ShouldDenyXFrameOptions() const override; | |
84 bool ShouldServeMimeTypeAsContentTypeHeader() const override; | |
85 | |
86 // net::URLFetcherDelegate overrides. | |
87 void OnURLFetchComplete(const net::URLFetcher* source) override; | |
88 | |
89 ~RemoteDataSource() override; | |
90 | |
91 scoped_refptr<net::URLRequestContextGetter> request_context_; | |
92 | |
93 using PendingRequestsMap = std::map<const net::URLFetcher*, GotDataCallback>; | |
94 PendingRequestsMap pending_; | |
95 bool localhost_failed_; | |
96 | |
97 DISALLOW_COPY_AND_ASSIGN(RemoteDataSource); | |
98 }; | |
99 | |
100 RemoteDataSource::RemoteDataSource( | |
101 net::URLRequestContextGetter* request_context) | |
102 : request_context_(request_context), localhost_failed_(false) {} | |
103 | |
104 RemoteDataSource::~RemoteDataSource() { | |
105 for (const auto& pair : pending_) { | |
106 delete pair.first; | |
107 pair.second.Run( | |
108 new base::RefCountedStaticMemory(kHttpNotFound, strlen(kHttpNotFound))); | |
109 } | |
110 } | |
111 | |
112 std::string RemoteDataSource::GetSource() const { | |
113 return chrome::kChromeUIVrShellUIHost; | |
114 } | |
115 | |
116 void RemoteDataSource::StartDataRequest( | |
117 const std::string& path, | |
118 int render_process_id, | |
119 int render_frame_id, | |
120 const content::URLDataSource::GotDataCallback& callback) { | |
121 GURL url = GURL((localhost_failed_ ? kRemoteBaseAlt : kRemoteBase) + | |
122 (path.empty() ? std::string(kRemoteDefaultPath) : path)); | |
123 if (!url.is_valid()) { | |
124 callback.Run( | |
125 new base::RefCountedStaticMemory(kHttpNotFound, strlen(kHttpNotFound))); | |
126 return; | |
127 } | |
128 net::URLFetcher* fetcher = | |
129 net::URLFetcher::Create(url, net::URLFetcher::GET, this).release(); | |
130 pending_[fetcher] = callback; | |
131 fetcher->SetRequestContext(request_context_.get()); | |
132 fetcher->Start(); | |
133 } | |
134 | |
135 std::string RemoteDataSource::GetMimeType(const std::string& path) const { | |
136 return GetMimeTypeForPath(path); | |
137 } | |
138 | |
139 bool RemoteDataSource::ShouldAddContentSecurityPolicy() const { | |
140 return false; | |
141 } | |
142 | |
143 bool RemoteDataSource::ShouldDenyXFrameOptions() const { | |
144 return false; | |
145 } | |
146 | |
147 bool RemoteDataSource::ShouldServeMimeTypeAsContentTypeHeader() const { | |
148 return true; | |
149 } | |
150 | |
151 void RemoteDataSource::OnURLFetchComplete(const net::URLFetcher* source) { | |
152 DCHECK(source); | |
153 PendingRequestsMap::iterator it = pending_.find(source); | |
154 DCHECK(it != pending_.end()); | |
155 std::string response; | |
156 source->GetResponseAsString(&response); | |
157 | |
158 delete source; | |
159 if (response.empty() && !localhost_failed_) { | |
160 localhost_failed_ = true; | |
161 content::URLDataSource::GotDataCallback callback = it->second; | |
162 pending_.erase(it); | |
163 StartDataRequest("", 0, 0, callback); | |
xiyuan
2016/09/15 22:58:47
nit: Don't we lose path info with "" ?
mthiesse
2016/09/16 15:04:07
No, an empty string is what URL fetching passes to
mthiesse
2016/09/16 15:07:24
Actually, we should be checking here that the path
bshe
2016/09/16 18:27:53
It is easy to make mistakes(such as use a resource
| |
164 } else { | |
165 it->second.Run(base::RefCountedString::TakeString(&response)); | |
166 pending_.erase(it); | |
167 } | |
168 } | |
169 #else | |
170 content::WebUIDataSource* CreateVrShellUIHTMLSource() { | |
171 content::WebUIDataSource* source = | |
172 content::WebUIDataSource::Create(chrome::kChromeUIVrShellUIHost); | |
173 source->AddResourcePath("vr_shell_ui.js", IDR_VR_SHELL_UI_JS); | |
174 source->AddResourcePath("vr_shell_ui.css", IDR_VR_SHELL_UI_CSS); | |
175 source->SetDefaultResource(IDR_VR_SHELL_UI_HTML); | |
176 source->DisableI18nAndUseGzipForAllPaths(); | |
177 return source; | |
178 } | |
179 #endif | |
180 | |
181 } // namespace | |
182 | |
183 VrShellUIUI::VrShellUIUI(content::WebUI* web_ui) : WebUIController(web_ui) { | |
184 Profile* profile = Profile::FromWebUI(web_ui); | |
185 #if !defined(ENABLE_VR_SHELL_UI_DEV) | |
186 content::WebUIDataSource::Add(profile, CreateVrShellUIHTMLSource()); | |
187 #else | |
188 RemoteDataSource* source = new RemoteDataSource(profile->GetRequestContext()); | |
xiyuan
2016/09/15 22:58:47
nit: Get rid of |source| and fold into the next li
bshe
2016/09/16 18:27:53
Done.
| |
189 content::URLDataSource::Add(profile, source); | |
190 #endif | |
191 web_ui->AddMessageHandler(new VrShellUIMessageHandler); | |
192 } | |
193 | |
194 VrShellUIUI::~VrShellUIUI() {} | |
OLD | NEW |