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

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

Issue 1264333002: First step to refactor ServiceWorkerVersion to make event dispatching more modular. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rockot nits Created 4 years, 11 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 <map> 10 #include <map>
11 #include <queue> 11 #include <queue>
12 #include <set> 12 #include <set>
13 #include <string> 13 #include <string>
14 #include <vector> 14 #include <vector>
15 15
16 #include "base/callback.h" 16 #include "base/callback.h"
17 #include "base/containers/scoped_ptr_hash_map.h"
17 #include "base/gtest_prod_util.h" 18 #include "base/gtest_prod_util.h"
18 #include "base/id_map.h" 19 #include "base/id_map.h"
19 #include "base/macros.h" 20 #include "base/macros.h"
20 #include "base/memory/ref_counted.h" 21 #include "base/memory/ref_counted.h"
21 #include "base/memory/scoped_ptr.h" 22 #include "base/memory/scoped_ptr.h"
22 #include "base/observer_list.h" 23 #include "base/observer_list.h"
23 #include "base/timer/timer.h" 24 #include "base/timer/timer.h"
24 #include "content/browser/background_sync/background_sync_registration_handle.h" 25 #include "content/browser/background_sync/background_sync_registration_handle.h"
25 #include "content/browser/service_worker/embedded_worker_instance.h" 26 #include "content/browser/service_worker/embedded_worker_instance.h"
27 #include "content/browser/service_worker/service_worker_metrics.h"
26 #include "content/browser/service_worker/service_worker_script_cache_map.h" 28 #include "content/browser/service_worker/service_worker_script_cache_map.h"
27 #include "content/common/background_sync_service.mojom.h" 29 #include "content/common/background_sync_service.mojom.h"
28 #include "content/common/content_export.h" 30 #include "content/common/content_export.h"
29 #include "content/common/service_port_service.mojom.h"
30 #include "content/common/service_worker/service_worker_status_code.h" 31 #include "content/common/service_worker/service_worker_status_code.h"
31 #include "content/common/service_worker/service_worker_types.h" 32 #include "content/common/service_worker/service_worker_types.h"
32 #include "content/public/common/service_registry.h" 33 #include "content/public/common/service_registry.h"
33 #include "third_party/WebKit/public/platform/WebGeofencingEventType.h" 34 #include "third_party/WebKit/public/platform/WebGeofencingEventType.h"
34 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWor kerEventResult.h" 35 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWor kerEventResult.h"
35 36
36 // Windows headers will redefine SendMessage. 37 // Windows headers will redefine SendMessage.
37 #ifdef SendMessage 38 #ifdef SendMessage
38 #undef SendMessage 39 #undef SendMessage
39 #endif 40 #endif
(...skipping 27 matching lines...) Expand all
67 // one of them is activated. This class connects the actual script with a 68 // one of them is activated. This class connects the actual script with a
68 // running worker. 69 // running worker.
69 class CONTENT_EXPORT ServiceWorkerVersion 70 class CONTENT_EXPORT ServiceWorkerVersion
70 : NON_EXPORTED_BASE(public base::RefCounted<ServiceWorkerVersion>), 71 : NON_EXPORTED_BASE(public base::RefCounted<ServiceWorkerVersion>),
71 public EmbeddedWorkerInstance::Listener { 72 public EmbeddedWorkerInstance::Listener {
72 public: 73 public:
73 typedef base::Callback<void(ServiceWorkerStatusCode)> StatusCallback; 74 typedef base::Callback<void(ServiceWorkerStatusCode)> StatusCallback;
74 typedef base::Callback<void(ServiceWorkerStatusCode, 75 typedef base::Callback<void(ServiceWorkerStatusCode,
75 ServiceWorkerFetchEventResult, 76 ServiceWorkerFetchEventResult,
76 const ServiceWorkerResponse&)> FetchCallback; 77 const ServiceWorkerResponse&)> FetchCallback;
77 typedef base::Callback<void(ServiceWorkerStatusCode,
78 bool /* accept_connction */,
79 const base::string16& /* name */,
80 const base::string16& /* data */)>
81 ServicePortConnectCallback;
82 78
83 enum RunningStatus { 79 enum RunningStatus {
84 STOPPED = EmbeddedWorkerInstance::STOPPED, 80 STOPPED = EmbeddedWorkerInstance::STOPPED,
85 STARTING = EmbeddedWorkerInstance::STARTING, 81 STARTING = EmbeddedWorkerInstance::STARTING,
86 RUNNING = EmbeddedWorkerInstance::RUNNING, 82 RUNNING = EmbeddedWorkerInstance::RUNNING,
87 STOPPING = EmbeddedWorkerInstance::STOPPING, 83 STOPPING = EmbeddedWorkerInstance::STOPPING,
88 }; 84 };
89 85
90 // Current version status; some of the status (e.g. INSTALLED and ACTIVATED) 86 // Current version status; some of the status (e.g. INSTALLED and ACTIVATED)
91 // should be persisted unlike running status. 87 // should be persisted unlike running status.
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 // Schedules an update to be run 'soon'. 167 // Schedules an update to be run 'soon'.
172 void ScheduleUpdate(); 168 void ScheduleUpdate();
173 169
174 // If an update is scheduled but not yet started, this resets the timer 170 // If an update is scheduled but not yet started, this resets the timer
175 // delaying the start time by a 'small' amount. 171 // delaying the start time by a 'small' amount.
176 void DeferScheduledUpdate(); 172 void DeferScheduledUpdate();
177 173
178 // Starts an update now. 174 // Starts an update now.
179 void StartUpdate(); 175 void StartUpdate();
180 176
177 // Starts the worker if it isn't already running, and calls |task| when the
178 // worker is running, or |error_callback| if starting the worker failed.
179 void RunAfterStartWorker(const StatusCallback& error_callback,
180 const base::Closure& task);
181
182 // Call this while the worker is running before dispatching an event to the
183 // worker. This informs ServiceWorkerVersion about the event in progress.
184 // Returns a request id, which should later be passed to FinishRequest when
185 // the event finished.
186 // The |error_callback| is called if either ServiceWorkerVersion decides the
187 // event is taking too long, or if for some reason the worker stops or is
188 // killed before the request finishes.
189 int StartRequest(ServiceWorkerMetrics::EventType event_type,
190 const StatusCallback& error_callback);
191
192 // Informs ServiceWorkerVersion that an event has finished being dispatched.
193 // Returns false if no pending requests with the provided id exist, for
194 // example if the request has already timed out.
195 bool FinishRequest(int request_id);
196
197 // Connects to a specific mojo service exposed by the (running) service
198 // worker. If a connection to a service for the same Interface already exists
199 // this will return that existing connection. The |request_id| must be a value
200 // previously returned by StartRequest. If the connection to the service
201 // fails or closes before the request finished, the error callback associated
202 // with |request_id| is called.
203 // Only call GetMojoServiceForRequest once for a specific |request_id|.
204 template <typename Interface>
205 base::WeakPtr<Interface> GetMojoServiceForRequest(int request_id);
206
181 // Sends a message event to the associated embedded worker. 207 // Sends a message event to the associated embedded worker.
182 void DispatchMessageEvent( 208 void DispatchMessageEvent(
183 const base::string16& message, 209 const base::string16& message,
184 const std::vector<TransferredMessagePort>& sent_message_ports, 210 const std::vector<TransferredMessagePort>& sent_message_ports,
185 const StatusCallback& callback); 211 const StatusCallback& callback);
186 212
187 // Sends install event to the associated embedded worker and asynchronously 213 // Sends install event to the associated embedded worker and asynchronously
188 // calls |callback| when it errors out or it gets a response from the worker 214 // calls |callback| when it errors out or it gets a response from the worker
189 // to notify install completion. 215 // to notify install completion.
190 // 216 //
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 // calls |callback| when it errors out or it gets a response from the worker 273 // calls |callback| when it errors out or it gets a response from the worker
248 // to notify completion. 274 // to notify completion.
249 // 275 //
250 // This must be called when the status() is ACTIVATED. 276 // This must be called when the status() is ACTIVATED.
251 void DispatchGeofencingEvent( 277 void DispatchGeofencingEvent(
252 const StatusCallback& callback, 278 const StatusCallback& callback,
253 blink::WebGeofencingEventType event_type, 279 blink::WebGeofencingEventType event_type,
254 const std::string& region_id, 280 const std::string& region_id,
255 const blink::WebCircularGeofencingRegion& region); 281 const blink::WebCircularGeofencingRegion& region);
256 282
257 // Sends a ServicePort connect event to the associated embedded worker and
258 // asynchronously calls |callback| with the response from the worker.
259 //
260 // This must be called when the status() is ACTIVATED.
261 void DispatchServicePortConnectEvent(
262 const ServicePortConnectCallback& callback,
263 const GURL& target_url,
264 const GURL& origin,
265 int port_id);
266
267 // Sends a cross origin message event to the associated embedded worker and 283 // Sends a cross origin message event to the associated embedded worker and
268 // asynchronously calls |callback| when the message was sent (or failed to 284 // asynchronously calls |callback| when the message was sent (or failed to
269 // sent). 285 // sent).
270 // It is the responsibility of the code calling this method to make sure that 286 // It is the responsibility of the code calling this method to make sure that
271 // any transferred message ports are put on hold while potentially a process 287 // any transferred message ports are put on hold while potentially a process
272 // for the service worker is spun up. 288 // for the service worker is spun up.
273 // 289 //
274 // This must be called when the status() is ACTIVATED. 290 // This must be called when the status() is ACTIVATED.
275 void DispatchCrossOriginMessageEvent( 291 void DispatchCrossOriginMessageEvent(
276 const NavigatorConnectClient& client, 292 const NavigatorConnectClient& client,
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
359 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, IdleTimeout); 375 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, IdleTimeout);
360 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, SetDevToolsAttached); 376 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, SetDevToolsAttached);
361 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, StaleUpdate_FreshWorker); 377 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, StaleUpdate_FreshWorker);
362 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, 378 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest,
363 StaleUpdate_NonActiveWorker); 379 StaleUpdate_NonActiveWorker);
364 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, StaleUpdate_StartWorker); 380 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, StaleUpdate_StartWorker);
365 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, StaleUpdate_RunningWorker); 381 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, StaleUpdate_RunningWorker);
366 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, 382 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest,
367 StaleUpdate_DoNotDeferTimer); 383 StaleUpdate_DoNotDeferTimer);
368 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerWaitForeverInFetchTest, RequestTimeout); 384 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerWaitForeverInFetchTest, RequestTimeout);
385 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, RequestTimeout);
369 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerFailToStartTest, Timeout); 386 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerFailToStartTest, Timeout);
370 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionBrowserTest, 387 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionBrowserTest,
371 TimeoutStartingWorker); 388 TimeoutStartingWorker);
372 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionBrowserTest, 389 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionBrowserTest,
373 TimeoutWorkerInEvent); 390 TimeoutWorkerInEvent);
374 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerStallInStoppingTest, DetachThenStart); 391 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerStallInStoppingTest, DetachThenStart);
375 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerStallInStoppingTest, DetachThenRestart); 392 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerStallInStoppingTest, DetachThenRestart);
376 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, 393 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest,
377 RegisterForeignFetchScopes); 394 RegisterForeignFetchScopes);
378 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, RequestCustomizedTimeout); 395 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, RequestCustomizedTimeout);
379 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerWaitForeverInFetchTest, 396 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerWaitForeverInFetchTest,
380 MixedRequestTimeouts); 397 MixedRequestTimeouts);
381 398
382 class Metrics; 399 class Metrics;
383 class PingController; 400 class PingController;
384 401
385 // Used for UMA; add new entries to the end, before NUM_REQUEST_TYPES.
386 enum RequestType { 402 enum RequestType {
387 REQUEST_ACTIVATE, 403 REQUEST_ACTIVATE,
388 REQUEST_INSTALL, 404 REQUEST_INSTALL,
389 REQUEST_FETCH, 405 REQUEST_FETCH,
390 REQUEST_SYNC, 406 REQUEST_SYNC,
391 REQUEST_NOTIFICATION_CLICK, 407 REQUEST_NOTIFICATION_CLICK,
392 REQUEST_PUSH, 408 REQUEST_PUSH,
393 REQUEST_GEOFENCING, 409 REQUEST_GEOFENCING,
394 REQUEST_SERVICE_PORT_CONNECT, 410 REQUEST_CUSTOM,
395 NUM_REQUEST_TYPES 411 NUM_REQUEST_TYPES
396 }; 412 };
397 413
398 struct RequestInfo { 414 struct RequestInfo {
399 RequestInfo(int id, RequestType type, const base::TimeTicks& expiration); 415 RequestInfo(int id,
416 RequestType type,
417 ServiceWorkerMetrics::EventType event_type,
418 const base::TimeTicks& expiration);
400 ~RequestInfo(); 419 ~RequestInfo();
401 bool operator>(const RequestInfo& other) const; 420 bool operator>(const RequestInfo& other) const;
402 int id; 421 int id;
403 RequestType type; 422 RequestType type;
423 ServiceWorkerMetrics::EventType event_type;
404 base::TimeTicks expiration; 424 base::TimeTicks expiration;
405 }; 425 };
406 426
407 template <typename CallbackType> 427 template <typename CallbackType>
408 struct PendingRequest { 428 struct PendingRequest {
409 PendingRequest(const CallbackType& callback, const base::TimeTicks& time); 429 PendingRequest(const CallbackType& callback, const base::TimeTicks& time);
410 ~PendingRequest(); 430 ~PendingRequest();
411 431
412 CallbackType callback; 432 CallbackType callback;
413 base::TimeTicks start_time; 433 base::TimeTicks start_time;
434 // Name of the mojo service this request is associated with. Used to call
435 // the callback when a connection closes with outstanding requests.
436 // Compared as pointer, so should only contain static strings. Typically
437 // this would be Interface::Name_ for some mojo interface.
438 const char* mojo_service = nullptr;
439 };
440
441 // Base class to enable storing a list of mojo interface pointers for
442 // arbitrary interfaces. The destructor is also responsible for calling the
443 // error callbacks for any outstanding requests using this service.
444 class CONTENT_EXPORT BaseMojoServiceWrapper {
445 public:
446 BaseMojoServiceWrapper(ServiceWorkerVersion* worker,
447 const char* service_name);
448 virtual ~BaseMojoServiceWrapper();
449
450 private:
451 ServiceWorkerVersion* worker_;
452 const char* service_name_;
453
454 DISALLOW_COPY_AND_ASSIGN(BaseMojoServiceWrapper);
455 };
456
457 // Wrapper around a mojo::InterfacePtr, which passes out WeakPtr's to the
458 // interface.
459 template <typename Interface>
460 class MojoServiceWrapper : public BaseMojoServiceWrapper {
461 public:
462 MojoServiceWrapper(ServiceWorkerVersion* worker,
463 mojo::InterfacePtr<Interface> interface)
464 : BaseMojoServiceWrapper(worker, Interface::Name_),
465 interface_(std::move(interface)),
466 weak_ptr_factory_(interface_.get()) {}
467
468 base::WeakPtr<Interface> GetWeakPtr() {
469 return weak_ptr_factory_.GetWeakPtr();
470 }
471
472 private:
473 mojo::InterfacePtr<Interface> interface_;
474 base::WeakPtrFactory<Interface> weak_ptr_factory_;
414 }; 475 };
415 476
416 typedef ServiceWorkerVersion self; 477 typedef ServiceWorkerVersion self;
417 using ServiceWorkerClients = std::vector<ServiceWorkerClientInfo>; 478 using ServiceWorkerClients = std::vector<ServiceWorkerClientInfo>;
418 using RequestInfoPriorityQueue = 479 using RequestInfoPriorityQueue =
419 std::priority_queue<RequestInfo, 480 std::priority_queue<RequestInfo,
420 std::vector<RequestInfo>, 481 std::vector<RequestInfo>,
421 std::greater<RequestInfo>>; 482 std::greater<RequestInfo>>;
422 483
423 // The timeout timer interval. 484 // The timeout timer interval.
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
474 void OnInstallEventFinished(int request_id, 535 void OnInstallEventFinished(int request_id,
475 blink::WebServiceWorkerEventResult result); 536 blink::WebServiceWorkerEventResult result);
476 void OnFetchEventFinished(int request_id, 537 void OnFetchEventFinished(int request_id,
477 ServiceWorkerFetchEventResult result, 538 ServiceWorkerFetchEventResult result,
478 const ServiceWorkerResponse& response); 539 const ServiceWorkerResponse& response);
479 void OnSyncEventFinished(int request_id, ServiceWorkerEventStatus status); 540 void OnSyncEventFinished(int request_id, ServiceWorkerEventStatus status);
480 void OnNotificationClickEventFinished(int request_id); 541 void OnNotificationClickEventFinished(int request_id);
481 void OnPushEventFinished(int request_id, 542 void OnPushEventFinished(int request_id,
482 blink::WebServiceWorkerEventResult result); 543 blink::WebServiceWorkerEventResult result);
483 void OnGeofencingEventFinished(int request_id); 544 void OnGeofencingEventFinished(int request_id);
484 void OnServicePortConnectEventFinished(int request_id,
485 ServicePortConnectResult result,
486 const mojo::String& name,
487 const mojo::String& data);
488 void OnOpenWindow(int request_id, GURL url); 545 void OnOpenWindow(int request_id, GURL url);
489 void OnOpenWindowFinished(int request_id, 546 void OnOpenWindowFinished(int request_id,
490 ServiceWorkerStatusCode status, 547 ServiceWorkerStatusCode status,
491 const std::string& client_uuid, 548 const std::string& client_uuid,
492 const ServiceWorkerClientInfo& client_info); 549 const ServiceWorkerClientInfo& client_info);
493 550
494 void OnSetCachedMetadata(const GURL& url, const std::vector<char>& data); 551 void OnSetCachedMetadata(const GURL& url, const std::vector<char>& data);
495 void OnSetCachedMetadataFinished(int64_t callback_id, int result); 552 void OnSetCachedMetadataFinished(int64_t callback_id, int result);
496 void OnClearCachedMetadata(const GURL& url); 553 void OnClearCachedMetadata(const GURL& url);
497 void OnClearCachedMetadataFinished(int64_t callback_id, int result); 554 void OnClearCachedMetadataFinished(int64_t callback_id, int result);
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
548 // and records metrics about startup. 605 // and records metrics about startup.
549 void RecordStartWorkerResult(ServiceWorkerStatusCode status); 606 void RecordStartWorkerResult(ServiceWorkerStatusCode status);
550 607
551 template <typename IDMAP> 608 template <typename IDMAP>
552 void RemoveCallbackAndStopIfRedundant(IDMAP* callbacks, int request_id); 609 void RemoveCallbackAndStopIfRedundant(IDMAP* callbacks, int request_id);
553 610
554 template <typename CallbackType> 611 template <typename CallbackType>
555 int AddRequest( 612 int AddRequest(
556 const CallbackType& callback, 613 const CallbackType& callback,
557 IDMap<PendingRequest<CallbackType>, IDMapOwnPointer>* callback_map, 614 IDMap<PendingRequest<CallbackType>, IDMapOwnPointer>* callback_map,
558 RequestType request_type); 615 RequestType request_type,
616 ServiceWorkerMetrics::EventType event_type);
559 617
560 template <typename CallbackType> 618 template <typename CallbackType>
561 int AddRequestWithExpiration( 619 int AddRequestWithExpiration(
562 const CallbackType& callback, 620 const CallbackType& callback,
563 IDMap<PendingRequest<CallbackType>, IDMapOwnPointer>* callback_map, 621 IDMap<PendingRequest<CallbackType>, IDMapOwnPointer>* callback_map,
564 RequestType request_type, 622 RequestType request_type,
623 ServiceWorkerMetrics::EventType event_type,
565 base::TimeTicks expiration); 624 base::TimeTicks expiration);
566 625
567 bool MaybeTimeOutRequest(const RequestInfo& info); 626 bool MaybeTimeOutRequest(const RequestInfo& info);
568 bool ShouldStopIfRequestTimesOut(const RequestInfo& info); 627 bool ShouldStopIfRequestTimesOut(const RequestInfo& info);
569 void SetAllRequestExpirations(const base::TimeTicks& expiration); 628 void SetAllRequestExpirations(const base::TimeTicks& expiration);
570 629
571 // Returns the reason the embedded worker failed to start, using information 630 // Returns the reason the embedded worker failed to start, using information
572 // inaccessible to EmbeddedWorkerInstance. Returns |default_code| if it can't 631 // inaccessible to EmbeddedWorkerInstance. Returns |default_code| if it can't
573 // deduce a reason. 632 // deduce a reason.
574 ServiceWorkerStatusCode DeduceStartWorkerFailureReason( 633 ServiceWorkerStatusCode DeduceStartWorkerFailureReason(
575 ServiceWorkerStatusCode default_code); 634 ServiceWorkerStatusCode default_code);
576 635
577 // Sets |stale_time_| if this worker is stale, causing an update to eventually 636 // Sets |stale_time_| if this worker is stale, causing an update to eventually
578 // occur once the worker stops or is running too long. 637 // occur once the worker stops or is running too long.
579 void MarkIfStale(); 638 void MarkIfStale();
580 639
581 void FoundRegistrationForUpdate( 640 void FoundRegistrationForUpdate(
582 ServiceWorkerStatusCode status, 641 ServiceWorkerStatusCode status,
583 const scoped_refptr<ServiceWorkerRegistration>& registration); 642 const scoped_refptr<ServiceWorkerRegistration>& registration);
584 643
585 void OnStoppedInternal(EmbeddedWorkerInstance::Status old_status); 644 void OnStoppedInternal(EmbeddedWorkerInstance::Status old_status);
586 645
587 // Called when a connection to a mojo event Dispatcher drops or fails. 646 // Called when a connection to a mojo event Dispatcher drops or fails.
588 // Calls callbacks for any outstanding requests to the dispatcher as well 647 // Calls callbacks for any outstanding requests to the dispatcher as well
589 // as cleans up the dispatcher. 648 // as cleans up the dispatcher.
590 void OnServicePortDispatcherConnectionError();
591 void OnBackgroundSyncDispatcherConnectionError(); 649 void OnBackgroundSyncDispatcherConnectionError();
592 650
651 // Called when the remote side of a connection to a mojo service is lost.
652 void OnMojoConnectionError(const char* service_name);
653
593 // Called at the beginning of each Dispatch*Event function: records 654 // Called at the beginning of each Dispatch*Event function: records
594 // the time elapsed since idle (generally the time since the previous 655 // the time elapsed since idle (generally the time since the previous
595 // event ended). 656 // event ended).
596 void OnBeginEvent(); 657 void OnBeginEvent();
597 658
598 const int64_t version_id_; 659 const int64_t version_id_;
599 const int64_t registration_id_; 660 const int64_t registration_id_;
600 const GURL script_url_; 661 const GURL script_url_;
601 const GURL scope_; 662 const GURL scope_;
602 std::vector<GURL> foreign_fetch_scopes_; 663 std::vector<GURL> foreign_fetch_scopes_;
603 664
604 Status status_ = NEW; 665 Status status_ = NEW;
605 scoped_ptr<EmbeddedWorkerInstance> embedded_worker_; 666 scoped_ptr<EmbeddedWorkerInstance> embedded_worker_;
606 std::vector<StatusCallback> start_callbacks_; 667 std::vector<StatusCallback> start_callbacks_;
607 std::vector<StatusCallback> stop_callbacks_; 668 std::vector<StatusCallback> stop_callbacks_;
608 std::vector<base::Closure> status_change_callbacks_; 669 std::vector<base::Closure> status_change_callbacks_;
609 670
610 // Message callbacks. (Update HasInflightRequests() too when you update this 671 // Message callbacks. (Update HasInflightRequests() too when you update this
611 // list.) 672 // list.)
612 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer> activate_requests_; 673 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer> activate_requests_;
613 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer> install_requests_; 674 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer> install_requests_;
614 IDMap<PendingRequest<FetchCallback>, IDMapOwnPointer> fetch_requests_; 675 IDMap<PendingRequest<FetchCallback>, IDMapOwnPointer> fetch_requests_;
615 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer> sync_requests_; 676 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer> sync_requests_;
616 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer> 677 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer>
617 notification_click_requests_; 678 notification_click_requests_;
618 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer> push_requests_; 679 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer> push_requests_;
619 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer> geofencing_requests_; 680 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer> geofencing_requests_;
620 IDMap<PendingRequest<ServicePortConnectCallback>, IDMapOwnPointer> 681 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer> custom_requests_;
621 service_port_connect_requests_;
622 682
623 ServicePortDispatcherPtr service_port_dispatcher_;
624 BackgroundSyncServiceClientPtr background_sync_dispatcher_; 683 BackgroundSyncServiceClientPtr background_sync_dispatcher_;
625 684
685 // Stores all open connections to mojo services. Maps the service name to
686 // the actual interface pointer. When a connection is closed it is removed
687 // from this map.
688 // mojo_services_[Interface::Name_] is assumed to always contain a
689 // MojoServiceWrapper<Interface> instance.
690 base::ScopedPtrHashMap<const char*, scoped_ptr<BaseMojoServiceWrapper>>
691 mojo_services_;
692
626 std::set<const ServiceWorkerURLRequestJob*> streaming_url_request_jobs_; 693 std::set<const ServiceWorkerURLRequestJob*> streaming_url_request_jobs_;
627 694
628 std::map<std::string, ServiceWorkerProviderHost*> controllee_map_; 695 std::map<std::string, ServiceWorkerProviderHost*> controllee_map_;
629 // Will be null while shutting down. 696 // Will be null while shutting down.
630 base::WeakPtr<ServiceWorkerContextCore> context_; 697 base::WeakPtr<ServiceWorkerContextCore> context_;
631 base::ObserverList<Listener> listeners_; 698 base::ObserverList<Listener> listeners_;
632 ServiceWorkerScriptCacheMap script_cache_map_; 699 ServiceWorkerScriptCacheMap script_cache_map_;
633 base::OneShotTimer update_timer_; 700 base::OneShotTimer update_timer_;
634 701
635 // Starts running in StartWorker and continues until the worker is stopped. 702 // Starts running in StartWorker and continues until the worker is stopped.
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
670 737
671 scoped_ptr<PingController> ping_controller_; 738 scoped_ptr<PingController> ping_controller_;
672 scoped_ptr<Metrics> metrics_; 739 scoped_ptr<Metrics> metrics_;
673 const bool should_exclude_from_uma_ = false; 740 const bool should_exclude_from_uma_ = false;
674 741
675 base::WeakPtrFactory<ServiceWorkerVersion> weak_factory_; 742 base::WeakPtrFactory<ServiceWorkerVersion> weak_factory_;
676 743
677 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerVersion); 744 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerVersion);
678 }; 745 };
679 746
747 template <typename Interface>
748 base::WeakPtr<Interface> ServiceWorkerVersion::GetMojoServiceForRequest(
749 int request_id) {
750 DCHECK_EQ(RUNNING, running_status());
751 PendingRequest<StatusCallback>* request = custom_requests_.Lookup(request_id);
752 DCHECK(request) << "Invalid request id";
753 DCHECK(!request->mojo_service)
754 << "Request is already associated with a mojo service";
755
756 MojoServiceWrapper<Interface>* service =
757 static_cast<MojoServiceWrapper<Interface>*>(
758 mojo_services_.get(Interface::Name_));
759 if (!service) {
760 mojo::InterfacePtr<Interface> interface;
761 embedded_worker_->GetServiceRegistry()->ConnectToRemoteService(
762 mojo::GetProxy(&interface));
763 interface.set_connection_error_handler(
764 base::Bind(&ServiceWorkerVersion::OnMojoConnectionError,
765 weak_factory_.GetWeakPtr(), Interface::Name_));
766 service = new MojoServiceWrapper<Interface>(this, std::move(interface));
767 mojo_services_.add(Interface::Name_, make_scoped_ptr(service));
768 }
769 request->mojo_service = Interface::Name_;
770 return service->GetWeakPtr();
771 }
772
680 } // namespace content 773 } // namespace content
681 774
682 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ 775 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_
OLDNEW
« no previous file with comments | « content/browser/service_worker/service_worker_test_utils.h ('k') | content/browser/service_worker/service_worker_version.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698