File: extension_installed_bubble_model.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; 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 (134 lines) | stat: -rw-r--r-- 4,929 bytes parent folder | download | duplicates (2)
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
// Copyright 2020 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/extensions/extension_installed_bubble_model.h"

#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/extensions/commands/command_service.h"
#include "chrome/browser/extensions/extension_sync_util.h"
#include "chrome/browser/extensions/extension_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/signin_promo_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/extensions/api/omnibox/omnibox_handler.h"
#include "chrome/grit/branded_strings.h"
#include "chrome/grit/generated_resources.h"
#include "components/prefs/pref_service.h"
#include "components/signin/public/base/signin_pref_names.h"
#include "extensions/common/api/extension_action/action_info.h"
#include "extensions/common/command.h"
#include "extensions/common/extension.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/image/image_skia_operations.h"

namespace {

std::optional<extensions::Command> CommandForExtensionAction(
    const extensions::Extension* extension,
    Profile* profile) {
  const auto* info = extensions::ActionInfo::GetExtensionActionInfo(extension);

  if (!info) {
    return std::nullopt;
  }

  auto* service = extensions::CommandService::Get(profile);
  extensions::Command command;

  if (service->GetExtensionActionCommand(extension->id(), info->type,
                                         extensions::CommandService::ACTIVE,
                                         &command, nullptr)) {
    return command;
  }

  return std::nullopt;
}

std::u16string MakeHowToUseText(const extensions::ActionInfo* action,
                                std::optional<extensions::Command> command,
                                const std::string& keyword) {
  std::u16string extra;
  if (command.has_value()) {
    extra = command->accelerator().GetShortcutText();
  }

  // TODO(crbug.com/405148986): This returns an empty string for MV3 extensions
  // which specify the "action" key in the manifest since
  // ActionInfo::Type::kAction is not handled. Add the appropriate string here.
  int message_id = 0;
  if (action && action->type == extensions::ActionInfo::Type::kBrowser) {
    message_id =
        extra.empty()
            ? IDS_EXTENSION_INSTALLED_BROWSER_ACTION_INFO
            : IDS_EXTENSION_INSTALLED_BROWSER_ACTION_INFO_WITH_SHORTCUT;
  } else if (action && action->type == extensions::ActionInfo::Type::kPage) {
    message_id = extra.empty()
                     ? IDS_EXTENSION_INSTALLED_PAGE_ACTION_INFO
                     : IDS_EXTENSION_INSTALLED_PAGE_ACTION_INFO_WITH_SHORTCUT;
  } else if (!keyword.empty()) {
    extra = base::UTF8ToUTF16(keyword);
    message_id = IDS_EXTENSION_INSTALLED_OMNIBOX_KEYWORD_INFO;
  }

  if (!message_id) {
    return std::u16string();
  }

  return extra.empty() ? l10n_util::GetStringUTF16(message_id)
                       : l10n_util::GetStringFUTF16(message_id, extra);
}

}  // namespace

ExtensionInstalledBubbleModel::ExtensionInstalledBubbleModel(
    Profile* profile,
    const extensions::Extension* extension,
    const SkBitmap& icon)
    : icon_(icon),
      extension_id_(extension->id()),
      extension_name_(extension->name()) {
  const std::string& keyword = extensions::OmniboxInfo::GetKeyword(extension);
  std::optional<extensions::Command> command =
      CommandForExtensionAction(extension, profile);
  const auto* action_info =
      extensions::ActionInfo::GetExtensionActionInfo(extension);

  const bool toolbar_action = !!action_info;

  anchor_to_action_ = toolbar_action;
  anchor_to_omnibox_ = !toolbar_action && !keyword.empty();

  show_how_to_use_ =
      (toolbar_action && !action_info->synthesized) || !keyword.empty();
  // If there's a shortcut, don't show the how-to-manage text because it
  // clutters the bubble.
  show_how_to_manage_ = !command.has_value() || anchor_to_omnibox_;
  show_key_binding_ = command.has_value();

  if (show_how_to_use_) {
    how_to_use_text_ = MakeHowToUseText(action_info, command, keyword);

    // Don't show how to use if the text returned is empty.
    show_how_to_use_ = !how_to_use_text_.empty();
  }
}

ExtensionInstalledBubbleModel::~ExtensionInstalledBubbleModel() = default;

std::u16string ExtensionInstalledBubbleModel::GetHowToUseText() const {
  DCHECK(show_how_to_use_);
  return how_to_use_text_;
}

gfx::ImageSkia ExtensionInstalledBubbleModel::MakeIconOfSize(
    const gfx::Size& wanted) const {
  gfx::Size size(icon_.width(), icon_.height());
  if (size.width() > wanted.width() || size.height() > wanted.height()) {
    size.SetSize(wanted.width(), wanted.height());
  }

  return gfx::ImageSkiaOperations::CreateResizedImage(
      gfx::ImageSkia::CreateFrom1xBitmap(icon_),
      skia::ImageOperations::RESIZE_BEST, size);
}