1 // Copyright 2015 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 #include "components/web_view/frame_connection.h"
8 #include "base/callback.h"
9 #include "components/clipboard/public/interfaces/clipboard.mojom.h"
10 #include "components/resource_provider/public/interfaces/resource_provider.mojom.h"
11 #include "components/view_manager/public/interfaces/gpu.mojom.h"
12 #include "components/view_manager/public/interfaces/view_tree_host.mojom.h"
13 #include "components/web_view/frame_tree.h"
14 #include "mojo/application/public/cpp/application_connection.h"
15 #include "mojo/application/public/cpp/application_impl.h"
16 #include "mojo/services/network/public/interfaces/cookie_store.mojom.h"
17 #include "mojo/services/network/public/interfaces/url_loader_factory.mojom.h"
18 #include "mojo/services/network/public/interfaces/web_socket_factory.mojom.h"
19 #include "mojo/services/tracing/public/interfaces/tracing.mojom.h"
21 #if defined(OS_LINUX) && !defined(OS_ANDROID)
22 #include "components/font_service/public/interfaces/font_service.mojom.h"
28 // Callback from when the content handler id is obtained.
29 void OnGotContentHandlerForFrame(
30 const uint32_t existing_content_handler_id
,
31 const FrameTreeDelegate::CanNavigateFrameCallback
& callback
,
32 scoped_ptr
<FrameConnection
> connection
) {
33 mojo::ViewTreeClientPtr view_tree_client
;
34 if (existing_content_handler_id
!= connection
->GetContentHandlerID() ||
35 FrameTree::AlwaysCreateNewFrameTree()) {
36 view_tree_client
= connection
->GetViewTreeClient();
38 FrameConnection
* connection_ptr
= connection
.get();
39 callback
.Run(connection_ptr
->GetContentHandlerID(),
40 connection_ptr
->frame_tree_client(), connection
.Pass(),
41 view_tree_client
.Pass());
46 FrameConnection::FrameConnection() : application_connection_(nullptr) {
49 FrameConnection::~FrameConnection() {
53 void FrameConnection::CreateConnectionForCanNavigateFrame(
54 mojo::ApplicationImpl
* app
,
56 mojo::URLRequestPtr request
,
57 const FrameTreeDelegate::CanNavigateFrameCallback
& callback
) {
58 scoped_ptr
<FrameConnection
> frame_connection(new FrameConnection
);
59 FrameConnection
* connection
= frame_connection
.get();
60 connection
->Init(app
, request
.Pass(),
61 base::Bind(&OnGotContentHandlerForFrame
, frame
->app_id(),
62 callback
, base::Passed(&frame_connection
)));
65 void FrameConnection::Init(mojo::ApplicationImpl
* app
,
66 mojo::URLRequestPtr request
,
67 const base::Closure
& on_got_id_callback
) {
68 DCHECK(!application_connection_
);
70 mojo::CapabilityFilterPtr
filter(mojo::CapabilityFilter::New());
71 mojo::Array
<mojo::String
> resource_provider_interfaces
;
72 resource_provider_interfaces
.push_back(
73 resource_provider::ResourceProvider::Name_
);
74 filter
->filter
.insert("mojo:resource_provider",
75 resource_provider_interfaces
.Pass());
77 mojo::Array
<mojo::String
> network_service_interfaces
;
78 network_service_interfaces
.push_back(mojo::CookieStore::Name_
);
79 network_service_interfaces
.push_back(mojo::URLLoaderFactory::Name_
);
80 network_service_interfaces
.push_back(mojo::WebSocketFactory::Name_
);
81 filter
->filter
.insert("mojo:network_service",
82 network_service_interfaces
.Pass());
84 mojo::Array
<mojo::String
> clipboard_interfaces
;
85 clipboard_interfaces
.push_back(mojo::Clipboard::Name_
);
86 filter
->filter
.insert("mojo:clipboard", clipboard_interfaces
.Pass());
88 mojo::Array
<mojo::String
> tracing_interfaces
;
89 tracing_interfaces
.push_back(tracing::StartupPerformanceDataCollector::Name_
);
90 filter
->filter
.insert("mojo:tracing", tracing_interfaces
.Pass());
92 mojo::Array
<mojo::String
> view_manager_interfaces
;
93 view_manager_interfaces
.push_back(mojo::Gpu::Name_
);
94 view_manager_interfaces
.push_back(mojo::ViewTreeHostFactory::Name_
);
95 filter
->filter
.insert("mojo:view_manager", view_manager_interfaces
.Pass());
97 #if defined(OS_LINUX) && !defined(OS_ANDROID)
98 mojo::Array
<mojo::String
> font_service_interfaces
;
99 font_service_interfaces
.push_back(font_service::FontService::Name_
);
100 filter
->filter
.insert("mojo:font_service", font_service_interfaces
.Pass());
103 application_connection_
= app
->ConnectToApplicationWithCapabilityFilter(
104 request
.Pass(), filter
.Pass());
105 application_connection_
->ConnectToService(&frame_tree_client_
);
106 frame_tree_client_
.set_connection_error_handler([]() {
107 // TODO(sky): implement this.
111 application_connection_
->AddContentHandlerIDCallback(on_got_id_callback
);
114 mojo::ViewTreeClientPtr
FrameConnection::GetViewTreeClient() {
115 DCHECK(application_connection_
);
116 mojo::ViewTreeClientPtr view_tree_client
;
117 application_connection_
->ConnectToService(&view_tree_client
);
118 return view_tree_client
.Pass();
121 uint32_t FrameConnection::GetContentHandlerID() const {
122 uint32_t content_handler_id
= mojo::Shell::kInvalidContentHandlerID
;
123 if (!application_connection_
->GetContentHandlerID(&content_handler_id
))
125 return content_handler_id
;
128 } // namespace web_view