OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 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/page_load_metrics/observers/resource_tracking_page_load
_metrics_observer.h" | 5 #include "chrome/browser/page_load_metrics/observers/resource_tracking_page_load
_metrics_observer.h" |
6 | 6 |
| 7 #include "components/offline_pages/core/background/request_coordinator.h" |
| 8 #include "components/offline_pages/core/background/resource_data_type.h" |
| 9 #include "content/public/common/resource_type.h" |
| 10 |
| 11 namespace { |
| 12 offline_pages::ResourceDataType ConvertResourceTypeToResourceDataType( |
| 13 content::ResourceType type) { |
| 14 switch (type) { |
| 15 case (content::RESOURCE_TYPE_MAIN_FRAME): |
| 16 return offline_pages::ResourceDataType::TEXT_HTML; |
| 17 case (content::RESOURCE_TYPE_SUB_FRAME): |
| 18 return offline_pages::ResourceDataType::TEXT_HTML; |
| 19 case (content::RESOURCE_TYPE_STYLESHEET): |
| 20 return offline_pages::ResourceDataType::TEXT_CSS; |
| 21 case (content::RESOURCE_TYPE_SCRIPT): |
| 22 return offline_pages::ResourceDataType::TEXT_SCRIPT; |
| 23 case (content::RESOURCE_TYPE_IMAGE): |
| 24 return offline_pages::ResourceDataType::IMAGE; |
| 25 case (content::RESOURCE_TYPE_FONT_RESOURCE): |
| 26 return offline_pages::ResourceDataType::OTHER; |
| 27 case (content::RESOURCE_TYPE_SUB_RESOURCE): |
| 28 return offline_pages::ResourceDataType::OTHER; |
| 29 case (content::RESOURCE_TYPE_OBJECT): |
| 30 return offline_pages::ResourceDataType::OTHER; |
| 31 case (content::RESOURCE_TYPE_MEDIA): |
| 32 return offline_pages::ResourceDataType::MEDIA; |
| 33 case (content::RESOURCE_TYPE_WORKER): |
| 34 return offline_pages::ResourceDataType::OTHER; |
| 35 case (content::RESOURCE_TYPE_SHARED_WORKER): |
| 36 return offline_pages::ResourceDataType::OTHER; |
| 37 case (content::RESOURCE_TYPE_PREFETCH): |
| 38 return offline_pages::ResourceDataType::OTHER; |
| 39 case (content::RESOURCE_TYPE_FAVICON): |
| 40 return offline_pages::ResourceDataType::OTHER; |
| 41 case (content::RESOURCE_TYPE_XHR): |
| 42 return offline_pages::ResourceDataType::XHR; |
| 43 case (content::RESOURCE_TYPE_PING): |
| 44 return offline_pages::ResourceDataType::OTHER; |
| 45 case (content::RESOURCE_TYPE_SERVICE_WORKER): |
| 46 return offline_pages::ResourceDataType::OTHER; |
| 47 case (content::RESOURCE_TYPE_CSP_REPORT): |
| 48 return offline_pages::ResourceDataType::OTHER; |
| 49 case (content::RESOURCE_TYPE_PLUGIN_RESOURCE): |
| 50 return offline_pages::ResourceDataType::OTHER; |
| 51 default: |
| 52 return offline_pages::ResourceDataType::OTHER; |
| 53 } |
| 54 } |
| 55 } // namespace |
| 56 |
7 namespace page_load_metrics { | 57 namespace page_load_metrics { |
8 | 58 |
9 ResourceTrackingPageLoadMetricsObserver:: | 59 ResourceTrackingPageLoadMetricsObserver:: |
10 ResourceTrackingPageLoadMetricsObserver() | 60 ResourceTrackingPageLoadMetricsObserver( |
11 : started_count_(0), completed_count_(0) {} | 61 offline_pages::ResourceTrackerObserver* request_coordinator) |
| 62 : started_count_(0), |
| 63 completed_count_(0), |
| 64 request_coordinator_(request_coordinator) {} |
12 ResourceTrackingPageLoadMetricsObserver:: | 65 ResourceTrackingPageLoadMetricsObserver:: |
13 ~ResourceTrackingPageLoadMetricsObserver() {} | 66 ~ResourceTrackingPageLoadMetricsObserver() {} |
14 | 67 |
15 void ResourceTrackingPageLoadMetricsObserver::OnStartedResource( | 68 void ResourceTrackingPageLoadMetricsObserver::OnStartedResource( |
16 const ExtraRequestStartInfo& extra_request_start_info) { | 69 const ExtraRequestStartInfo& extra_request_start_info) { |
17 // TODO(petewiL): Store this by type. | 70 // TODO(petewiL): Store this by type. Until we do, only look at images. |
18 ++started_count_; | 71 if (extra_request_start_info.resource_type == content::RESOURCE_TYPE_IMAGE) { |
| 72 ++started_count_; |
| 73 InformObservers(content::ResourceType::RESOURCE_TYPE_IMAGE, started_count_, |
| 74 completed_count_); |
| 75 } |
19 } | 76 } |
20 | 77 |
21 void ResourceTrackingPageLoadMetricsObserver::OnLoadedResource( | 78 void ResourceTrackingPageLoadMetricsObserver::OnLoadedResource( |
22 const ExtraRequestCompleteInfo& extra_request_complete_info) { | 79 const ExtraRequestCompleteInfo& extra_request_complete_info) { |
23 // TODO(petewil): Check to see if the type of the request changed. If it did, | 80 // TODO(petewil): Check to see if the type of the request changed. If it did, |
24 // update the old and new types for the started type. Then update by type for | 81 // update the old and new types for the started type. Then update by type for |
25 // the completed type. | 82 // the completed type. Maybe we can just skip that, and count XHR as its own |
26 ++completed_count_; | 83 // type. |
| 84 if (extra_request_complete_info.resource_type == |
| 85 content::RESOURCE_TYPE_IMAGE) { |
| 86 ++completed_count_; |
| 87 InformObservers(content::ResourceType::RESOURCE_TYPE_IMAGE, started_count_, |
| 88 completed_count_); |
| 89 } |
27 } | 90 } |
28 | 91 |
29 void ResourceTrackingPageLoadMetricsObserver::GetCountsForTypeForTesting( | 92 void ResourceTrackingPageLoadMetricsObserver::GetCountsForTypeForTesting( |
30 const content::ResourceType type, | 93 const content::ResourceType type, |
31 int64_t* started_count, | 94 int64_t* started_count, |
32 int64_t* completed_count) { | 95 int64_t* completed_count) { |
33 if (started_count != nullptr) | 96 if (started_count != nullptr) |
34 *started_count = started_count_; | 97 *started_count = started_count_; |
35 if (completed_count != nullptr) | 98 if (completed_count != nullptr) |
36 *completed_count = completed_count_; | 99 *completed_count = completed_count_; |
37 } | 100 } |
38 | 101 |
| 102 void ResourceTrackingPageLoadMetricsObserver::InformObservers( |
| 103 const content::ResourceType type, |
| 104 int64_t started_count, |
| 105 int64_t completed_count) { |
| 106 if (request_coordinator_) { |
| 107 offline_pages::ResourceDataType converted_type = |
| 108 ConvertResourceTypeToResourceDataType(type); |
| 109 request_coordinator_->ObserveResourceTracking(converted_type, started_count, |
| 110 completed_count); |
| 111 } |
| 112 } |
| 113 |
39 } // namespace page_load_metrics | 114 } // namespace page_load_metrics |
OLD | NEW |