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

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

Issue 2702503003: [Dial] Refactor DialRegistry and DeviceDescriptionFetcher so they can be used by MediaSinkService (Closed)
Patch Set: resolve code review comments from Derek Created 3 years, 9 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
1 // Copyright (c) 2017 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2017 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/extensions/api/dial/device_description_fetcher.h"
6
5 #include "base/strings/string_util.h" 7 #include "base/strings/string_util.h"
6 #include "base/strings/stringprintf.h" 8 #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/extensions/api/dial/dial_device_data.h"
9 #include "chrome/browser/profiles/profile.h" 10 #include "chrome/browser/profiles/profile.h"
10 #include "content/public/browser/browser_thread.h" 11 #include "content/public/browser/browser_thread.h"
11 #include "net/base/load_flags.h" 12 #include "net/base/load_flags.h"
12 #include "net/http/http_response_headers.h" 13 #include "net/http/http_response_headers.h"
13 #include "net/http/http_status_code.h" 14 #include "net/http/http_status_code.h"
14 #include "net/http/http_util.h" 15 #include "net/http/http_util.h"
15 #include "net/url_request/url_fetcher.h" 16 #include "net/url_request/url_fetcher.h"
17 #include "net/url_request/url_request_context_getter.h"
16 18
17 using content::BrowserThread; 19 using content::BrowserThread;
18 20
19 constexpr char kApplicationUrlHeaderName[] = "Application-URL"; 21 constexpr char kApplicationUrlHeaderName[] = "Application-URL";
20 constexpr int kMaxRetries = 3; 22 constexpr int kMaxRetries = 3;
21 // DIAL devices are unlikely to expose uPnP functions other than DIAL, so 256kb 23 // DIAL devices are unlikely to expose uPnP functions other than DIAL, so 256kb
22 // should be more than sufficient. 24 // should be more than sufficient.
23 constexpr int kMaxDescriptionSizeBytes = 262144; 25 constexpr int kMaxDescriptionSizeBytes = 262144;
24 26
25 namespace extensions { 27 namespace extensions {
26 namespace api { 28 namespace api {
27 namespace dial { 29 namespace dial {
28 30
29 DeviceDescriptionFetcher::DeviceDescriptionFetcher( 31 DeviceDescriptionFetcher::DeviceDescriptionFetcher(
30 const GURL& device_description_url, 32 const GURL& device_description_url,
31 Profile* profile, 33 net::URLRequestContextGetter* request_context,
32 base::OnceCallback<void(const DialDeviceDescriptionData&)> success_cb, 34 base::OnceCallback<void(const DialDeviceDescriptionData&)> success_cb,
33 base::OnceCallback<void(const std::string&)> error_cb) 35 base::OnceCallback<void(const std::string&)> error_cb)
34 : device_description_url_(device_description_url), 36 : device_description_url_(device_description_url),
35 profile_(profile), 37 request_context_(request_context),
36 success_cb_(std::move(success_cb)), 38 success_cb_(std::move(success_cb)),
37 error_cb_(std::move(error_cb)) { 39 error_cb_(std::move(error_cb)) {
38 DCHECK_CURRENTLY_ON(BrowserThread::UI); 40 DCHECK(request_context_);
39 DCHECK(profile_);
40 DCHECK(device_description_url_.is_valid()); 41 DCHECK(device_description_url_.is_valid());
41 } 42 }
42 43
43 DeviceDescriptionFetcher::~DeviceDescriptionFetcher() { 44 DeviceDescriptionFetcher::~DeviceDescriptionFetcher() {
44 DCHECK_CURRENTLY_ON(BrowserThread::UI); 45 DCHECK(thread_checker_.CalledOnValidThread());
45 } 46 }
46 47
47 void DeviceDescriptionFetcher::Start() { 48 void DeviceDescriptionFetcher::Start() {
48 DCHECK_CURRENTLY_ON(BrowserThread::UI); 49 DCHECK(thread_checker_.CalledOnValidThread());
49 DCHECK(!fetcher_); 50 DCHECK(!fetcher_);
50 51
51 // DIAL returns device descriptions via GET request. 52 // DIAL returns device descriptions via GET request.
52 fetcher_ = 53 fetcher_ =
53 net::URLFetcher::Create(kURLFetcherIDForTest, device_description_url_, 54 net::URLFetcher::Create(kURLFetcherIDForTest, device_description_url_,
54 net::URLFetcher::GET, this); 55 net::URLFetcher::GET, this);
55 56
56 // net::LOAD_BYPASS_PROXY: Proxies almost certainly hurt more cases than they 57 // net::LOAD_BYPASS_PROXY: Proxies almost certainly hurt more cases than they
57 // help. 58 // help.
58 // net::LOAD_DISABLE_CACHE: The request should not touch the cache. 59 // net::LOAD_DISABLE_CACHE: The request should not touch the cache.
59 // net::LOAD_DO_NOT_{SAVE,SEND}_COOKIES: The request should not touch cookies. 60 // net::LOAD_DO_NOT_{SAVE,SEND}_COOKIES: The request should not touch cookies.
60 // net::LOAD_DO_NOT_SEND_AUTH_DATA: The request should not send auth data. 61 // net::LOAD_DO_NOT_SEND_AUTH_DATA: The request should not send auth data.
61 fetcher_->SetLoadFlags(net::LOAD_BYPASS_PROXY | net::LOAD_DISABLE_CACHE | 62 fetcher_->SetLoadFlags(net::LOAD_BYPASS_PROXY | net::LOAD_DISABLE_CACHE |
62 net::LOAD_DO_NOT_SAVE_COOKIES | 63 net::LOAD_DO_NOT_SAVE_COOKIES |
63 net::LOAD_DO_NOT_SEND_COOKIES | 64 net::LOAD_DO_NOT_SEND_COOKIES |
64 net::LOAD_DO_NOT_SEND_AUTH_DATA); 65 net::LOAD_DO_NOT_SEND_AUTH_DATA);
65 66
66 // Section 5.4 of the DIAL spec prohibits redirects. 67 // Section 5.4 of the DIAL spec prohibits redirects.
67 fetcher_->SetStopOnRedirect(true); 68 fetcher_->SetStopOnRedirect(true);
68 69
69 // Allow the fetcher to retry on 5XX responses and ERR_NETWORK_CHANGED. 70 // Allow the fetcher to retry on 5XX responses and ERR_NETWORK_CHANGED.
70 fetcher_->SetMaxRetriesOn5xx(kMaxRetries); 71 fetcher_->SetMaxRetriesOn5xx(kMaxRetries);
71 fetcher_->SetAutomaticallyRetryOnNetworkChanges(kMaxRetries); 72 fetcher_->SetAutomaticallyRetryOnNetworkChanges(kMaxRetries);
72 73
73 fetcher_->SetRequestContext(profile_->GetRequestContext()); 74 fetcher_->SetRequestContext(request_context_.get());
74 fetcher_->Start(); 75 fetcher_->Start();
75 } 76 }
76 77
77 void DeviceDescriptionFetcher::OnURLFetchComplete( 78 void DeviceDescriptionFetcher::OnURLFetchComplete(
78 const net::URLFetcher* source) { 79 const net::URLFetcher* source) {
79 DCHECK_EQ(fetcher_.get(), source); 80 DCHECK_EQ(fetcher_.get(), source);
80 81
81 if (source->GetResponseCode() != net::HTTP_OK) { 82 if (source->GetResponseCode() != net::HTTP_OK) {
82 ReportError( 83 ReportError(
83 base::StringPrintf("HTTP %d: Unable to fetch device description", 84 base::StringPrintf("HTTP %d: Unable to fetch device description",
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 int64_t current, 142 int64_t current,
142 int64_t total) {} 143 int64_t total) {}
143 144
144 void DeviceDescriptionFetcher::ReportError(const std::string& message) { 145 void DeviceDescriptionFetcher::ReportError(const std::string& message) {
145 std::move(error_cb_).Run(message); 146 std::move(error_cb_).Run(message);
146 } 147 }
147 148
148 } // namespace dial 149 } // namespace dial
149 } // namespace api 150 } // namespace api
150 } // namespace extensions 151 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698