OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "components/dom_distiller/core/distiller_url_fetcher.h" | 5 #include "components/dom_distiller/core/distiller_url_fetcher.h" |
6 | 6 |
7 #include "components/data_use_measurement/core/data_use_user_data.h" | 7 #include "components/data_use_measurement/core/data_use_user_data.h" |
8 #include "net/http/http_status_code.h" | 8 #include "net/http/http_status_code.h" |
| 9 #include "net/traffic_annotation/network_traffic_annotation.h" |
9 #include "net/url_request/url_fetcher.h" | 10 #include "net/url_request/url_fetcher.h" |
10 #include "net/url_request/url_fetcher_delegate.h" | 11 #include "net/url_request/url_fetcher_delegate.h" |
11 #include "net/url_request/url_request_context_getter.h" | 12 #include "net/url_request/url_request_context_getter.h" |
12 #include "net/url_request/url_request_status.h" | 13 #include "net/url_request/url_request_status.h" |
13 #include "url/gurl.h" | 14 #include "url/gurl.h" |
14 | 15 |
15 using net::URLFetcher; | 16 using net::URLFetcher; |
16 | 17 |
17 namespace dom_distiller { | 18 namespace dom_distiller { |
18 | 19 |
(...skipping 21 matching lines...) Expand all Loading... |
40 // Don't allow a fetch if one is pending. | 41 // Don't allow a fetch if one is pending. |
41 DCHECK(!url_fetcher_ || !url_fetcher_->GetStatus().is_io_pending()); | 42 DCHECK(!url_fetcher_ || !url_fetcher_->GetStatus().is_io_pending()); |
42 callback_ = callback; | 43 callback_ = callback; |
43 url_fetcher_ = CreateURLFetcher(context_getter_, url); | 44 url_fetcher_ = CreateURLFetcher(context_getter_, url); |
44 url_fetcher_->Start(); | 45 url_fetcher_->Start(); |
45 } | 46 } |
46 | 47 |
47 std::unique_ptr<URLFetcher> DistillerURLFetcher::CreateURLFetcher( | 48 std::unique_ptr<URLFetcher> DistillerURLFetcher::CreateURLFetcher( |
48 net::URLRequestContextGetter* context_getter, | 49 net::URLRequestContextGetter* context_getter, |
49 const std::string& url) { | 50 const std::string& url) { |
| 51 net::NetworkTrafficAnnotationTag traffic_annotation = |
| 52 net::DefineNetworkTrafficAnnotation("dom_distiller", R"( |
| 53 semantics { |
| 54 sender: "DOM Distiller" |
| 55 description: |
| 56 "Chromium provides Mobile-friendly view on Android phones when the " |
| 57 "web page contains an article, and is not mobile-friendly. If the " |
| 58 "user enters Mobile-friendly view, the main content would be " |
| 59 "extracted and reflowed in a simple layout for better readability. " |
| 60 "On iOS, apps can add URLs to the Reading List in Chromium. When " |
| 61 "opening the entries in the Reading List with no or limited " |
| 62 "network, the simple layout would be shown. DOM distiller is the " |
| 63 "backend service for Mobile-friendly view and Reading List." |
| 64 trigger: |
| 65 "When the user enters Mobile-friendly view on Android phones, or " |
| 66 "adds entries to the Reading List on iOS. Note that Reading List " |
| 67 "entries can be added from other apps." |
| 68 data: |
| 69 "URLs of the required website resources to fetch." |
| 70 destination: WEBSITE |
| 71 } |
| 72 policy { |
| 73 cookies_allowed: true |
| 74 cookies_store: "user" |
| 75 setting: "Users can enable or disable Mobile-friendly view by " |
| 76 "toggling chrome://flags#reader-mode-heuristics in Chromium on " |
| 77 "Android." |
| 78 policy_exception_justification: |
| 79 "Not implemented, considered not useful as no content is being " |
| 80 "uploaded; this request merely downloads the resources on the web." |
| 81 })"); |
50 std::unique_ptr<net::URLFetcher> fetcher = | 82 std::unique_ptr<net::URLFetcher> fetcher = |
51 URLFetcher::Create(GURL(url), URLFetcher::GET, this); | 83 URLFetcher::Create(GURL(url), URLFetcher::GET, this, traffic_annotation); |
52 data_use_measurement::DataUseUserData::AttachToFetcher( | 84 data_use_measurement::DataUseUserData::AttachToFetcher( |
53 fetcher.get(), data_use_measurement::DataUseUserData::DOM_DISTILLER); | 85 fetcher.get(), data_use_measurement::DataUseUserData::DOM_DISTILLER); |
54 fetcher->SetRequestContext(context_getter); | 86 fetcher->SetRequestContext(context_getter); |
55 static const int kMaxRetries = 5; | 87 static const int kMaxRetries = 5; |
56 fetcher->SetMaxRetriesOn5xx(kMaxRetries); | 88 fetcher->SetMaxRetriesOn5xx(kMaxRetries); |
57 return fetcher; | 89 return fetcher; |
58 } | 90 } |
59 | 91 |
60 void DistillerURLFetcher::OnURLFetchComplete( | 92 void DistillerURLFetcher::OnURLFetchComplete( |
61 const URLFetcher* source) { | 93 const URLFetcher* source) { |
62 std::string response; | 94 std::string response; |
63 if (source && source->GetStatus().is_success() && | 95 if (source && source->GetStatus().is_success() && |
64 source->GetResponseCode() == net::HTTP_OK) { | 96 source->GetResponseCode() == net::HTTP_OK) { |
65 // Only copy over the data if the request was successful. Insert | 97 // Only copy over the data if the request was successful. Insert |
66 // an empty string into the proto otherwise. | 98 // an empty string into the proto otherwise. |
67 source->GetResponseAsString(&response); | 99 source->GetResponseAsString(&response); |
68 } | 100 } |
69 callback_.Run(response); | 101 callback_.Run(response); |
70 } | 102 } |
71 | 103 |
72 } // namespace dom_distiller | 104 } // namespace dom_distiller |
OLD | NEW |