File: birch_keyed_service.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (211 lines) | stat: -rw-r--r-- 7,213 bytes parent folder | download | duplicates (6)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright 2023 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/ui/ash/birch/birch_keyed_service.h"

#include <memory>
#include <optional>

#include "ash/birch/birch_model.h"
#include "ash/shell.h"
#include "base/functional/bind.h"
#include "chrome/browser/ash/file_suggest/file_suggest_keyed_service_factory.h"
#include "chrome/browser/favicon/favicon_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/ash/birch/birch_calendar_provider.h"
#include "chrome/browser/ui/ash/birch/birch_file_suggest_provider.h"
#include "chrome/browser/ui/ash/birch/birch_last_active_provider.h"
#include "chrome/browser/ui/ash/birch/birch_lost_media_provider.h"
#include "chrome/browser/ui/ash/birch/birch_most_visited_provider.h"
#include "chrome/browser/ui/ash/birch/birch_recent_tabs_provider.h"
#include "chrome/browser/ui/ash/birch/birch_release_notes_provider.h"
#include "chrome/browser/ui/ash/birch/birch_self_share_provider.h"
#include "chrome/browser/ui/ash/birch/refresh_token_waiter.h"
#include "chrome/grit/chrome_unscaled_resources.h"
#include "components/favicon/core/favicon_service.h"
#include "components/favicon_base/favicon_types.h"
#include "ui/base/models/image_model.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_operations.h"

namespace ash {

namespace {

// The file within the cryptohome to save removed items into.
constexpr char kRemovedBirchItemsFile[] = "birch/removed_items.pb";

// The minimum size to query for favicons.
constexpr int kMinimumFaviconSize = 32;

// Utility method to resize the raw favicon bitmap into a `gfx::Image`.
gfx::Image ResizeLargeIcon(
    const favicon_base::FaviconRawBitmapResult& db_result,
    int desired_size) {
  gfx::Image image = gfx::Image::CreateFrom1xPNGBytes(db_result.bitmap_data);

  SkBitmap resized = skia::ImageOperations::Resize(
      image.AsBitmap(), skia::ImageOperations::RESIZE_BEST, desired_size,
      desired_size);

  return gfx::Image::CreateFrom1xBitmap(resized);
}

// Callback for FaviconService icon lookup that uses the
// `favicon_base::FaviconRawBitmapResult`.
void OnGotFaviconImageRaw(
    base::OnceCallback<void(const ui::ImageModel&)> callback,
    const favicon_base::FaviconRawBitmapResult& image_result) {
  if (!image_result.is_valid()) {
    std::move(callback).Run(ui::ImageModel());
    return;
  }

  gfx::Image image = ResizeLargeIcon(image_result, kMinimumFaviconSize);

  std::move(callback).Run(ui::ImageModel::FromImage(image));
}

}  // namespace

BirchKeyedService::BirchKeyedService(Profile* profile)
    : profile_(profile),
      calendar_provider_(std::make_unique<BirchCalendarProvider>(profile)),
      file_suggest_provider_(
          std::make_unique<BirchFileSuggestProvider>(profile)),
      recent_tabs_provider_(std::make_unique<BirchRecentTabsProvider>(profile)),
      last_active_provider_(std::make_unique<BirchLastActiveProvider>(profile)),
      most_visited_provider_(
          std::make_unique<BirchMostVisitedProvider>(profile)),
      release_notes_provider_(
          std::make_unique<BirchReleaseNotesProvider>(profile)),
      self_share_provider_(std::make_unique<BirchSelfShareProvider>(profile)),
      lost_media_provider_(std::make_unique<BirchLostMediaProvider>(profile)),
      refresh_token_waiter_(std::make_unique<RefreshTokenWaiter>(profile)) {
  calendar_provider_->Initialize();
  Shell::Get()->birch_model()->SetClientAndInit(this);
  shell_observation_.Observe(Shell::Get());
}

BirchKeyedService::~BirchKeyedService() {
  ShutdownBirch();
}

void BirchKeyedService::OnShellDestroying() {
  ShutdownBirch();
}

BirchDataProvider* BirchKeyedService::GetCalendarProvider() {
  if (calendar_provider_for_test_) {
    return calendar_provider_for_test_;
  }
  return calendar_provider_.get();
}

BirchDataProvider* BirchKeyedService::GetFileSuggestProvider() {
  if (file_suggest_provider_for_test_) {
    return file_suggest_provider_for_test_;
  }
  return file_suggest_provider_.get();
}

BirchDataProvider* BirchKeyedService::GetRecentTabsProvider() {
  if (recent_tabs_provider_for_test_) {
    return recent_tabs_provider_for_test_;
  }
  return recent_tabs_provider_.get();
}

BirchDataProvider* BirchKeyedService::GetLastActiveProvider() {
  if (last_active_provider_for_test_) {
    return last_active_provider_for_test_;
  }
  return last_active_provider_.get();
}

BirchDataProvider* BirchKeyedService::GetMostVisitedProvider() {
  if (most_visited_provider_for_test_) {
    return most_visited_provider_for_test_;
  }
  return most_visited_provider_.get();
}

BirchDataProvider* BirchKeyedService::GetReleaseNotesProvider() {
  if (release_notes_provider_for_test_) {
    return release_notes_provider_for_test_;
  }
  return release_notes_provider_.get();
}

BirchDataProvider* BirchKeyedService::GetSelfShareProvider() {
  if (self_share_provider_for_test_) {
    return self_share_provider_for_test_;
  }
  return self_share_provider_.get();
}

BirchDataProvider* BirchKeyedService::GetLostMediaProvider() {
  if (lost_media_provider_for_test_) {
    return lost_media_provider_for_test_;
  }
  return lost_media_provider_.get();
}

void BirchKeyedService::WaitForRefreshTokens(base::OnceClosure callback) {
  refresh_token_waiter_->Wait(std::move(callback));
}

base::FilePath BirchKeyedService::GetRemovedItemsFilePath() {
  return profile_->GetPath().AppendASCII(kRemovedBirchItemsFile);
}

void BirchKeyedService::RemoveFileItemFromLauncher(const base::FilePath& path) {
  std::vector<base::FilePath> file_paths;
  file_paths.push_back(path);
  auto* file_suggest_keyed_service =
      FileSuggestKeyedServiceFactory::GetInstance()->GetService(profile_);
  file_suggest_keyed_service->RemoveSuggestionsAndNotify(file_paths);
}

void BirchKeyedService::GetFaviconImage(
    const GURL& url,
    const bool is_page_url,
    base::OnceCallback<void(const ui::ImageModel&)> callback) {
  favicon::FaviconService* service =
      FaviconServiceFactory::GetInstance()->GetForProfile(
          profile_, ServiceAccessType::EXPLICIT_ACCESS);
  favicon_base::IconType icon_type = favicon_base::IconType::kFavicon;

  if (is_page_url) {
    const favicon_base::IconTypeSet icon_types = {icon_type};
    service->GetLargestRawFaviconForPageURL(
        url, {icon_types}, kMinimumFaviconSize,
        base::BindOnce(&OnGotFaviconImageRaw, std::move(callback)),
        &cancelable_task_tracker_);
  } else {
    service->GetRawFavicon(
        url, icon_type, kMinimumFaviconSize,
        base::BindOnce(&OnGotFaviconImageRaw, std::move(callback)),
        &cancelable_task_tracker_);
  }
}

ui::ImageModel BirchKeyedService::GetChromeBackupIcon() {
  return ui::ImageModel::FromImageSkia(
      *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
          IDR_CHROME_APP_ICON_192));
}

void BirchKeyedService::ShutdownBirch() {
  if (is_shutdown_) {
    return;
  }
  is_shutdown_ = true;
  shell_observation_.Reset();
  Shell::Get()->birch_model()->SetClientAndInit(nullptr);
  calendar_provider_->Shutdown();
}

}  // namespace ash