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

Side by Side Diff: content/browser/service_worker/service_worker_version.h

Issue 2166523003: Add ref count to service workers for extension API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address comments from Devlin & MichaelN Created 4 years, 2 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 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 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_
7 7
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <functional> 10 #include <functional>
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after
228 int StartRequest(ServiceWorkerMetrics::EventType event_type, 228 int StartRequest(ServiceWorkerMetrics::EventType event_type,
229 const StatusCallback& error_callback); 229 const StatusCallback& error_callback);
230 230
231 // Same as StartRequest, but allows the caller to specify a custom timeout for 231 // Same as StartRequest, but allows the caller to specify a custom timeout for
232 // the event, as well as the behavior for when the request times out. 232 // the event, as well as the behavior for when the request times out.
233 int StartRequestWithCustomTimeout(ServiceWorkerMetrics::EventType event_type, 233 int StartRequestWithCustomTimeout(ServiceWorkerMetrics::EventType event_type,
234 const StatusCallback& error_callback, 234 const StatusCallback& error_callback,
235 const base::TimeDelta& timeout, 235 const base::TimeDelta& timeout,
236 TimeoutBehavior timeout_behavior); 236 TimeoutBehavior timeout_behavior);
237 237
238 // Starts a request of type EventType::EXTERNAL_REQUEST.
239 // Provides a mechanism to external clients to keep the worker running.
240 // |request_uuid| is a GUID for clients to identify the request.
241 // Returns true if the request was successfully scheduled to starrt.
242 bool StartExternalRequest(const std::string& request_uuid);
243
238 // Informs ServiceWorkerVersion that an event has finished being dispatched. 244 // Informs ServiceWorkerVersion that an event has finished being dispatched.
239 // Returns false if no pending requests with the provided id exist, for 245 // Returns false if no pending requests with the provided id exist, for
240 // example if the request has already timed out. 246 // example if the request has already timed out.
241 // Pass the result of the event to |was_handled|, which is used to record 247 // Pass the result of the event to |was_handled|, which is used to record
242 // statistics based on the event status. 248 // statistics based on the event status.
243 // TODO(mek): Use something other than a bool for event status. 249 // TODO(mek): Use something other than a bool for event status.
244 bool FinishRequest(int request_id, 250 bool FinishRequest(int request_id,
245 bool was_handled, 251 bool was_handled,
246 base::Time dispatch_event_time); 252 base::Time dispatch_event_time);
247 253
254 // Finishes an external request that was started by StartExternalRequest().
255 // Returns false if there was an error finishing the request: e.g. the request
256 // was not found or the worker already terminated.
257 bool FinishExternalRequest(const std::string& request_uuid);
258
248 // Connects to a specific mojo service exposed by the (running) service 259 // Connects to a specific mojo service exposed by the (running) service
249 // worker. If a connection to a service for the same Interface already exists 260 // worker. If a connection to a service for the same Interface already exists
250 // this will return that existing connection. The |request_id| must be a value 261 // this will return that existing connection. The |request_id| must be a value
251 // previously returned by StartRequest. If the connection to the service 262 // previously returned by StartRequest. If the connection to the service
252 // fails or closes before the request finished, the error callback associated 263 // fails or closes before the request finished, the error callback associated
253 // with |request_id| is called. 264 // with |request_id| is called.
254 // Only call GetMojoServiceForRequest once for a specific |request_id|. 265 // Only call GetMojoServiceForRequest once for a specific |request_id|.
255 template <typename Interface> 266 template <typename Interface>
256 base::WeakPtr<Interface> GetMojoServiceForRequest(int request_id); 267 base::WeakPtr<Interface> GetMojoServiceForRequest(int request_id);
257 268
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
362 void SetMainScriptHttpResponseInfo(const net::HttpResponseInfo& http_info); 373 void SetMainScriptHttpResponseInfo(const net::HttpResponseInfo& http_info);
363 const net::HttpResponseInfo* GetMainScriptHttpResponseInfo(); 374 const net::HttpResponseInfo* GetMainScriptHttpResponseInfo();
364 375
365 // Simulate ping timeout. Should be used for tests-only. 376 // Simulate ping timeout. Should be used for tests-only.
366 void SimulatePingTimeoutForTesting(); 377 void SimulatePingTimeoutForTesting();
367 378
368 // Returns true if the service worker has work to do: it has pending 379 // Returns true if the service worker has work to do: it has pending
369 // requests, in-progress streaming URLRequestJobs, or pending start callbacks. 380 // requests, in-progress streaming URLRequestJobs, or pending start callbacks.
370 bool HasWork() const; 381 bool HasWork() const;
371 382
383 // Returns the number of pending activity count of this worker.
384 size_t GetPendingExternalRequestCountForTest() const {
385 return external_request_uuid_to_request_id_.size();
386 }
387
372 private: 388 private:
373 friend class base::RefCounted<ServiceWorkerVersion>; 389 friend class base::RefCounted<ServiceWorkerVersion>;
374 friend class ServiceWorkerMetrics; 390 friend class ServiceWorkerMetrics;
375 friend class ServiceWorkerReadFromCacheJobTest; 391 friend class ServiceWorkerReadFromCacheJobTest;
376 friend class ServiceWorkerStallInStoppingTest; 392 friend class ServiceWorkerStallInStoppingTest;
377 friend class ServiceWorkerURLRequestJobTest; 393 friend class ServiceWorkerURLRequestJobTest;
378 friend class ServiceWorkerVersionBrowserTest; 394 friend class ServiceWorkerVersionBrowserTest;
379 friend class ServiceWorkerVersionTest; 395 friend class ServiceWorkerVersionTest;
380 396
381 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerControlleeRequestHandlerTest, 397 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerControlleeRequestHandlerTest,
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after
674 690
675 // Called at the beginning of each Dispatch*Event function: records 691 // Called at the beginning of each Dispatch*Event function: records
676 // the time elapsed since idle (generally the time since the previous 692 // the time elapsed since idle (generally the time since the previous
677 // event ended). 693 // event ended).
678 void OnBeginEvent(); 694 void OnBeginEvent();
679 695
680 // Resets |start_worker_first_purpose_| and fires and clears all start 696 // Resets |start_worker_first_purpose_| and fires and clears all start
681 // callbacks. 697 // callbacks.
682 void FinishStartWorker(ServiceWorkerStatusCode status); 698 void FinishStartWorker(ServiceWorkerStatusCode status);
683 699
700 // Removes any pending external request that has GUID of |request_uuid|.
701 void CleanUpExternalRequest(const std::string& request_uuid,
702 ServiceWorkerStatusCode status);
703
684 const int64_t version_id_; 704 const int64_t version_id_;
685 const int64_t registration_id_; 705 const int64_t registration_id_;
686 const GURL script_url_; 706 const GURL script_url_;
687 const GURL scope_; 707 const GURL scope_;
688 std::vector<GURL> foreign_fetch_scopes_; 708 std::vector<GURL> foreign_fetch_scopes_;
689 std::vector<url::Origin> foreign_fetch_origins_; 709 std::vector<url::Origin> foreign_fetch_origins_;
690 FetchHandlerExistence fetch_handler_existence_; 710 FetchHandlerExistence fetch_handler_existence_;
691 ServiceWorkerMetrics::Site site_for_uma_; 711 ServiceWorkerMetrics::Site site_for_uma_;
692 712
693 Status status_ = NEW; 713 Status status_ = NEW;
694 std::unique_ptr<EmbeddedWorkerInstance> embedded_worker_; 714 std::unique_ptr<EmbeddedWorkerInstance> embedded_worker_;
695 std::vector<StatusCallback> start_callbacks_; 715 std::vector<StatusCallback> start_callbacks_;
696 std::vector<StatusCallback> stop_callbacks_; 716 std::vector<StatusCallback> stop_callbacks_;
697 std::vector<base::Closure> status_change_callbacks_; 717 std::vector<base::Closure> status_change_callbacks_;
698 718
699 // Holds in-flight requests, including requests due to outstanding push, 719 // Holds in-flight requests, including requests due to outstanding push,
700 // fetch, sync, etc. events. 720 // fetch, sync, etc. events.
701 IDMap<PendingRequest, IDMapOwnPointer> pending_requests_; 721 IDMap<PendingRequest, IDMapOwnPointer> pending_requests_;
702 722
723 // Container for pending external requests for this service worker.
724 // (key, value): (request uuid, request id).
725 using RequestUUIDToRequestIDMap = std::map<std::string, int>;
726 RequestUUIDToRequestIDMap external_request_uuid_to_request_id_;
727
703 // Stores all open connections to mojo services. Maps the service name to 728 // Stores all open connections to mojo services. Maps the service name to
704 // the actual interface pointer. When a connection is closed it is removed 729 // the actual interface pointer. When a connection is closed it is removed
705 // from this map. 730 // from this map.
706 // mojo_services_[Interface::Name_] is assumed to always contain a 731 // mojo_services_[Interface::Name_] is assumed to always contain a
707 // MojoServiceWrapper<Interface> instance. 732 // MojoServiceWrapper<Interface> instance.
708 base::ScopedPtrHashMap<const char*, std::unique_ptr<BaseMojoServiceWrapper>> 733 base::ScopedPtrHashMap<const char*, std::unique_ptr<BaseMojoServiceWrapper>>
709 mojo_services_; 734 mojo_services_;
710 735
711 std::set<const ServiceWorkerURLRequestJob*> streaming_url_request_jobs_; 736 std::set<const ServiceWorkerURLRequestJob*> streaming_url_request_jobs_;
712 737
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
838 863
839 // At this point |this| can have been deleted, so don't do anything other 864 // At this point |this| can have been deleted, so don't do anything other
840 // than returning. 865 // than returning.
841 866
842 return true; 867 return true;
843 } 868 }
844 869
845 } // namespace content 870 } // namespace content
846 871
847 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ 872 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698