decorators_utils.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Copyright 2020 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.
  4. #ifndef COMPONENTS_PERFORMANCE_MANAGER_DECORATORS_DECORATORS_UTILS_H_
  5. #define COMPONENTS_PERFORMANCE_MANAGER_DECORATORS_DECORATORS_UTILS_H_
  6. #include "components/performance_manager/graph/page_node_impl.h"
  7. #include "components/performance_manager/public/performance_manager.h"
  8. #include "content/public/browser/browser_thread.h"
  9. namespace performance_manager {
  10. // Helper function meant to be used by decorators tracking properties associated
  11. // with WebContents. This will do the WebContents to PageNode translation and
  12. // post a task to the PM sequence to set a property on the appropriate
  13. // decorator.
  14. // This function can only be called from the UI thread.
  15. template <typename T, class decorator_data_type>
  16. void SetPropertyForWebContentsPageNode(
  17. content::WebContents* contents,
  18. void (decorator_data_type::*setter_function)(T),
  19. T value) {
  20. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  21. PerformanceManager::CallOnGraph(
  22. FROM_HERE,
  23. base::BindOnce(
  24. [](base::WeakPtr<PageNode> node,
  25. void (decorator_data_type::*setter_function)(T), T value) {
  26. if (node) {
  27. auto* data = decorator_data_type::GetOrCreate(
  28. PageNodeImpl::FromNode(node.get()));
  29. DCHECK(data);
  30. (data->*setter_function)(value);
  31. }
  32. },
  33. PerformanceManager::GetPrimaryPageNodeForWebContents(contents),
  34. setter_function, value));
  35. }
  36. } // namespace performance_manager
  37. #endif // COMPONENTS_PERFORMANCE_MANAGER_DECORATORS_DECORATORS_UTILS_H_