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

Side by Side Diff: chrome/browser/ui/webui/vr_shell/vr_shell_ui_ui.cc

Issue 2341803003: Add chrome://vr-shell-ui internal page (Closed)
Patch Set: reivews Created 4 years, 3 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
« no previous file with comments | « chrome/browser/ui/webui/vr_shell/vr_shell_ui_ui.h ('k') | chrome/common/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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
12 #if !defined(ENABLE_VR_SHELL_UI_DEV)
13 #include "chrome/grit/browser_resources.h"
14 #include "content/public/browser/web_ui_data_source.h"
15 #else
16 #include <map>
17 #include "base/macros.h"
18 #include "base/memory/ref_counted_memory.h"
19 #include "base/strings/string_util.h"
20 #include "content/public/browser/url_data_source.h"
21 #include "net/url_request/url_fetcher.h"
22 #include "net/url_request/url_fetcher_delegate.h"
23 #include "net/url_request/url_request_context_getter.h"
24 #endif
25
26 namespace {
27
28 #if defined(ENABLE_VR_SHELL_UI_DEV)
29 std::string PathWithoutParams(const std::string& path) {
30 return GURL(std::string("chrome://vr-shell-ui/") + path).path().substr(1);
31 }
32
33 const char kRemoteBase[] = "http://localhost:8080/";
34 const char kRemoteBaseAlt[] = "https://jcarpenter.github.io/hoverboard-ui/";
35 const char kRemoteDefaultPath[] = "vr_shell_ui.html";
36 const char kHttpNotFound[] = "HTTP/1.1 404 Not Found\n\n";
37
38 // RemoteDataSource ---------------------------------------------------------
39
40 std::string GetMimeTypeForPath(const std::string& path) {
41 std::string filename = PathWithoutParams(path);
42 if (base::EndsWith(filename, ".html", base::CompareCase::INSENSITIVE_ASCII)) {
43 return "text/html";
44 } else if (base::EndsWith(filename, ".css",
45 base::CompareCase::INSENSITIVE_ASCII)) {
46 return "text/css";
47 } else if (base::EndsWith(filename, ".js",
48 base::CompareCase::INSENSITIVE_ASCII)) {
49 return "application/javascript";
50 } else if (base::EndsWith(filename, ".png",
51 base::CompareCase::INSENSITIVE_ASCII)) {
52 return "image/png";
53 } else if (base::EndsWith(filename, ".gif",
54 base::CompareCase::INSENSITIVE_ASCII)) {
55 return "image/gif";
56 } else if (base::EndsWith(filename, ".svg",
57 base::CompareCase::INSENSITIVE_ASCII)) {
58 return "image/svg+xml";
59 } else if (base::EndsWith(filename, ".manifest",
60 base::CompareCase::INSENSITIVE_ASCII)) {
61 return "text/cache-manifest";
62 }
63 return "text/html";
64 }
65
66 class RemoteDataSource : public content::URLDataSource,
67 public net::URLFetcherDelegate {
68 public:
69 using GotDataCallback = content::URLDataSource::GotDataCallback;
70
71 explicit RemoteDataSource(net::URLRequestContextGetter* request_context);
72
73 // content::URLDataSource implementation.
74 std::string GetSource() const override;
75 void StartDataRequest(const std::string& path,
76 int render_process_id,
77 int render_frame_id,
78 const GotDataCallback& callback) override;
79
80 private:
81 // content::URLDataSource overrides.
82 std::string GetMimeType(const std::string& path) const override;
83 bool ShouldAddContentSecurityPolicy() const override;
84 bool ShouldDenyXFrameOptions() const override;
85 bool ShouldServeMimeTypeAsContentTypeHeader() const override;
86
87 // net::URLFetcherDelegate overrides.
88 void OnURLFetchComplete(const net::URLFetcher* source) override;
89
90 ~RemoteDataSource() override;
91
92 scoped_refptr<net::URLRequestContextGetter> request_context_;
93
94 using PendingRequestsMap = std::map<const net::URLFetcher*, GotDataCallback>;
95 PendingRequestsMap pending_;
96 bool use_localhost_;
97
98 DISALLOW_COPY_AND_ASSIGN(RemoteDataSource);
99 };
100
101 RemoteDataSource::RemoteDataSource(
102 net::URLRequestContextGetter* request_context)
103 : request_context_(request_context), use_localhost_(true) {}
104
105 RemoteDataSource::~RemoteDataSource() {
106 for (const auto& pair : pending_) {
107 delete pair.first;
108 pair.second.Run(
109 new base::RefCountedStaticMemory(kHttpNotFound, strlen(kHttpNotFound)));
110 }
111 }
112
113 std::string RemoteDataSource::GetSource() const {
114 return chrome::kChromeUIVrShellUIHost;
115 }
116
117 void RemoteDataSource::StartDataRequest(
118 const std::string& path,
119 int render_process_id,
120 int render_frame_id,
121 const content::URLDataSource::GotDataCallback& callback) {
122 GURL url = GURL((use_localhost_ ? kRemoteBase : kRemoteBaseAlt) +
123 (path.empty() ? std::string(kRemoteDefaultPath) : path));
124 if (!url.is_valid()) {
125 callback.Run(
126 new base::RefCountedStaticMemory(kHttpNotFound, strlen(kHttpNotFound)));
127 return;
128 }
129 net::URLFetcher* fetcher =
130 net::URLFetcher::Create(url, net::URLFetcher::GET, this).release();
131 pending_[fetcher] = callback;
132 fetcher->SetRequestContext(request_context_.get());
133 fetcher->Start();
134 }
135
136 std::string RemoteDataSource::GetMimeType(const std::string& path) const {
137 return GetMimeTypeForPath(path);
138 }
139
140 bool RemoteDataSource::ShouldAddContentSecurityPolicy() const {
141 return false;
142 }
143
144 bool RemoteDataSource::ShouldDenyXFrameOptions() const {
145 return false;
146 }
147
148 bool RemoteDataSource::ShouldServeMimeTypeAsContentTypeHeader() const {
149 return true;
150 }
151
152 void RemoteDataSource::OnURLFetchComplete(const net::URLFetcher* source) {
153 DCHECK(source);
154 PendingRequestsMap::iterator it = pending_.find(source);
155 DCHECK(it != pending_.end());
156 std::string response;
157 source->GetResponseAsString(&response);
158
159 if (response.empty() && use_localhost_) {
160 if (source->GetOriginalURL().path().substr(1) == kRemoteDefaultPath) {
161 // Failed to request default page from local host, try request default
162 // page from remote server. Empty string indicates default page.
163 use_localhost_ = false;
164 content::URLDataSource::GotDataCallback callback = it->second;
165 pending_.erase(it);
xiyuan 2016/09/16 20:51:39 This and L170 should be moved to "delete source" l
bshe 2016/09/16 21:29:20 Done.
166 StartDataRequest("", 0, 0, callback);
167 }
168 } else {
169 it->second.Run(base::RefCountedString::TakeString(&response));
170 pending_.erase(it);
171 }
172 delete source;
173 }
174 #else
175 content::WebUIDataSource* CreateVrShellUIHTMLSource() {
176 content::WebUIDataSource* source =
177 content::WebUIDataSource::Create(chrome::kChromeUIVrShellUIHost);
178 source->AddResourcePath("vr_shell_ui.js", IDR_VR_SHELL_UI_JS);
179 source->AddResourcePath("vr_shell_ui.css", IDR_VR_SHELL_UI_CSS);
180 source->SetDefaultResource(IDR_VR_SHELL_UI_HTML);
181 source->DisableI18nAndUseGzipForAllPaths();
182 return source;
183 }
184 #endif
185
186 } // namespace
187
188 VrShellUIUI::VrShellUIUI(content::WebUI* web_ui) : WebUIController(web_ui) {
189 Profile* profile = Profile::FromWebUI(web_ui);
190 #if !defined(ENABLE_VR_SHELL_UI_DEV)
191 content::WebUIDataSource::Add(profile, CreateVrShellUIHTMLSource());
192 #else
193 content::URLDataSource::Add(
194 profile, new RemoteDataSource(profile->GetRequestContext()));
195 #endif
196 web_ui->AddMessageHandler(new VrShellUIMessageHandler);
197 }
198
199 VrShellUIUI::~VrShellUIUI() {}
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/vr_shell/vr_shell_ui_ui.h ('k') | chrome/common/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698