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

Side by Side Diff: content/browser/service_worker/service_worker_fetch_dispatcher.cc

Issue 1795863006: service worker: Attribute purpose to start worker attempts for UMA (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: patch for landing? Created 4 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "content/browser/service_worker/service_worker_fetch_dispatcher.h" 5 #include "content/browser/service_worker/service_worker_fetch_dispatcher.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/trace_event/trace_event.h" 10 #include "base/trace_event/trace_event.h"
11 #include "content/browser/service_worker/service_worker_version.h" 11 #include "content/browser/service_worker/service_worker_version.h"
12 #include "content/common/service_worker/service_worker_messages.h" 12 #include "content/common/service_worker/service_worker_messages.h"
13 13
14 namespace content { 14 namespace content {
15 15
16 namespace {
17 using EventType = ServiceWorkerMetrics::EventType;
18 EventType ResourceTypeToEventType(ResourceType resource_type) {
19 switch (resource_type) {
20 case RESOURCE_TYPE_MAIN_FRAME:
21 return EventType::FETCH_MAIN_FRAME;
22 case RESOURCE_TYPE_SUB_FRAME:
23 return EventType::FETCH_SUB_FRAME;
24 case RESOURCE_TYPE_SHARED_WORKER:
25 return EventType::FETCH_SHARED_WORKER;
26 case RESOURCE_TYPE_SERVICE_WORKER:
27 case RESOURCE_TYPE_LAST_TYPE:
28 NOTREACHED() << resource_type;
29 return EventType::FETCH_SUB_RESOURCE;
30 default:
31 return EventType::FETCH_SUB_RESOURCE;
32 }
33 }
34 } // namespace
35
16 ServiceWorkerFetchDispatcher::ServiceWorkerFetchDispatcher( 36 ServiceWorkerFetchDispatcher::ServiceWorkerFetchDispatcher(
17 scoped_ptr<ServiceWorkerFetchRequest> request, 37 scoped_ptr<ServiceWorkerFetchRequest> request,
18 ServiceWorkerVersion* version, 38 ServiceWorkerVersion* version,
39 ResourceType resource_type,
19 const base::Closure& prepare_callback, 40 const base::Closure& prepare_callback,
20 const FetchCallback& fetch_callback) 41 const FetchCallback& fetch_callback)
21 : version_(version), 42 : version_(version),
22 prepare_callback_(prepare_callback), 43 prepare_callback_(prepare_callback),
23 fetch_callback_(fetch_callback), 44 fetch_callback_(fetch_callback),
24 request_(std::move(request)), 45 request_(std::move(request)),
46 resource_type_(resource_type),
25 weak_factory_(this) {} 47 weak_factory_(this) {}
26 48
27 ServiceWorkerFetchDispatcher::~ServiceWorkerFetchDispatcher() {} 49 ServiceWorkerFetchDispatcher::~ServiceWorkerFetchDispatcher() {}
28 50
29 void ServiceWorkerFetchDispatcher::Run() { 51 void ServiceWorkerFetchDispatcher::Run() {
30 DCHECK(version_->status() == ServiceWorkerVersion::ACTIVATING || 52 DCHECK(version_->status() == ServiceWorkerVersion::ACTIVATING ||
31 version_->status() == ServiceWorkerVersion::ACTIVATED) 53 version_->status() == ServiceWorkerVersion::ACTIVATED)
32 << version_->status(); 54 << version_->status();
33 55
34 if (version_->status() == ServiceWorkerVersion::ACTIVATING) { 56 if (version_->status() == ServiceWorkerVersion::ACTIVATING) {
35 version_->RegisterStatusChangeCallback( 57 version_->RegisterStatusChangeCallback(
36 base::Bind(&ServiceWorkerFetchDispatcher::DidWaitActivation, 58 base::Bind(&ServiceWorkerFetchDispatcher::DidWaitActivation,
37 weak_factory_.GetWeakPtr())); 59 weak_factory_.GetWeakPtr()));
38 return; 60 return;
39 } 61 }
40 DidWaitActivation(); 62 DidWaitActivation();
41 } 63 }
42 64
43 void ServiceWorkerFetchDispatcher::DidWaitActivation() { 65 void ServiceWorkerFetchDispatcher::DidWaitActivation() {
44 if (version_->status() != ServiceWorkerVersion::ACTIVATED) { 66 if (version_->status() != ServiceWorkerVersion::ACTIVATED) {
45 DCHECK_EQ(ServiceWorkerVersion::INSTALLED, version_->status()); 67 DCHECK_EQ(ServiceWorkerVersion::INSTALLED, version_->status());
46 DidFailActivation(); 68 DidFailActivation();
47 return; 69 return;
48 } 70 }
49 version_->RunAfterStartWorker( 71 version_->RunAfterStartWorker(
72 ResourceTypeToEventType(resource_type_),
50 base::Bind(&ServiceWorkerFetchDispatcher::DispatchFetchEvent, 73 base::Bind(&ServiceWorkerFetchDispatcher::DispatchFetchEvent,
51 weak_factory_.GetWeakPtr()), 74 weak_factory_.GetWeakPtr()),
52 base::Bind(&ServiceWorkerFetchDispatcher::DidFail, 75 base::Bind(&ServiceWorkerFetchDispatcher::DidFail,
53 weak_factory_.GetWeakPtr())); 76 weak_factory_.GetWeakPtr()));
54 } 77 }
55 78
56 void ServiceWorkerFetchDispatcher::DidFailActivation() { 79 void ServiceWorkerFetchDispatcher::DidFailActivation() {
57 // The previous activation seems to have failed, abort the step 80 // The previous activation seems to have failed, abort the step
58 // with activate error. (The error should be separately reported 81 // with activate error. (The error should be separately reported
59 // to the associated documents and association must be dropped 82 // to the associated documents and association must be dropped
60 // at this point) 83 // at this point)
61 DidFinish(SERVICE_WORKER_ERROR_ACTIVATE_WORKER_FAILED, 84 DidFinish(SERVICE_WORKER_ERROR_ACTIVATE_WORKER_FAILED,
62 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, 85 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK,
63 ServiceWorkerResponse()); 86 ServiceWorkerResponse());
64 } 87 }
65 88
66 void ServiceWorkerFetchDispatcher::DispatchFetchEvent() { 89 void ServiceWorkerFetchDispatcher::DispatchFetchEvent() {
67 DCHECK_EQ(ServiceWorkerVersion::RUNNING, version_->running_status()) 90 DCHECK_EQ(ServiceWorkerVersion::RUNNING, version_->running_status())
68 << "Worker stopped too soon after it was started."; 91 << "Worker stopped too soon after it was started.";
69 92
70 DCHECK(!prepare_callback_.is_null()); 93 DCHECK(!prepare_callback_.is_null());
71 base::Closure prepare_callback = prepare_callback_; 94 base::Closure prepare_callback = prepare_callback_;
72 prepare_callback.Run(); 95 prepare_callback.Run();
73 96
74 int request_id = 97 int request_id =
75 version_->StartRequest(ServiceWorkerMetrics::EventType::FETCH, 98 version_->StartRequest(ServiceWorkerMetrics::EventType::FETCH,
Marijn Kruisselbrink 2016/03/16 23:33:41 Bit late of a comment, but it seems weird that you
76 base::Bind(&ServiceWorkerFetchDispatcher::DidFail, 99 base::Bind(&ServiceWorkerFetchDispatcher::DidFail,
77 weak_factory_.GetWeakPtr())); 100 weak_factory_.GetWeakPtr()));
78 version_->DispatchEvent<ServiceWorkerHostMsg_FetchEventFinished>( 101 version_->DispatchEvent<ServiceWorkerHostMsg_FetchEventFinished>(
79 request_id, ServiceWorkerMsg_FetchEvent(request_id, *request_.get()), 102 request_id, ServiceWorkerMsg_FetchEvent(request_id, *request_.get()),
80 base::Bind(&ServiceWorkerFetchDispatcher::DidFinish, 103 base::Bind(&ServiceWorkerFetchDispatcher::DidFinish,
81 weak_factory_.GetWeakPtr())); 104 weak_factory_.GetWeakPtr()));
82 } 105 }
83 106
84 void ServiceWorkerFetchDispatcher::DidPrepare() { 107 void ServiceWorkerFetchDispatcher::DidPrepare() {
85 DCHECK(!prepare_callback_.is_null()); 108 DCHECK(!prepare_callback_.is_null());
(...skipping 18 matching lines...) Expand all
104 if (!version_->FinishRequest(request_id, handled)) 127 if (!version_->FinishRequest(request_id, handled))
105 NOTREACHED() << "Should only receive one reply per event"; 128 NOTREACHED() << "Should only receive one reply per event";
106 129
107 DCHECK(!fetch_callback_.is_null()); 130 DCHECK(!fetch_callback_.is_null());
108 FetchCallback fetch_callback = fetch_callback_; 131 FetchCallback fetch_callback = fetch_callback_;
109 scoped_refptr<ServiceWorkerVersion> version = version_; 132 scoped_refptr<ServiceWorkerVersion> version = version_;
110 fetch_callback.Run(SERVICE_WORKER_OK, fetch_result, response, version); 133 fetch_callback.Run(SERVICE_WORKER_OK, fetch_result, response, version);
111 } 134 }
112 135
113 } // namespace content 136 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698