1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87

ash / components / arc / memory / arc_memory_bridge.cc [blame]

// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/components/arc/memory/arc_memory_bridge.h"

#include <utility>

#include "ash/components/arc/arc_browser_context_keyed_service_factory_base.h"
#include "ash/components/arc/mojom/memory.mojom-forward.h"
#include "ash/components/arc/session/arc_bridge_service.h"
#include "ash/components/arc/session/connection_observer.h"
#include "base/logging.h"
#include "base/memory/singleton.h"
#include "content/public/browser/browser_context.h"

namespace arc {

// Singleton factory for ArcMemoryBridge.
class ArcMemoryBridgeFactory
    : public internal::ArcBrowserContextKeyedServiceFactoryBase<
          ArcMemoryBridge,
          ArcMemoryBridgeFactory> {
 public:
  // Factory name used by ArcBrowserContextKeyedServiceFactoryBase.
  static constexpr const char* kName = "ArcMemoryBridgeFactory";

  static ArcMemoryBridgeFactory* GetInstance();

 private:
  friend base::DefaultSingletonTraits<ArcMemoryBridgeFactory>;
  ArcMemoryBridgeFactory() = default;
  ~ArcMemoryBridgeFactory() override = default;
};

// static
ArcMemoryBridgeFactory* ArcMemoryBridgeFactory::GetInstance() {
  return base::Singleton<ArcMemoryBridgeFactory>::get();
}

// static
ArcMemoryBridge* ArcMemoryBridge::GetForBrowserContext(
    content::BrowserContext* context) {
  return ArcMemoryBridgeFactory::GetForBrowserContext(context);
}

// static
ArcMemoryBridge* ArcMemoryBridge::GetForBrowserContextForTesting(
    content::BrowserContext* context) {
  return ArcMemoryBridgeFactory::GetForBrowserContextForTesting(context);
}

ArcMemoryBridge::ArcMemoryBridge(content::BrowserContext* context,
                                 ArcBridgeService* bridge_service)
    : arc_bridge_service_(bridge_service) {}

ArcMemoryBridge::~ArcMemoryBridge() {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
}

void ArcMemoryBridge::DropCaches(DropCachesCallback callback) {
  auto* memory_instance =
      ARC_GET_INSTANCE_FOR_METHOD(arc_bridge_service_->memory(), DropCaches);
  if (!memory_instance) {
    std::move(callback).Run(/*result=*/false);
    return;
  }
  memory_instance->DropCaches(std::move(callback));
}

void ArcMemoryBridge::Reclaim(mojom::ReclaimRequestPtr request,
                              ReclaimCallback callback) {
  auto* const memory_instance =
      ARC_GET_INSTANCE_FOR_METHOD(arc_bridge_service_->memory(), Reclaim);
  if (!memory_instance) {
    std::move(callback).Run(mojom::ReclaimResult::New(0, 0));
    return;
  }
  memory_instance->Reclaim(std::move(request), std::move(callback));
}

// static
void ArcMemoryBridge::EnsureFactoryBuilt() {
  ArcMemoryBridgeFactory::GetInstance();
}

}  // namespace arc