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

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: add EventType argument to StartRequest 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 BaseMojoService {
Ken Rockot(use gerrit already) 2016/01/07 20:36:48 nit: I find the name a little confusing since it's
Marijn Kruisselbrink 2016/01/07 21:33:06 renamed it to (Base)MojoServiceWrapper
445 public:
446 BaseMojoService(ServiceWorkerVersion* worker, const char* service_name);
447 virtual ~BaseMojoService();
448
449 private:
450 ServiceWorkerVersion* worker_;
451 const char* service_name_;
452 };
Ken Rockot(use gerrit already) 2016/01/07 20:36:48 nit: DISALLOW_COPY_AND_ASSIGN
Marijn Kruisselbrink 2016/01/07 21:33:06 Done
453
454 // Wrapper around a mojo::InterfacePtr, which passes out WeakPtr's to the
455 // interface.
456 template <typename Interface>
457 class MojoService : public BaseMojoService {
458 public:
459 MojoService(ServiceWorkerVersion* worker,
460 mojo::InterfacePtr<Interface> interface)
461 : BaseMojoService(worker, Interface::Name_),
462 interface_(std::move(interface)),
463 weak_ptr_factory_(interface_.get()) {}
464
465 base::WeakPtr<Interface> GetWeakPtr() {
466 return weak_ptr_factory_.GetWeakPtr();
467 }
468
469 private:
470 mojo::InterfacePtr<Interface> interface_;
471 base::WeakPtrFactory<Interface> weak_ptr_factory_;
414 }; 472 };
415 473
416 typedef ServiceWorkerVersion self; 474 typedef ServiceWorkerVersion self;
417 using ServiceWorkerClients = std::vector<ServiceWorkerClientInfo>; 475 using ServiceWorkerClients = std::vector<ServiceWorkerClientInfo>;
418 using RequestInfoPriorityQueue = 476 using RequestInfoPriorityQueue =
419 std::priority_queue<RequestInfo, 477 std::priority_queue<RequestInfo,
420 std::vector<RequestInfo>, 478 std::vector<RequestInfo>,
421 std::greater<RequestInfo>>; 479 std::greater<RequestInfo>>;
422 480
423 // The timeout timer interval. 481 // The timeout timer interval.
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
474 void OnInstallEventFinished(int request_id, 532 void OnInstallEventFinished(int request_id,
475 blink::WebServiceWorkerEventResult result); 533 blink::WebServiceWorkerEventResult result);
476 void OnFetchEventFinished(int request_id, 534 void OnFetchEventFinished(int request_id,
477 ServiceWorkerFetchEventResult result, 535 ServiceWorkerFetchEventResult result,
478 const ServiceWorkerResponse& response); 536 const ServiceWorkerResponse& response);
479 void OnSyncEventFinished(int request_id, ServiceWorkerEventStatus status); 537 void OnSyncEventFinished(int request_id, ServiceWorkerEventStatus status);
480 void OnNotificationClickEventFinished(int request_id); 538 void OnNotificationClickEventFinished(int request_id);
481 void OnPushEventFinished(int request_id, 539 void OnPushEventFinished(int request_id,
482 blink::WebServiceWorkerEventResult result); 540 blink::WebServiceWorkerEventResult result);
483 void OnGeofencingEventFinished(int request_id); 541 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); 542 void OnOpenWindow(int request_id, GURL url);
489 void OnOpenWindowFinished(int request_id, 543 void OnOpenWindowFinished(int request_id,
490 ServiceWorkerStatusCode status, 544 ServiceWorkerStatusCode status,
491 const std::string& client_uuid, 545 const std::string& client_uuid,
492 const ServiceWorkerClientInfo& client_info); 546 const ServiceWorkerClientInfo& client_info);
493 547
494 void OnSetCachedMetadata(const GURL& url, const std::vector<char>& data); 548 void OnSetCachedMetadata(const GURL& url, const std::vector<char>& data);
495 void OnSetCachedMetadataFinished(int64_t callback_id, int result); 549 void OnSetCachedMetadataFinished(int64_t callback_id, int result);
496 void OnClearCachedMetadata(const GURL& url); 550 void OnClearCachedMetadata(const GURL& url);
497 void OnClearCachedMetadataFinished(int64_t callback_id, int result); 551 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. 602 // and records metrics about startup.
549 void RecordStartWorkerResult(ServiceWorkerStatusCode status); 603 void RecordStartWorkerResult(ServiceWorkerStatusCode status);
550 604
551 template <typename IDMAP> 605 template <typename IDMAP>
552 void RemoveCallbackAndStopIfRedundant(IDMAP* callbacks, int request_id); 606 void RemoveCallbackAndStopIfRedundant(IDMAP* callbacks, int request_id);
553 607
554 template <typename CallbackType> 608 template <typename CallbackType>
555 int AddRequest( 609 int AddRequest(
556 const CallbackType& callback, 610 const CallbackType& callback,
557 IDMap<PendingRequest<CallbackType>, IDMapOwnPointer>* callback_map, 611 IDMap<PendingRequest<CallbackType>, IDMapOwnPointer>* callback_map,
558 RequestType request_type); 612 RequestType request_type,
613 ServiceWorkerMetrics::EventType event_type);
559 614
560 template <typename CallbackType> 615 template <typename CallbackType>
561 int AddRequestWithExpiration( 616 int AddRequestWithExpiration(
562 const CallbackType& callback, 617 const CallbackType& callback,
563 IDMap<PendingRequest<CallbackType>, IDMapOwnPointer>* callback_map, 618 IDMap<PendingRequest<CallbackType>, IDMapOwnPointer>* callback_map,
564 RequestType request_type, 619 RequestType request_type,
620 ServiceWorkerMetrics::EventType event_type,
565 base::TimeTicks expiration); 621 base::TimeTicks expiration);
566 622
567 bool MaybeTimeOutRequest(const RequestInfo& info); 623 bool MaybeTimeOutRequest(const RequestInfo& info);
568 bool ShouldStopIfRequestTimesOut(const RequestInfo& info); 624 bool ShouldStopIfRequestTimesOut(const RequestInfo& info);
569 void SetAllRequestExpirations(const base::TimeTicks& expiration); 625 void SetAllRequestExpirations(const base::TimeTicks& expiration);
570 626
571 // Returns the reason the embedded worker failed to start, using information 627 // Returns the reason the embedded worker failed to start, using information
572 // inaccessible to EmbeddedWorkerInstance. Returns |default_code| if it can't 628 // inaccessible to EmbeddedWorkerInstance. Returns |default_code| if it can't
573 // deduce a reason. 629 // deduce a reason.
574 ServiceWorkerStatusCode DeduceStartWorkerFailureReason( 630 ServiceWorkerStatusCode DeduceStartWorkerFailureReason(
575 ServiceWorkerStatusCode default_code); 631 ServiceWorkerStatusCode default_code);
576 632
577 // Sets |stale_time_| if this worker is stale, causing an update to eventually 633 // Sets |stale_time_| if this worker is stale, causing an update to eventually
578 // occur once the worker stops or is running too long. 634 // occur once the worker stops or is running too long.
579 void MarkIfStale(); 635 void MarkIfStale();
580 636
581 void FoundRegistrationForUpdate( 637 void FoundRegistrationForUpdate(
582 ServiceWorkerStatusCode status, 638 ServiceWorkerStatusCode status,
583 const scoped_refptr<ServiceWorkerRegistration>& registration); 639 const scoped_refptr<ServiceWorkerRegistration>& registration);
584 640
585 void OnStoppedInternal(EmbeddedWorkerInstance::Status old_status); 641 void OnStoppedInternal(EmbeddedWorkerInstance::Status old_status);
586 642
587 // Called when a connection to a mojo event Dispatcher drops or fails. 643 // Called when a connection to a mojo event Dispatcher drops or fails.
588 // Calls callbacks for any outstanding requests to the dispatcher as well 644 // Calls callbacks for any outstanding requests to the dispatcher as well
589 // as cleans up the dispatcher. 645 // as cleans up the dispatcher.
590 void OnServicePortDispatcherConnectionError();
591 void OnBackgroundSyncDispatcherConnectionError(); 646 void OnBackgroundSyncDispatcherConnectionError();
592 647
648 // Called when the remote side of a connection to a mojo service is lost.
649 void OnMojoConnectionError(const char* service_name);
650
593 // Called at the beginning of each Dispatch*Event function: records 651 // Called at the beginning of each Dispatch*Event function: records
594 // the time elapsed since idle (generally the time since the previous 652 // the time elapsed since idle (generally the time since the previous
595 // event ended). 653 // event ended).
596 void OnBeginEvent(); 654 void OnBeginEvent();
597 655
598 const int64_t version_id_; 656 const int64_t version_id_;
599 const int64_t registration_id_; 657 const int64_t registration_id_;
600 const GURL script_url_; 658 const GURL script_url_;
601 const GURL scope_; 659 const GURL scope_;
602 std::vector<GURL> foreign_fetch_scopes_; 660 std::vector<GURL> foreign_fetch_scopes_;
603 661
604 Status status_ = NEW; 662 Status status_ = NEW;
605 scoped_ptr<EmbeddedWorkerInstance> embedded_worker_; 663 scoped_ptr<EmbeddedWorkerInstance> embedded_worker_;
606 std::vector<StatusCallback> start_callbacks_; 664 std::vector<StatusCallback> start_callbacks_;
607 std::vector<StatusCallback> stop_callbacks_; 665 std::vector<StatusCallback> stop_callbacks_;
608 std::vector<base::Closure> status_change_callbacks_; 666 std::vector<base::Closure> status_change_callbacks_;
609 667
610 // Message callbacks. (Update HasInflightRequests() too when you update this 668 // Message callbacks. (Update HasInflightRequests() too when you update this
611 // list.) 669 // list.)
612 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer> activate_requests_; 670 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer> activate_requests_;
613 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer> install_requests_; 671 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer> install_requests_;
614 IDMap<PendingRequest<FetchCallback>, IDMapOwnPointer> fetch_requests_; 672 IDMap<PendingRequest<FetchCallback>, IDMapOwnPointer> fetch_requests_;
615 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer> sync_requests_; 673 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer> sync_requests_;
616 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer> 674 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer>
617 notification_click_requests_; 675 notification_click_requests_;
618 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer> push_requests_; 676 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer> push_requests_;
619 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer> geofencing_requests_; 677 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer> geofencing_requests_;
620 IDMap<PendingRequest<ServicePortConnectCallback>, IDMapOwnPointer> 678 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer> custom_requests_;
621 service_port_connect_requests_;
622 679
623 ServicePortDispatcherPtr service_port_dispatcher_;
624 BackgroundSyncServiceClientPtr background_sync_dispatcher_; 680 BackgroundSyncServiceClientPtr background_sync_dispatcher_;
625 681
682 // Stores all open connections to mojo services. Maps the service name to
683 // the actual interface pointer. When a connection is closed it is removed
684 // from this map.
685 // mojo_services_[Interface::Name_] is assumed to always contain a
686 // MojoService<Interface> instance.
687 base::ScopedPtrHashMap<const char*, scoped_ptr<BaseMojoService>>
688 mojo_services_;
689
626 std::set<const ServiceWorkerURLRequestJob*> streaming_url_request_jobs_; 690 std::set<const ServiceWorkerURLRequestJob*> streaming_url_request_jobs_;
627 691
628 std::map<std::string, ServiceWorkerProviderHost*> controllee_map_; 692 std::map<std::string, ServiceWorkerProviderHost*> controllee_map_;
629 // Will be null while shutting down. 693 // Will be null while shutting down.
630 base::WeakPtr<ServiceWorkerContextCore> context_; 694 base::WeakPtr<ServiceWorkerContextCore> context_;
631 base::ObserverList<Listener> listeners_; 695 base::ObserverList<Listener> listeners_;
632 ServiceWorkerScriptCacheMap script_cache_map_; 696 ServiceWorkerScriptCacheMap script_cache_map_;
633 base::OneShotTimer update_timer_; 697 base::OneShotTimer update_timer_;
634 698
635 // Starts running in StartWorker and continues until the worker is stopped. 699 // Starts running in StartWorker and continues until the worker is stopped.
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
670 734
671 scoped_ptr<PingController> ping_controller_; 735 scoped_ptr<PingController> ping_controller_;
672 scoped_ptr<Metrics> metrics_; 736 scoped_ptr<Metrics> metrics_;
673 const bool should_exclude_from_uma_ = false; 737 const bool should_exclude_from_uma_ = false;
674 738
675 base::WeakPtrFactory<ServiceWorkerVersion> weak_factory_; 739 base::WeakPtrFactory<ServiceWorkerVersion> weak_factory_;
676 740
677 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerVersion); 741 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerVersion);
678 }; 742 };
679 743
744 template <typename Interface>
745 base::WeakPtr<Interface> ServiceWorkerVersion::GetMojoServiceForRequest(
746 int request_id) {
747 DCHECK_EQ(RUNNING, running_status());
748 PendingRequest<StatusCallback>* request = custom_requests_.Lookup(request_id);
749 DCHECK(request) << "Invalid request id";
750 DCHECK(!request->mojo_service)
751 << "Request is already associated with a mojo service";
752
753 MojoService<Interface>* service = static_cast<MojoService<Interface>*>(
754 mojo_services_.get(Interface::Name_));
755 if (!service) {
756 mojo::InterfacePtr<Interface> interface;
757 embedded_worker_->GetServiceRegistry()->ConnectToRemoteService(
758 mojo::GetProxy(&interface));
759 interface.set_connection_error_handler(
760 base::Bind(&ServiceWorkerVersion::OnMojoConnectionError,
761 weak_factory_.GetWeakPtr(), Interface::Name_));
762 service = new MojoService<Interface>(this, std::move(interface));
763 mojo_services_.add(Interface::Name_, make_scoped_ptr(service));
764 }
765 request->mojo_service = Interface::Name_;
766 return service->GetWeakPtr();
767 }
768
680 } // namespace content 769 } // namespace content
681 770
682 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ 771 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698