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
3 // found in the LICENSE file.
5 #ifndef CONTENT_CHILD_NAVIGATOR_CONNECT_SERVICE_PORT_PROVIDER_H_
6 #define CONTENT_CHILD_NAVIGATOR_CONNECT_SERVICE_PORT_PROVIDER_H_
8 #include "base/compiler_specific.h"
9 #include "base/id_map.h"
10 #include "base/memory/ref_counted.h"
11 #include "content/child/worker_task_runner.h"
12 #include "content/common/service_port_service.mojom.h"
13 #include "third_party/WebKit/public/platform/modules/navigator_services/WebServicePortProvider.h"
18 class SingleThreadTaskRunner
;
22 class WebServicePortProviderClient
;
31 class ServiceRegistry
;
32 struct TransferredMessagePort
;
34 // Main entry point for the navigator.services API in a child process. This
35 // implementes the blink API and passes calls on to the browser process, as
36 // well as receives events from the browser process to pass back on to blink.
37 // One instance of this class is created for each ServicePortCollection, the
38 // instance lives on the worker thread it is associated with, and is
39 // conceptually owned by the ServicePortCollection in blink. Refcounted because
40 // some operations might require asynchronous operations that require this class
41 // to potentially stick around for a bit longer after a ServicePortCollection is
43 // Uses mojo to communicate with the browser process.
44 class ServicePortProvider
45 : public blink::WebServicePortProvider
,
46 public ServicePortServiceClient
,
47 public base::RefCountedThreadSafe
<ServicePortProvider
> {
50 blink::WebServicePortProviderClient
* client
,
51 const scoped_refptr
<base::SingleThreadTaskRunner
>& main_loop
);
53 // WebServicePortProvider implementation.
54 virtual void destroy();
55 virtual void connect(const blink::WebURL
& target_url
,
56 const blink::WebString
& origin
,
57 blink::WebServicePortConnectCallbacks
* callbacks
);
58 virtual void postMessage(blink::WebServicePortID port_id
,
59 const blink::WebString
& message
,
60 blink::WebMessagePortChannelArray
* channels
);
61 virtual void closePort(blink::WebServicePortID port_id
);
63 // ServicePortServiceClient implementation.
64 void PostMessage(int32_t port_id
,
65 const mojo::String
& message
,
66 mojo::Array
<MojoTransferredMessagePortPtr
> ports
,
67 mojo::Array
<int32_t> new_routing_ids
) override
;
70 ~ServicePortProvider() override
;
71 friend class base::RefCountedThreadSafe
<ServicePortProvider
>;
73 void PostMessageToBrowser(int port_id
,
74 const base::string16
& message
,
75 const std::vector
<TransferredMessagePort
> ports
);
78 scoped_ptr
<blink::WebServicePortConnectCallbacks
> callbacks
,
79 ServicePortConnectResult result
,
82 blink::WebServicePortProviderClient
* client_
;
84 // Helper method that returns an initialized ServicePortServicePtr.
85 ServicePortServicePtr
& GetServicePortServicePtr();
86 mojo::Binding
<ServicePortServiceClient
> binding_
;
88 ServicePortServicePtr service_port_service_
;
90 scoped_refptr
<base::SingleThreadTaskRunner
> main_loop_
;
92 DISALLOW_COPY_AND_ASSIGN(ServicePortProvider
);
95 } // namespace content
97 #endif // CONTENT_CHILD_NAVIGATOR_CONNECT_SERVICE_PORT_PROVIDER_H_