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/mus/public/interfaces/gpu.mojom.h"
11 #include "components/mus/public/interfaces/view_tree_host.mojom.h"
12 #include "components/resource_provider/public/interfaces/resource_provider.mojom.h"
13 #include "components/web_view/frame_tree.h"
14 #include "components/web_view/frame_utils.h"
15 #include "components/web_view/test_runner/public/interfaces/layout_test_runner.mojom.h"
16 #include "mojo/application/public/cpp/application_connection.h"
17 #include "mojo/application/public/cpp/application_impl.h"
18 #include "mojo/services/network/public/interfaces/cookie_store.mojom.h"
19 #include "mojo/services/network/public/interfaces/url_loader_factory.mojom.h"
20 #include "mojo/services/network/public/interfaces/web_socket_factory.mojom.h"
21 #include "mojo/services/tracing/public/interfaces/tracing.mojom.h"
23 #if defined(OS_LINUX) && !defined(OS_ANDROID)
24 #include "components/font_service/public/interfaces/font_service.mojom.h"
30 // Callback from when the content handler id is obtained.
31 void OnGotContentHandlerForFrame(
32 const uint32_t existing_content_handler_id
,
33 const FrameTreeDelegate::CanNavigateFrameCallback
& callback
,
34 scoped_ptr
<FrameConnection
> connection
) {
35 mojo::ViewTreeClientPtr view_tree_client
;
36 if (!AreAppIdsEqual(existing_content_handler_id
,
37 connection
->GetContentHandlerID())) {
38 view_tree_client
= connection
->GetViewTreeClient();
40 FrameConnection
* connection_ptr
= connection
.get();
41 callback
.Run(connection_ptr
->GetContentHandlerID(),
42 connection_ptr
->frame_client(), connection
.Pass(),
43 view_tree_client
.Pass());
48 FrameConnection::FrameConnection() : application_connection_(nullptr) {
51 FrameConnection::~FrameConnection() {
55 void FrameConnection::CreateConnectionForCanNavigateFrame(
56 mojo::ApplicationImpl
* app
,
58 mojo::URLRequestPtr request
,
59 const FrameTreeDelegate::CanNavigateFrameCallback
& callback
) {
60 scoped_ptr
<FrameConnection
> frame_connection(new FrameConnection
);
61 FrameConnection
* connection
= frame_connection
.get();
62 connection
->Init(app
, request
.Pass(),
63 base::Bind(&OnGotContentHandlerForFrame
, frame
->app_id(),
64 callback
, base::Passed(&frame_connection
)));
67 void FrameConnection::Init(mojo::ApplicationImpl
* app
,
68 mojo::URLRequestPtr request
,
69 const base::Closure
& on_got_id_callback
) {
70 DCHECK(!application_connection_
);
72 mojo::CapabilityFilterPtr
filter(mojo::CapabilityFilter::New());
73 mojo::Array
<mojo::String
> resource_provider_interfaces
;
74 resource_provider_interfaces
.push_back(
75 resource_provider::ResourceProvider::Name_
);
76 filter
->filter
.insert("mojo:resource_provider",
77 resource_provider_interfaces
.Pass());
79 mojo::Array
<mojo::String
> network_service_interfaces
;
80 network_service_interfaces
.push_back(mojo::CookieStore::Name_
);
81 network_service_interfaces
.push_back(mojo::URLLoaderFactory::Name_
);
82 network_service_interfaces
.push_back(mojo::WebSocketFactory::Name_
);
83 filter
->filter
.insert("mojo:network_service",
84 network_service_interfaces
.Pass());
86 mojo::Array
<mojo::String
> clipboard_interfaces
;
87 clipboard_interfaces
.push_back(mojo::Clipboard::Name_
);
88 filter
->filter
.insert("mojo:clipboard", clipboard_interfaces
.Pass());
90 mojo::Array
<mojo::String
> tracing_interfaces
;
91 tracing_interfaces
.push_back(tracing::StartupPerformanceDataCollector::Name_
);
92 filter
->filter
.insert("mojo:tracing", tracing_interfaces
.Pass());
94 mojo::Array
<mojo::String
> view_manager_interfaces
;
95 view_manager_interfaces
.push_back(mojo::Gpu::Name_
);
96 view_manager_interfaces
.push_back(mojo::ViewTreeHostFactory::Name_
);
97 filter
->filter
.insert("mojo:mus", view_manager_interfaces
.Pass());
99 mojo::Array
<mojo::String
> test_runner_interfaces
;
100 test_runner_interfaces
.push_back(LayoutTestRunner::Name_
);
101 filter
->filter
.insert("mojo:web_view_test_runner",
102 test_runner_interfaces
.Pass());
104 #if defined(OS_LINUX) && !defined(OS_ANDROID)
105 mojo::Array
<mojo::String
> font_service_interfaces
;
106 font_service_interfaces
.push_back(font_service::FontService::Name_
);
107 filter
->filter
.insert("mojo:font_service", font_service_interfaces
.Pass());
110 application_connection_
= app
->ConnectToApplicationWithCapabilityFilter(
111 request
.Pass(), filter
.Pass());
112 application_connection_
->ConnectToService(&frame_client_
);
113 application_connection_
->AddContentHandlerIDCallback(on_got_id_callback
);
116 mojo::ViewTreeClientPtr
FrameConnection::GetViewTreeClient() {
117 DCHECK(application_connection_
);
118 mojo::ViewTreeClientPtr view_tree_client
;
119 application_connection_
->ConnectToService(&view_tree_client
);
120 return view_tree_client
.Pass();
123 uint32_t FrameConnection::GetContentHandlerID() const {
124 uint32_t content_handler_id
= mojo::Shell::kInvalidContentHandlerID
;
125 if (!application_connection_
->GetContentHandlerID(&content_handler_id
))
127 return content_handler_id
;
130 } // namespace web_view