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

Side by Side Diff: chrome/browser/extensions/api/dial/device_description_fetcher.cc

Issue 2583853004: [chrome.dial] Adds chrome.dial.fetchDeviceDecription API. (Closed)
Patch Set: Respond to imcheng@ comments Created 3 years, 11 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
OLDNEW
(Empty)
1 // Copyright (c) 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 "base/strings/string_util.h"
6 #include "base/strings/stringprintf.h"
7 #include "chrome/browser/extensions/api/dial/device_description_fetcher.h"
8 #include "chrome/browser/extensions/api/dial/dial_device_data.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "net/base/load_flags.h"
12 #include "net/http/http_response_headers.h"
13 #include "net/http/http_status_code.h"
14 #include "net/http/http_util.h"
15 #include "net/url_request/url_fetcher.h"
16
17 using content::BrowserThread;
18
19 constexpr char kApplicationUrlHeaderName[] = "Application-URL";
20 constexpr int kMaxRetries = 3;
21 constexpr int kMaxDescriptionSizeBytes = 262144;
22
23 namespace extensions {
24
25 DeviceDescriptionFetcher::DeviceDescriptionFetcher(
26 const GURL& device_description_url,
27 Profile* profile,
28 base::OnceCallback<void(const DialDeviceDescription&)> success_cb,
29 base::OnceCallback<void(const std::string&)> error_cb)
30 : device_description_url_(device_description_url),
31 profile_(profile),
32 success_cb_(std::move(success_cb)),
33 error_cb_(std::move(error_cb)) {
34 DCHECK_CURRENTLY_ON(BrowserThread::UI);
35 DCHECK(profile_);
36 DCHECK(device_description_url_.is_valid());
37 }
38
39 DeviceDescriptionFetcher::~DeviceDescriptionFetcher() {
40 DCHECK_CURRENTLY_ON(BrowserThread::UI);
41 }
42
43 void DeviceDescriptionFetcher::Start() {
44 DCHECK_CURRENTLY_ON(BrowserThread::UI);
45 DCHECK(!fetcher_);
46 fetcher_ = net::URLFetcher::Create(kURLFetcherID, device_description_url_,
47 net::URLFetcher::GET, this);
48
49 // The request should not side effect cache or the cookie store or send auth
50 // data. Proxies almost certainly hurt more cases than they help.
51 fetcher_->SetLoadFlags(net::LOAD_BYPASS_PROXY | net::LOAD_DISABLE_CACHE |
52 net::LOAD_DO_NOT_SAVE_COOKIES |
53 net::LOAD_DO_NOT_SEND_COOKIES |
54 net::LOAD_DO_NOT_SEND_AUTH_DATA);
55
56 // Technically uPnP allows devices to issue HTTP 307 redirects (sec
57 // 2.1). However, we want to enforce that redirects are not followed to hosts
58 // other than the DIAL device; and there is no logical reason why a device
59 // needs to redirect a request for the device description.
60 //
61 // We can relax this restriction if there are well-behaved devices that
62 // actually need redirects.
63 fetcher_->SetStopOnRedirect(true);
64
65 // Allow the fetcher to retry on 5XX responses and ERR_NETWORK_CHANGED.
66 fetcher_->SetMaxRetriesOn5xx(kMaxRetries);
67 fetcher_->SetAutomaticallyRetryOnNetworkChanges(kMaxRetries);
68
69 fetcher_->SetRequestContext(profile_->GetRequestContext());
70 fetcher_->Start();
71 }
72
73 void DeviceDescriptionFetcher::OnURLFetchComplete(
74 const net::URLFetcher* source) {
75 if (source->GetResponseCode() != net::HTTP_OK) {
76 ReportError(
77 base::StringPrintf("HTTP %d: Unable to fetch device description",
78 source->GetResponseCode()));
79 return;
80 }
81
82 const net::HttpResponseHeaders* headers = source->GetResponseHeaders();
83
84 // NOTE: The uPnP spec requires devices to set a Content-Type: header of
85 // text/xml; charset="utf-8" (sec 2.11). However Chromecast (and possibly
86 // other devices) do not comply, so not checking this header specifcially.
87
88 std::string app_url_header;
89 if (!headers->GetNormalizedHeader(kApplicationUrlHeaderName,
90 &app_url_header) ||
91 app_url_header.empty()) {
92 ReportError("Missing or empty Application-URL:");
93 return;
94 }
95
96 // Section 5.4 of the DIAL spec implies that the Application URL should not
97 // have path, query or fragment...unsure if that can be enforced.
98 GURL app_url(app_url_header);
99 if (!app_url.is_valid() || !app_url.SchemeIs("http") ||
100 !app_url.HostIsIPAddress() ||
101 app_url.host() != device_description_url_.host()) {
102 ReportError(base::StringPrintf("Invalid Application-URL: %s",
103 app_url_header.c_str()));
104 return;
105 }
106
107 if (source->GetReceivedResponseContentLength() > kMaxDescriptionSizeBytes) {
108 ReportError("Response too large");
109 return;
110 }
111
112 std::string device_description;
113 if (!source->GetResponseAsString(&device_description) ||
114 device_description.empty()) {
115 ReportError("Missing or empty response");
116 return;
117 }
118
119 if (!base::IsStringUTF8(device_description)) {
120 ReportError("Invalid response encoding");
121 return;
122 }
123
124 std::move(success_cb_)
125 .Run(DialDeviceDescription(device_description, app_url));
126 }
127
128 void DeviceDescriptionFetcher::OnURLFetchDownloadProgress(
129 const net::URLFetcher* source,
130 int64_t current,
131 int64_t total,
132 int64_t current_network_bytes) {}
133
134 void DeviceDescriptionFetcher::OnURLFetchUploadProgress(
135 const net::URLFetcher* source,
136 int64_t current,
137 int64_t total) {}
138
139 void DeviceDescriptionFetcher::ReportError(const std::string& message) {
140 std::move(error_cb_).Run(message);
141 }
142
143 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698