File: app_icon_source.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 (122 lines) | stat: -rw-r--r-- 4,167 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
// 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/apps/app_service/app_icon_source.h"

#include <string>
#include <string_view>

#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/memory/ref_counted_memory.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "chrome/browser/apps/app_service/app_icon/dip_px_util.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/url_constants.h"
#include "components/services/app_service/public/cpp/app_types.h"
#include "components/services/app_service/public/cpp/icon_types.h"
#include "extensions/grit/extensions_browser_resources.h"
#include "ui/base/resource/resource_bundle.h"
#include "url/gurl.h"

namespace apps {

namespace {

void LoadDefaultImage(content::URLDataSource::GotDataCallback callback) {
  std::string_view contents =
      ui::ResourceBundle::GetSharedInstance().GetRawDataResourceForScale(
          IDR_APP_DEFAULT_ICON, apps_util::GetPrimaryDisplayUIScaleFactor());

  base::RefCountedBytes* image_bytes = new base::RefCountedBytes();
  image_bytes->as_vector().assign(contents.begin(), contents.end());
  std::move(callback).Run(image_bytes);
}

void RunCallback(content::URLDataSource::GotDataCallback callback,
                 apps::IconValuePtr iv) {
  if (!iv || iv->compressed.empty()) {
    LoadDefaultImage(std::move(callback));
    return;
  }
  base::RefCountedBytes* image_bytes =
      new base::RefCountedBytes(iv->compressed);
  std::move(callback).Run(image_bytes);
}

}  // namespace

AppIconSource::AppIconSource(Profile* profile) : profile_(profile) {}

AppIconSource::~AppIconSource() = default;

// static
GURL AppIconSource::GetIconURL(const std::string& app_id, int icon_size) {
  GURL icon_url(base::StringPrintf("%s%s/%d", chrome::kChromeUIAppIconURL,
                                   app_id.c_str(), icon_size));
  CHECK(icon_url.is_valid());
  return icon_url;
}

std::string AppIconSource::GetSource() {
  return chrome::kChromeUIAppIconHost;
}

void AppIconSource::StartDataRequest(
    const GURL& url,
    const content::WebContents::Getter& wc_getter,
    content::URLDataSource::GotDataCallback callback) {
  std::string request_path = content::URLDataSource::URLToRequestPath(url);
  std::vector<std::string> path_parts = base::SplitString(
      request_path, "/", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);

  // Check whether data exists, load default image if not.
  if (request_path.empty() || path_parts.size() < 2) {
    LoadDefaultImage(std::move(callback));
    return;
  }

  // Check whether data is of correct type, load default image if not.
  std::string size_param = base::ToLowerASCII(path_parts[1]);
  size_t query_position = size_param.find("?");
  if (query_position != std::string::npos) {
    size_param = size_param.substr(0, query_position);
  }
  int size_in_dip = 0;
  if (!base::StringToInt(size_param, &size_in_dip) || size_in_dip < 1) {
    LoadDefaultImage(std::move(callback));
    return;
  }

  auto* app_service_proxy =
      apps::AppServiceProxyFactory::GetForProfile(profile_);
  const std::string app_id = path_parts[0];
  app_service_proxy->LoadIcon(
      app_id, IconType::kCompressed, size_in_dip,
      /*allow_placeholder_icon=*/false,
      base::BindOnce(&RunCallback, std::move(callback)));
}

std::string AppIconSource::GetMimeType(const GURL&) {
  // We need to explicitly return a mime type, otherwise if the user tries to
  // drag the image they get no extension.
  return "image/png";
}

bool AppIconSource::AllowCaching() {
  // Should not be cached as caching is performed by proxy.
  return false;
}

bool AppIconSource::ShouldReplaceExistingSource() {
  // The source doesn't maintain its own state so there's no need to replace it.
  return false;
}

}  // namespace apps