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"
14 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h"
19 class SingleThreadTaskRunner
;
23 class WebServicePortProviderClient
;
32 class ServiceRegistry
;
33 struct TransferredMessagePort
;
35 // Main entry point for the navigator.services API in a child process. This
36 // implementes the blink API and passes calls on to the browser process, as
37 // well as receives events from the browser process to pass back on to blink.
38 // One instance of this class is created for each ServicePortCollection, the
39 // instance lives on the worker thread it is associated with, and is
40 // conceptually owned by the ServicePortCollection in blink. Refcounted because
41 // some operations might require asynchronous operations that require this class
42 // to potentially stick around for a bit longer after a ServicePortCollection is
44 // Uses mojo to communicate with the browser process.
45 class ServicePortProvider
46 : public blink::WebServicePortProvider
,
47 public ServicePortServiceClient
,
48 public base::RefCountedThreadSafe
<ServicePortProvider
> {
51 blink::WebServicePortProviderClient
* client
,
52 const scoped_refptr
<base::SingleThreadTaskRunner
>& main_loop
);
54 // WebServicePortProvider implementation.
55 virtual void destroy();
56 virtual void connect(const blink::WebURL
& target_url
,
57 const blink::WebString
& origin
,
58 blink::WebServicePortConnectCallbacks
* callbacks
);
59 virtual void postMessage(blink::WebServicePortID port_id
,
60 const blink::WebString
& message
,
61 blink::WebMessagePortChannelArray
* channels
);
62 virtual void closePort(blink::WebServicePortID port_id
);
64 // ServicePortServiceClient implementation.
65 void PostMessage(int32_t port_id
,
66 const mojo::String
& message
,
67 mojo::Array
<MojoTransferredMessagePortPtr
> ports
,
68 mojo::Array
<int32_t> new_routing_ids
) override
;
71 ~ServicePortProvider() override
;
72 friend class base::RefCountedThreadSafe
<ServicePortProvider
>;
74 void PostMessageToBrowser(int port_id
,
75 const base::string16
& message
,
76 const std::vector
<TransferredMessagePort
> ports
);
79 scoped_ptr
<blink::WebServicePortConnectCallbacks
> callbacks
,
80 ServicePortConnectResult result
,
83 blink::WebServicePortProviderClient
* client_
;
85 // Helper method that returns an initialized ServicePortServicePtr.
86 ServicePortServicePtr
& GetServicePortServicePtr();
87 mojo::Binding
<ServicePortServiceClient
> binding_
;
89 ServicePortServicePtr service_port_service_
;
91 scoped_refptr
<base::SingleThreadTaskRunner
> main_loop_
;
93 DISALLOW_COPY_AND_ASSIGN(ServicePortProvider
);
96 } // namespace content
98 #endif // CONTENT_CHILD_NAVIGATOR_CONNECT_SERVICE_PORT_PROVIDER_H_