| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/ui/webui/vr_shell/vr_shell_ui_ui.h" | 5 #include "chrome/browser/ui/webui/vr_shell/vr_shell_ui_ui.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <unordered_set> | 8 #include <unordered_set> |
| 9 | 9 |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 #endif | 30 #endif |
| 31 | 31 |
| 32 namespace { | 32 namespace { |
| 33 | 33 |
| 34 #if defined(ENABLE_VR_SHELL_UI_DEV) | 34 #if defined(ENABLE_VR_SHELL_UI_DEV) |
| 35 std::string PathWithoutParams(const std::string& path) { | 35 std::string PathWithoutParams(const std::string& path) { |
| 36 return GURL(std::string("chrome://vr-shell-ui/") + path).path().substr(1); | 36 return GURL(std::string("chrome://vr-shell-ui/") + path).path().substr(1); |
| 37 } | 37 } |
| 38 | 38 |
| 39 const char kRemoteBase[] = "http://localhost:8080/"; | 39 const char kRemoteBase[] = "http://localhost:8080/"; |
| 40 const char kRemoteBaseAlt[] = "https://jcarpenter.github.io/hoverboard-ui/"; | |
| 41 const char kRemoteDefaultPath[] = "vr_shell_ui.html"; | 40 const char kRemoteDefaultPath[] = "vr_shell_ui.html"; |
| 42 const char kHttpNotFound[] = "HTTP/1.1 404 Not Found\n\n"; | 41 const char kHttpNotFound[] = "HTTP/1.1 404 Not Found\n\n"; |
| 43 | 42 |
| 44 // RemoteDataSource --------------------------------------------------------- | 43 // RemoteDataSource --------------------------------------------------------- |
| 45 | 44 |
| 46 std::string GetMimeTypeForPath(const std::string& path) { | 45 std::string GetMimeTypeForPath(const std::string& path) { |
| 47 std::string filename = PathWithoutParams(path); | 46 std::string filename = PathWithoutParams(path); |
| 48 if (base::EndsWith(filename, ".html", base::CompareCase::INSENSITIVE_ASCII)) { | 47 if (base::EndsWith(filename, ".html", base::CompareCase::INSENSITIVE_ASCII)) { |
| 49 return "text/html"; | 48 return "text/html"; |
| 50 } else if (base::EndsWith(filename, ".css", | 49 } else if (base::EndsWith(filename, ".css", |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 | 93 |
| 95 // net::URLFetcherDelegate overrides. | 94 // net::URLFetcherDelegate overrides. |
| 96 void OnURLFetchComplete(const net::URLFetcher* source) override; | 95 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 97 | 96 |
| 98 ~RemoteDataSource() override; | 97 ~RemoteDataSource() override; |
| 99 | 98 |
| 100 scoped_refptr<net::URLRequestContextGetter> request_context_; | 99 scoped_refptr<net::URLRequestContextGetter> request_context_; |
| 101 | 100 |
| 102 using PendingRequestsMap = std::map<const net::URLFetcher*, GotDataCallback>; | 101 using PendingRequestsMap = std::map<const net::URLFetcher*, GotDataCallback>; |
| 103 PendingRequestsMap pending_; | 102 PendingRequestsMap pending_; |
| 104 bool use_localhost_; | |
| 105 | 103 |
| 106 DISALLOW_COPY_AND_ASSIGN(RemoteDataSource); | 104 DISALLOW_COPY_AND_ASSIGN(RemoteDataSource); |
| 107 }; | 105 }; |
| 108 | 106 |
| 109 RemoteDataSource::RemoteDataSource( | 107 RemoteDataSource::RemoteDataSource( |
| 110 net::URLRequestContextGetter* request_context) | 108 net::URLRequestContextGetter* request_context) |
| 111 : request_context_(request_context), use_localhost_(true) {} | 109 : request_context_(request_context) {} |
| 112 | 110 |
| 113 RemoteDataSource::~RemoteDataSource() { | 111 RemoteDataSource::~RemoteDataSource() { |
| 114 for (const auto& pair : pending_) { | 112 for (const auto& pair : pending_) { |
| 115 delete pair.first; | 113 delete pair.first; |
| 116 pair.second.Run( | 114 pair.second.Run( |
| 117 new base::RefCountedStaticMemory(kHttpNotFound, strlen(kHttpNotFound))); | 115 new base::RefCountedStaticMemory(kHttpNotFound, strlen(kHttpNotFound))); |
| 118 } | 116 } |
| 119 } | 117 } |
| 120 | 118 |
| 121 std::string RemoteDataSource::GetSource() const { | 119 std::string RemoteDataSource::GetSource() const { |
| 122 return chrome::kChromeUIVrShellUIHost; | 120 return chrome::kChromeUIVrShellUIHost; |
| 123 } | 121 } |
| 124 | 122 |
| 125 void RemoteDataSource::StartDataRequest( | 123 void RemoteDataSource::StartDataRequest( |
| 126 const std::string& path, | 124 const std::string& path, |
| 127 const content::ResourceRequestInfo::WebContentsGetter& wc_getter, | 125 const content::ResourceRequestInfo::WebContentsGetter& wc_getter, |
| 128 const content::URLDataSource::GotDataCallback& callback) { | 126 const content::URLDataSource::GotDataCallback& callback) { |
| 129 GURL url = GURL((use_localhost_ ? kRemoteBase : kRemoteBaseAlt) + | 127 GURL url = GURL(kRemoteBase + |
| 130 (path.empty() ? std::string(kRemoteDefaultPath) : path)); | 128 (path.empty() ? std::string(kRemoteDefaultPath) : path)); |
| 131 if (!url.is_valid()) { | 129 if (!url.is_valid()) { |
| 132 callback.Run( | 130 callback.Run( |
| 133 new base::RefCountedStaticMemory(kHttpNotFound, strlen(kHttpNotFound))); | 131 new base::RefCountedStaticMemory(kHttpNotFound, strlen(kHttpNotFound))); |
| 134 return; | 132 return; |
| 135 } | 133 } |
| 136 net::URLFetcher* fetcher = | 134 net::URLFetcher* fetcher = |
| 137 net::URLFetcher::Create(url, net::URLFetcher::GET, this).release(); | 135 net::URLFetcher::Create(url, net::URLFetcher::GET, this).release(); |
| 138 | 136 |
| 139 fetcher->AddExtraRequestHeader("Cache-Control: no-cache"); | 137 fetcher->AddExtraRequestHeader("Cache-Control: no-cache"); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 158 bool RemoteDataSource::ShouldServeMimeTypeAsContentTypeHeader() const { | 156 bool RemoteDataSource::ShouldServeMimeTypeAsContentTypeHeader() const { |
| 159 return true; | 157 return true; |
| 160 } | 158 } |
| 161 | 159 |
| 162 void RemoteDataSource::OnURLFetchComplete(const net::URLFetcher* source) { | 160 void RemoteDataSource::OnURLFetchComplete(const net::URLFetcher* source) { |
| 163 DCHECK(source); | 161 DCHECK(source); |
| 164 PendingRequestsMap::iterator it = pending_.find(source); | 162 PendingRequestsMap::iterator it = pending_.find(source); |
| 165 DCHECK(it != pending_.end()); | 163 DCHECK(it != pending_.end()); |
| 166 std::string response; | 164 std::string response; |
| 167 source->GetResponseAsString(&response); | 165 source->GetResponseAsString(&response); |
| 168 | |
| 169 if (response.empty() && use_localhost_) { | |
| 170 if (source->GetOriginalURL().path().substr(1) == kRemoteDefaultPath) { | |
| 171 // Failed to request default page from local host, try request default | |
| 172 // page from remote server. Empty string indicates default page. | |
| 173 use_localhost_ = false; | |
| 174 content::URLDataSource::GotDataCallback callback = it->second; | |
| 175 StartDataRequest(std::string(), | |
| 176 content::ResourceRequestInfo::WebContentsGetter(), | |
| 177 callback); | |
| 178 } | |
| 179 } else { | |
| 180 it->second.Run(base::RefCountedString::TakeString(&response)); | |
| 181 } | |
| 182 delete source; | 166 delete source; |
| 167 it->second.Run(base::RefCountedString::TakeString(&response)); |
| 183 pending_.erase(it); | 168 pending_.erase(it); |
| 184 } | 169 } |
| 185 #else | 170 #else |
| 186 content::WebUIDataSource* CreateVrShellUIHTMLSource() { | 171 content::WebUIDataSource* CreateVrShellUIHTMLSource() { |
| 187 content::WebUIDataSource* source = | 172 content::WebUIDataSource* source = |
| 188 content::WebUIDataSource::Create(chrome::kChromeUIVrShellUIHost); | 173 content::WebUIDataSource::Create(chrome::kChromeUIVrShellUIHost); |
| 189 source->UseGzip(std::unordered_set<std::string>() /* excluded_paths */); | 174 source->UseGzip(std::unordered_set<std::string>() /* excluded_paths */); |
| 190 source->AddResourcePath("vr_shell_ui.css", IDR_VR_SHELL_UI_CSS); | 175 source->AddResourcePath("vr_shell_ui.css", IDR_VR_SHELL_UI_CSS); |
| 191 source->AddResourcePath("vr_shell_ui.js", IDR_VR_SHELL_UI_JS); | 176 source->AddResourcePath("vr_shell_ui.js", IDR_VR_SHELL_UI_JS); |
| 192 source->AddResourcePath("vr_shell_ui_api.js", IDR_VR_SHELL_UI_API_JS); | 177 source->AddResourcePath("vr_shell_ui_api.js", IDR_VR_SHELL_UI_API_JS); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 216 #if !defined(ENABLE_VR_SHELL_UI_DEV) | 201 #if !defined(ENABLE_VR_SHELL_UI_DEV) |
| 217 content::WebUIDataSource::Add(profile, CreateVrShellUIHTMLSource()); | 202 content::WebUIDataSource::Add(profile, CreateVrShellUIHTMLSource()); |
| 218 #else | 203 #else |
| 219 content::URLDataSource::Add( | 204 content::URLDataSource::Add( |
| 220 profile, new RemoteDataSource(profile->GetRequestContext())); | 205 profile, new RemoteDataSource(profile->GetRequestContext())); |
| 221 #endif | 206 #endif |
| 222 web_ui->AddMessageHandler(base::MakeUnique<VrShellUIMessageHandler>()); | 207 web_ui->AddMessageHandler(base::MakeUnique<VrShellUIMessageHandler>()); |
| 223 } | 208 } |
| 224 | 209 |
| 225 VrShellUIUI::~VrShellUIUI() {} | 210 VrShellUIUI::~VrShellUIUI() {} |
| OLD | NEW |