File: image_fetcher_service_factory.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (132 lines) | stat: -rw-r--r-- 4,905 bytes parent folder | download | duplicates (5)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/image_fetcher/image_fetcher_service_factory.h"

#include <memory>
#include <utility>

#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/time/default_clock.h"
#include "build/build_config.h"
#include "chrome/browser/image_fetcher/image_decoder_impl.h"
#include "chrome/browser/net/system_network_context_manager.h"
#include "chrome/browser/profiles/profile_key.h"
#include "chrome/common/chrome_paths_internal.h"
#include "components/image_fetcher/core/cache/image_cache.h"
#include "components/image_fetcher/core/cache/image_data_store_disk.h"
#include "components/image_fetcher/core/cache/image_metadata_store_leveldb.h"
#include "components/image_fetcher/core/image_fetcher_service.h"
#include "components/keyed_service/core/simple_dependency_manager.h"
#include "components/keyed_service/core/simple_factory_key.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/storage_partition.h"

#if BUILDFLAG(IS_ANDROID)
#include "components/image_fetcher/image_fetcher_service_provider.h"
#endif

namespace {

// The path under the browser context's data directory which the image_cache
// will be stored.
const base::FilePath::CharType kImageCacheSubdir[] =
    FILE_PATH_LITERAL("image_cache");

base::FilePath GetCachePath(SimpleFactoryKey* key) {
  base::FilePath cache_path;
  chrome::GetUserCacheDirectory(key->GetPath(), &cache_path);
  return cache_path.Append(kImageCacheSubdir);
}

#if BUILDFLAG(IS_ANDROID)
image_fetcher::ImageFetcherService* GetImageFetcherService(
    SimpleFactoryKey* key) {
  return ImageFetcherServiceFactory::GetForKey(key);
}

std::string GetCachePathForJava(SimpleFactoryKey* key, std::string path) {
  base::FilePath cache_path;
  chrome::GetUserCacheDirectory(key->GetPath(), &cache_path);
  return cache_path.Append(kImageCacheSubdir).Append(path).MaybeAsASCII();
}
#endif

}  // namespace

// static
image_fetcher::ImageFetcherService* ImageFetcherServiceFactory::GetForKey(
    SimpleFactoryKey* key) {
  return static_cast<image_fetcher::ImageFetcherService*>(
      GetInstance()->GetServiceForKey(key, true));
}

// static
ImageFetcherServiceFactory* ImageFetcherServiceFactory::GetInstance() {
  static base::NoDestructor<ImageFetcherServiceFactory> instance;
  return instance.get();
}

ImageFetcherServiceFactory::ImageFetcherServiceFactory()
    : SimpleKeyedServiceFactory("ImageFetcherService",
                                SimpleDependencyManager::GetInstance()) {
// In order to move the android code to components, we need to push
// |GetImageFetcherService| to image_fetcher_bridge.
#if BUILDFLAG(IS_ANDROID)
  image_fetcher::SetImageFetcherServiceProvider(
      base::BindRepeating(&GetImageFetcherService));

  image_fetcher::SetImageFetcherCachePathProvider(
      base::BindRepeating(&GetCachePathForJava));
#endif
}

ImageFetcherServiceFactory::~ImageFetcherServiceFactory() = default;

std::unique_ptr<KeyedService>
ImageFetcherServiceFactory::BuildServiceInstanceFor(
    SimpleFactoryKey* key) const {
  base::FilePath cache_path = GetCachePath(key);
  ProfileKey* profile_key = ProfileKey::FromSimpleFactoryKey(key);

  scoped_refptr<base::SequencedTaskRunner> task_runner =
      base::ThreadPool::CreateSequencedTaskRunner(
          {base::MayBlock(), base::TaskPriority::USER_VISIBLE});
  base::DefaultClock* clock = base::DefaultClock::GetInstance();

  auto metadata_store =
      std::make_unique<image_fetcher::ImageMetadataStoreLevelDB>(
          profile_key->GetProtoDatabaseProvider(), cache_path, task_runner,
          clock);
  auto data_store = std::make_unique<image_fetcher::ImageDataStoreDisk>(
      cache_path, task_runner);

  scoped_refptr<image_fetcher::ImageCache> image_cache =
      base::MakeRefCounted<image_fetcher::ImageCache>(
          std::move(data_store), std::move(metadata_store),
          profile_key->GetPrefs(), clock, task_runner);

  scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory;
  // Network is null for some tests, may be removable after
  // https://crbug.com/981057.
  if (SystemNetworkContextManager::GetInstance()) {
    url_loader_factory =
        SystemNetworkContextManager::GetInstance()->GetSharedURLLoaderFactory();
  }

  auto cached_image_fetcher_service =
      std::make_unique<image_fetcher::ImageFetcherService>(
          std::make_unique<ImageDecoderImpl>(), std::move(url_loader_factory),
          std::move(image_cache), key->IsOffTheRecord());
  return cached_image_fetcher_service;
}

SimpleFactoryKey* ImageFetcherServiceFactory::GetKeyToUse(
    SimpleFactoryKey* key) const {
  return key;
}