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

Side by Side Diff: chrome/browser/resource_coordinator/resource_coordinator_web_contents_observer.cc

Issue 2938443002: [GRC] UKM Support (Closed)
Patch Set: Address reviewer feedback Created 3 years, 6 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 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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 #include "chrome/browser/resource_coordinator/resource_coordinator_web_contents_ observer.h" 5 #include "chrome/browser/resource_coordinator/resource_coordinator_web_contents_ observer.h"
6 6
7 #include <utility>
8
9 #include "components/ukm/public/interfaces/ukm_interface.mojom.h"
7 #include "content/public/browser/navigation_handle.h" 10 #include "content/public/browser/navigation_handle.h"
8 #include "content/public/browser/render_frame_host.h" 11 #include "content/public/browser/render_frame_host.h"
9 #include "content/public/browser/render_process_host.h" 12 #include "content/public/browser/render_process_host.h"
10 #include "content/public/common/service_manager_connection.h" 13 #include "content/public/common/service_manager_connection.h"
14 #include "content/public/common/service_names.mojom.h"
11 #include "services/resource_coordinator/public/cpp/resource_coordinator_features .h" 15 #include "services/resource_coordinator/public/cpp/resource_coordinator_features .h"
16 #include "services/resource_coordinator/public/interfaces/service_callbacks.mojo m.h"
17 #include "services/resource_coordinator/public/interfaces/service_constants.mojo m.h"
18 #include "services/service_manager/public/cpp/connector.h"
12 19
13 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ResourceCoordinatorWebContentsObserver); 20 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ResourceCoordinatorWebContentsObserver);
14 21
22 bool ResourceCoordinatorWebContentsObserver::ukm_recorder_initialized = false;
23
15 ResourceCoordinatorWebContentsObserver::ResourceCoordinatorWebContentsObserver( 24 ResourceCoordinatorWebContentsObserver::ResourceCoordinatorWebContentsObserver(
16 content::WebContents* web_contents) 25 content::WebContents* web_contents)
17 : WebContentsObserver(web_contents) { 26 : WebContentsObserver(web_contents) {
27 service_manager::Connector* connector =
28 content::ServiceManagerConnection::GetForProcess()->GetConnector();
29
18 tab_resource_coordinator_ = 30 tab_resource_coordinator_ =
19 base::MakeUnique<resource_coordinator::ResourceCoordinatorInterface>( 31 base::MakeUnique<resource_coordinator::ResourceCoordinatorInterface>(
20 content::ServiceManagerConnection::GetForProcess()->GetConnector(), 32 connector, resource_coordinator::CoordinationUnitType::kWebContents);
21 resource_coordinator::CoordinationUnitType::kWebContents); 33
34 connector->BindInterface(resource_coordinator::mojom::kServiceName,
35 mojo::MakeRequest(&service_callbacks_));
36
37 EnsureUkmRecorderInterface();
38 }
39
40 // TODO(matthalp) integrate into ResourceCoordinatorService once the UKM mojo
41 // interface can be accessed directly within GRC. GRC cannot currently use
42 // the UKM mojo inteface directly because of software layering issues
43 // (i.e. the UKM mojo interface is only reachable from the content_browser
44 // service which cannot be accesses by services in the services/ directory).
45 // Instead the ResourceCoordinatorWebContentsObserver is used as it lives
46 // within the content_browser service and can initialize the
47 // UkmRecorderInterface and pass that interface into GRC through
48 // resource_coordinator::mojom::ServiceCallbacks.
49 void ResourceCoordinatorWebContentsObserver::EnsureUkmRecorderInterface() {
50 if (ukm_recorder_initialized) {
51 return;
52 }
53
54 service_callbacks_->IsUkmRecorderInterfaceInitialized(base::Bind(
55 &ResourceCoordinatorWebContentsObserver::MaybeSetUkmRecorderInterface,
56 base::Unretained(this)));
57 }
58
59 void ResourceCoordinatorWebContentsObserver::MaybeSetUkmRecorderInterface(
60 bool ukm_recorder_already_initialized) {
61 if (ukm_recorder_already_initialized) {
62 return;
63 }
64
65 ukm::mojom::UkmRecorderInterfacePtr ukm_recorder_interface;
66 content::ServiceManagerConnection::GetForProcess()
67 ->GetConnector()
68 ->BindInterface(content::mojom::kBrowserServiceName,
69 mojo::MakeRequest(&ukm_recorder_interface));
70 service_callbacks_->SetUkmRecorderInterface(
71 std::move(ukm_recorder_interface));
72 ukm_recorder_initialized = true;
22 } 73 }
23 74
24 ResourceCoordinatorWebContentsObserver:: 75 ResourceCoordinatorWebContentsObserver::
25 ~ResourceCoordinatorWebContentsObserver() = default; 76 ~ResourceCoordinatorWebContentsObserver() = default;
26 77
27 // static 78 // static
28 bool ResourceCoordinatorWebContentsObserver::IsEnabled() { 79 bool ResourceCoordinatorWebContentsObserver::IsEnabled() {
29 return base::FeatureList::IsEnabled(features::kGlobalResourceCoordinator); 80 return base::FeatureList::IsEnabled(features::kGlobalResourceCoordinator);
30 } 81 }
31 82
(...skipping 18 matching lines...) Expand all
50 navigation_handle->GetRenderFrameHost(); 101 navigation_handle->GetRenderFrameHost();
51 102
52 auto* frame_resource_coordinator = 103 auto* frame_resource_coordinator =
53 render_frame_host->GetFrameResourceCoordinator(); 104 render_frame_host->GetFrameResourceCoordinator();
54 tab_resource_coordinator_->AddChild(*frame_resource_coordinator); 105 tab_resource_coordinator_->AddChild(*frame_resource_coordinator);
55 106
56 auto* process_resource_coordinator = 107 auto* process_resource_coordinator =
57 render_frame_host->GetProcess()->GetProcessResourceCoordinator(); 108 render_frame_host->GetProcess()->GetProcessResourceCoordinator();
58 process_resource_coordinator->AddChild(*frame_resource_coordinator); 109 process_resource_coordinator->AddChild(*frame_resource_coordinator);
59 } 110 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698