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

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

Issue 2168513004: [DO NOT COMMIT] ServiceWorker: First touch of mojo (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove an unnecessary file Created 4 years, 4 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 18 matching lines...) Expand all
29 #include "content/browser/service_worker/embedded_worker_instance.h" 29 #include "content/browser/service_worker/embedded_worker_instance.h"
30 #include "content/browser/service_worker/embedded_worker_status.h" 30 #include "content/browser/service_worker/embedded_worker_status.h"
31 #include "content/browser/service_worker/service_worker_metrics.h" 31 #include "content/browser/service_worker/service_worker_metrics.h"
32 #include "content/browser/service_worker/service_worker_script_cache_map.h" 32 #include "content/browser/service_worker/service_worker_script_cache_map.h"
33 #include "content/common/content_export.h" 33 #include "content/common/content_export.h"
34 #include "content/common/service_worker/service_worker_status_code.h" 34 #include "content/common/service_worker/service_worker_status_code.h"
35 #include "content/common/service_worker/service_worker_types.h" 35 #include "content/common/service_worker/service_worker_types.h"
36 #include "ipc/ipc_message.h" 36 #include "ipc/ipc_message.h"
37 #include "services/shell/public/cpp/interface_provider.h" 37 #include "services/shell/public/cpp/interface_provider.h"
38 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWor kerEventResult.h" 38 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWor kerEventResult.h"
39 #include "third_party/WebKit/public/platform/modules/serviceworker/service_worke r_global_scope.mojom.h"
39 #include "url/gurl.h" 40 #include "url/gurl.h"
40 #include "url/origin.h" 41 #include "url/origin.h"
41 42
42 // Windows headers will redefine SendMessage. 43 // Windows headers will redefine SendMessage.
43 #ifdef SendMessage 44 #ifdef SendMessage
44 #undef SendMessage 45 #undef SendMessage
45 #endif 46 #endif
46 47
47 namespace net { 48 namespace net {
48 class HttpResponseInfo; 49 class HttpResponseInfo;
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 // Connects to a specific mojo service exposed by the (running) service 227 // Connects to a specific mojo service exposed by the (running) service
227 // worker. If a connection to a service for the same Interface already exists 228 // worker. If a connection to a service for the same Interface already exists
228 // this will return that existing connection. The |request_id| must be a value 229 // this will return that existing connection. The |request_id| must be a value
229 // previously returned by StartRequest. If the connection to the service 230 // previously returned by StartRequest. If the connection to the service
230 // fails or closes before the request finished, the error callback associated 231 // fails or closes before the request finished, the error callback associated
231 // with |request_id| is called. 232 // with |request_id| is called.
232 // Only call GetMojoServiceForRequest once for a specific |request_id|. 233 // Only call GetMojoServiceForRequest once for a specific |request_id|.
233 template <typename Interface> 234 template <typename Interface>
234 base::WeakPtr<Interface> GetMojoServiceForRequest(int request_id); 235 base::WeakPtr<Interface> GetMojoServiceForRequest(int request_id);
235 236
237 template <typename Interface>
238 base::WeakPtr<Interface> GetMojoService();
239
240 template <typename Interface>
241 bool AddMojoService(mojo::InterfacePtr<Interface> interface,
242 bool overwrite = false);
243
236 // Dispatches an event. If dispatching the event fails, all of the error 244 // Dispatches an event. If dispatching the event fails, all of the error
237 // callbacks that were associated with |request_ids| via StartRequest are 245 // callbacks that were associated with |request_ids| via StartRequest are
238 // called. 246 // called.
239 // Use RegisterRequestCallback or RegisterSimpleRequest to register a callback 247 // Use RegisterRequestCallback or RegisterSimpleRequest to register a callback
240 // to receive messages sent back in response to this event before calling this 248 // to receive messages sent back in response to this event before calling this
241 // method. 249 // method.
242 // This must be called when the worker is running. 250 // This must be called when the worker is running.
243 void DispatchEvent(const std::vector<int>& request_ids, 251 void DispatchEvent(const std::vector<int>& request_ids,
244 const IPC::Message& message); 252 const IPC::Message& message);
245 253
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 void SetMainScriptHttpResponseInfo(const net::HttpResponseInfo& http_info); 348 void SetMainScriptHttpResponseInfo(const net::HttpResponseInfo& http_info);
341 const net::HttpResponseInfo* GetMainScriptHttpResponseInfo(); 349 const net::HttpResponseInfo* GetMainScriptHttpResponseInfo();
342 350
343 // Simulate ping timeout. Should be used for tests-only. 351 // Simulate ping timeout. Should be used for tests-only.
344 void SimulatePingTimeoutForTesting(); 352 void SimulatePingTimeoutForTesting();
345 353
346 // Returns true if the service worker has work to do: it has pending 354 // Returns true if the service worker has work to do: it has pending
347 // requests, in-progress streaming URLRequestJobs, or pending start callbacks. 355 // requests, in-progress streaming URLRequestJobs, or pending start callbacks.
348 bool HasWork() const; 356 bool HasWork() const;
349 357
358 // Interfaces called from ServiceWorkerGlobalScopeHost
359 void OnSimpleEventResponse(int request_id,
360 ServiceWorkerStatusCode result);
350 private: 361 private:
362 friend class ServiceWorkerGlobalScopeHostImpl;
351 friend class base::RefCounted<ServiceWorkerVersion>; 363 friend class base::RefCounted<ServiceWorkerVersion>;
352 friend class ServiceWorkerMetrics; 364 friend class ServiceWorkerMetrics;
353 friend class ServiceWorkerReadFromCacheJobTest; 365 friend class ServiceWorkerReadFromCacheJobTest;
354 friend class ServiceWorkerStallInStoppingTest; 366 friend class ServiceWorkerStallInStoppingTest;
355 friend class ServiceWorkerURLRequestJobTest; 367 friend class ServiceWorkerURLRequestJobTest;
356 friend class ServiceWorkerVersionBrowserTest; 368 friend class ServiceWorkerVersionBrowserTest;
357 friend class ServiceWorkerVersionTest; 369 friend class ServiceWorkerVersionTest;
358 370
359 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerControlleeRequestHandlerTest, 371 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerControlleeRequestHandlerTest,
360 ActivateWaitingVersion); 372 ActivateWaitingVersion);
(...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after
734 746
735 template <typename Interface> 747 template <typename Interface>
736 base::WeakPtr<Interface> ServiceWorkerVersion::GetMojoServiceForRequest( 748 base::WeakPtr<Interface> ServiceWorkerVersion::GetMojoServiceForRequest(
737 int request_id) { 749 int request_id) {
738 DCHECK_EQ(EmbeddedWorkerStatus::RUNNING, running_status()); 750 DCHECK_EQ(EmbeddedWorkerStatus::RUNNING, running_status());
739 PendingRequest* request = pending_requests_.Lookup(request_id); 751 PendingRequest* request = pending_requests_.Lookup(request_id);
740 DCHECK(request) << "Invalid request id"; 752 DCHECK(request) << "Invalid request id";
741 DCHECK(!request->mojo_service) 753 DCHECK(!request->mojo_service)
742 << "Request is already associated with a mojo service"; 754 << "Request is already associated with a mojo service";
743 755
756 base::WeakPtr<Interface> interface = GetMojoService<Interface>();
757 request->mojo_service = Interface::Name_;
758 return interface;
759 }
760
761 template <typename Interface>
762 base::WeakPtr<Interface> ServiceWorkerVersion::GetMojoService() {
763 DCHECK_EQ(EmbeddedWorkerStatus::RUNNING, running_status());
744 MojoServiceWrapper<Interface>* service = 764 MojoServiceWrapper<Interface>* service =
745 static_cast<MojoServiceWrapper<Interface>*>( 765 static_cast<MojoServiceWrapper<Interface>*>(
746 mojo_services_.get(Interface::Name_)); 766 mojo_services_.get(Interface::Name_));
747 if (!service) { 767 if (!service) {
748 mojo::InterfacePtr<Interface> interface; 768 mojo::InterfacePtr<Interface> interface;
749 embedded_worker_->GetRemoteInterfaces()->GetInterface(&interface); 769 embedded_worker_->GetRemoteInterfaces()->GetInterface(&interface);
750 interface.set_connection_error_handler( 770 interface.set_connection_error_handler(
751 base::Bind(&ServiceWorkerVersion::OnMojoConnectionError, 771 base::Bind(&ServiceWorkerVersion::OnMojoConnectionError,
752 weak_factory_.GetWeakPtr(), Interface::Name_)); 772 weak_factory_.GetWeakPtr(), Interface::Name_));
753 service = new MojoServiceWrapper<Interface>(this, std::move(interface)); 773 service = new MojoServiceWrapper<Interface>(this, std::move(interface));
754 mojo_services_.add(Interface::Name_, base::WrapUnique(service)); 774 mojo_services_.add(Interface::Name_, base::WrapUnique(service));
755 } 775 }
756 request->mojo_service = Interface::Name_;
757 return service->GetWeakPtr(); 776 return service->GetWeakPtr();
758 } 777 }
759 778
779 template <typename Interface>
780 bool ServiceWorkerVersion::AddMojoService(
781 mojo::InterfacePtr<Interface> interface, bool overwrite) {
782 LOG(ERROR) << __PRETTY_FUNCTION__;
783 // Fail if the interface is already registered.
784 if (!overwrite && mojo_services_.get(Interface::Name_)) {
785 return false;
786 }
787
788 interface.set_connection_error_handler(
789 base::Bind(&ServiceWorkerVersion::OnMojoConnectionError,
790 weak_factory_.GetWeakPtr(), Interface::Name_));
791 MojoServiceWrapper<Interface>* service =
792 new MojoServiceWrapper<Interface>(this, std::move(interface));
793 mojo_services_.set(Interface::Name_, base::WrapUnique(service));
794 return true;
795 }
796
797
760 template <typename ResponseMessage> 798 template <typename ResponseMessage>
761 void ServiceWorkerVersion::DispatchSimpleEvent(int request_id, 799 void ServiceWorkerVersion::DispatchSimpleEvent(int request_id,
762 const IPC::Message& message) { 800 const IPC::Message& message) {
763 RegisterSimpleRequest<ResponseMessage>(request_id); 801 RegisterSimpleRequest<ResponseMessage>(request_id);
764 DispatchEvent({request_id}, message); 802 DispatchEvent({request_id}, message);
765 } 803 }
766 804
767 template <typename ResponseMessage, typename ResponseCallbackType> 805 template <typename ResponseMessage, typename ResponseCallbackType>
768 void ServiceWorkerVersion::RegisterRequestCallback( 806 void ServiceWorkerVersion::RegisterRequestCallback(
769 int request_id, 807 int request_id,
770 const ResponseCallbackType& callback) { 808 const ResponseCallbackType& callback) {
771 PendingRequest* request = pending_requests_.Lookup(request_id); 809 PendingRequest* request = pending_requests_.Lookup(request_id);
772 DCHECK(request) << "Invalid request id"; 810 DCHECK(request) << "Invalid request id";
773 DCHECK(!request->listener) << "Callback was already registered"; 811 DCHECK(!request->listener) << "Callback was already registered";
774 DCHECK(!request->is_dispatched) << "Request already dispatched an IPC event"; 812 DCHECK(!request->is_dispatched) << "Request already dispatched an IPC event";
775 request->listener.reset( 813 request->listener.reset(
776 new EventResponseHandler<ResponseMessage, ResponseCallbackType>( 814 new EventResponseHandler<ResponseMessage, ResponseCallbackType>(
777 embedded_worker()->AsWeakPtr(), request_id, callback)); 815 embedded_worker()->AsWeakPtr(), request_id, callback));
778 } 816 }
779 817
780 template <typename ResponseMessage> 818 template <typename ResponseMessage>
781 void ServiceWorkerVersion::RegisterSimpleRequest(int request_id) { 819 void ServiceWorkerVersion::RegisterSimpleRequest(int request_id) {
782 RegisterRequestCallback<ResponseMessage>( 820 RegisterRequestCallback<ResponseMessage>(
783 request_id, 821 request_id,
784 base::Bind(&ServiceWorkerVersion::OnSimpleEventResponse, this)); 822 base::Bind(static_cast<void(ServiceWorkerVersion::*)(int, blink::WebServic eWorkerEventResult)>(&ServiceWorkerVersion::OnSimpleEventResponse), this));
785 } 823 }
786 824
787 template <typename ResponseMessage, typename CallbackType> 825 template <typename ResponseMessage, typename CallbackType>
788 bool ServiceWorkerVersion::EventResponseHandler<ResponseMessage, CallbackType>:: 826 bool ServiceWorkerVersion::EventResponseHandler<ResponseMessage, CallbackType>::
789 OnMessageReceived(const IPC::Message& message) { 827 OnMessageReceived(const IPC::Message& message) {
790 if (message.type() != ResponseMessage::ID) 828 if (message.type() != ResponseMessage::ID)
791 return false; 829 return false;
792 int received_request_id; 830 int received_request_id;
793 bool result = base::PickleIterator(message).ReadInt(&received_request_id); 831 bool result = base::PickleIterator(message).ReadInt(&received_request_id);
794 if (!result || received_request_id != request_id_) 832 if (!result || received_request_id != request_id_)
795 return false; 833 return false;
796 834
797 CallbackType protect(callback_); 835 CallbackType protect(callback_);
798 // Essentially same code as what IPC_MESSAGE_FORWARD expands to. 836 // Essentially same code as what IPC_MESSAGE_FORWARD expands to.
799 void* param = nullptr; 837 void* param = nullptr;
800 if (!ResponseMessage::Dispatch(&message, &callback_, this, param, 838 if (!ResponseMessage::Dispatch(&message, &callback_, this, param,
801 &CallbackType::Run)) 839 &CallbackType::Run))
802 message.set_dispatch_error(); 840 message.set_dispatch_error();
803 841
804 // At this point |this| can have been deleted, so don't do anything other 842 // At this point |this| can have been deleted, so don't do anything other
805 // than returning. 843 // than returning.
806 844
807 return true; 845 return true;
808 } 846 }
809 847
810 } // namespace content 848 } // namespace content
811 849
812 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ 850 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698