File: web_file_handlers_permission_handler.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 (294 lines) | stat: -rw-r--r-- 11,512 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// 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/extensions/file_handlers/web_file_handlers_permission_handler.h"

#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>

#include "base/files/file_path.h"
#include "base/files/safe_base_name.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/i18n/message_formatter.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/extensions/extensions_dialogs.h"
#include "chrome/browser/web_applications/web_app_utils.h"
#include "chrome/grit/generated_resources.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/pref_types.h"
#include "extensions/common/extension.h"
#include "extensions/common/features/feature.h"
#include "extensions/common/features/feature_provider.h"
#include "extensions/common/manifest_handlers/web_file_handlers_info.h"
#include "ui/base/interaction/element_identifier.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/dialog_model.h"
#include "ui/base/models/dialog_model_field.h"
#include "ui/base/mojom/ui_base_types.mojom-shared.h"
#include "ui/views/bubble/bubble_dialog_model_host.h"
#include "ui/views/widget/widget_delegate.h"
#include "ui/views/window/dialog_delegate.h"

DEFINE_ELEMENT_IDENTIFIER_VALUE(kWebFileHandlersFileLaunchDialogCheckbox);

namespace extensions {

namespace {

constexpr PrefMap kPrefShouldOpen{"web_file_handlers_should_open",
                                  PrefType::kBool,
                                  PrefScope::kExtensionSpecific};

// Get extension prefs for profile.
ExtensionPrefs* GetExtensionPrefs(Profile* profile) {
  return ExtensionPrefs::Get(profile);
}

// Get dictionary of extension prefs.
std::optional<bool> GetExtensionPrefsAsBoolean(
    ExtensionPrefs* extension_prefs,
    const ExtensionId& extension_id) {
  bool out_value = false;
  if (extension_prefs->ReadPrefAsBoolean(extension_id, kPrefShouldOpen,
                                         &out_value)) {
    return out_value;
  }
  return std::nullopt;
}

// Get extension pref result.
std::optional<bool> GetExtensionPrefsAsBoolean(const Extension& extension,
                                               Profile* profile) {
  return GetExtensionPrefsAsBoolean(GetExtensionPrefs(profile), extension.id());
}

class WebFileHandlersFileLaunchDialogDelegate : public ui::DialogModelDelegate {
 public:
  explicit WebFileHandlersFileLaunchDialogDelegate(
      base::OnceCallback<void(bool, bool)> callback,
      ui::ElementIdentifier checkbox_identifier)
      : callback_(std::move(callback)),
        checkbox_identifier_(checkbox_identifier) {}

  ~WebFileHandlersFileLaunchDialogDelegate() override = default;

  // Determine whether to open the file and maybe remember the decision.
  void OnDialogAccepted() { Run(/*should_open=*/true); }

  void OnDialogClosed() { Run(/*should_open=*/false); }

  // If escape is pressed, neither open the file nor remember the decision.
  void SetActionCloseCallback() {
    std::move(callback_).Run(/*should_open=*/false, /*should_remember=*/false);
  }

 private:
  base::OnceCallback<void(bool, bool)> callback_;

  void Run(bool should_open) {
    auto* checkbox =
        this->dialog_model()->GetCheckboxByUniqueId(checkbox_identifier_);
    bool should_remember = checkbox->is_checked();
    std::move(callback_).Run(should_open, should_remember);
  }

  // The element identifier is declared once by this parent and reused here.
  ui::ElementIdentifier checkbox_identifier_;
};

}  // namespace

// static
bool WebFileHandlersPermissionHandler::remember_selection_ = false;

// Open file using Web File Handlers. Maybe show a file launch dialog first.
WebFileHandlersPermissionHandler::WebFileHandlersPermissionHandler(
    Profile* profile)
    : profile_(profile) {}

WebFileHandlersPermissionHandler::~WebFileHandlersPermissionHandler() = default;

base::AutoReset<bool>
WebFileHandlersPermissionHandler::SetRememberSelectionForTesting(
    bool remember_selection) {
  remember_selection_ = remember_selection;
  return base::AutoReset<bool>(&remember_selection_, remember_selection);
}

void WebFileHandlersPermissionHandler::Confirm(
    const Extension& extension,
    const std::vector<base::SafeBaseName>& base_names,
    CallbackType launch_callback) {
  CHECK(!base_names.empty());

  // Default installed extensions can skip the file launch dialog.
  // TODO(crbug.com/40269541): Remove the allowlist check after development.
  // Also, for development and manual testing purposes, the manifest_features
  // allowlist can also bypass the dialog.
  if (WebFileHandlers::CanBypassPermissionDialog(extension)) {
    std::move(launch_callback).Run(/*should_open=*/true);
    return;
  }

  auto apps_file_handlers = GetAppsFileHandlers(extension);
  const std::vector<std::u16string> file_types =
      web_app::TransformFileExtensionsForDisplay(
          apps::GetFileExtensionsFromFileHandlers(apps_file_handlers));

  // Get saved preferences that represents previous file opening decisions.
  const auto current_prefs = GetExtensionPrefsAsBoolean(extension, profile_);
  if (current_prefs.has_value()) {
    std::move(launch_callback).Run(current_prefs.value());
    return;
  }

  // Maybe open the file. Maybe remember the decision to open the file.
  auto callback_after_dialog =
      base::BindOnce(&WebFileHandlersPermissionHandler::CallbackAfterDialog,
                     weak_factory_.GetWeakPtr(), extension.id(), file_types,
                     std::move(launch_callback));

  // Present a contextual file launch dialog.
  ShowFileLaunchDialog(std::move(base_names), file_types,
                       std::move(callback_after_dialog));
}

// static
void WebFileHandlersPermissionHandler::ShowFileLaunchDialog(
    const std::vector<base::SafeBaseName>& base_names,
    const std::vector<std::u16string>& file_types,
    base::OnceCallback<void(bool, bool)> callback) {
  auto checkbox_id =
      ui::ElementIdentifier(kWebFileHandlersFileLaunchDialogCheckbox);

  auto bubble_delegate_unique =
      std::make_unique<WebFileHandlersFileLaunchDialogDelegate>(
          std::move(callback), checkbox_id);
  WebFileHandlersFileLaunchDialogDelegate* bubble_delegate =
      bubble_delegate_unique.get();

  std::u16string title = base::i18n::MessageFormatter::FormatWithNamedArgs(
      l10n_util::GetStringUTF16(IDS_EXTENSIONS_WFH_PERMISSION_HANDLER_FILES),
      "FILE_COUNT", static_cast<int>(base_names.size()), "FILE1",
      base_names[0].path().value());

  // Prepare every file extension for display in the checkbox.
  const auto file_types_for_display = base::JoinString(
      file_types,
      l10n_util::GetStringUTF16(IDS_WEB_APP_FILE_HANDLING_LIST_SEPARATOR));
  ui::DialogModelLabel checkbox_label =
      ui::DialogModelLabel(base::i18n::MessageFormatter::FormatWithNamedArgs(
          l10n_util::GetStringUTF16(
              IDS_WEB_APP_FILE_HANDLING_DIALOG_STICKY_CHOICE),
          "FILE_TYPE_COUNT", static_cast<int>(file_types.size()), "FILE_TYPES",
          file_types_for_display));

  // Prepare checkbox for testing.
  ui::DialogModelCheckbox::Params checkbox_params;
  checkbox_params.SetIsChecked(remember_selection_);
  checkbox_params.SetVisible(true);

  // TODO(crbug.com/40269541): Add extension name and icon. Show files. Design:
  // https://docs.google.com/document/d/1h7ZjryB2zYEjUG9DqPLzAM1iSUXr8ZadUUY02ycExAQ
  std::unique_ptr<ui::DialogModel> dialog_model =
      ui::DialogModel::Builder(std::move(bubble_delegate_unique))
          .SetInternalName("WebFileHandlersFileLaunchDialogView")
          .SetTitle(title)
          .AddCheckbox(checkbox_id, checkbox_label, checkbox_params)
          .AddOkButton(
              base::BindOnce(
                  &WebFileHandlersFileLaunchDialogDelegate::OnDialogAccepted,
                  base::Unretained(bubble_delegate)),
              ui::DialogModel::Button::Params().SetLabel(
                  l10n_util::GetStringUTF16(
                      IDS_WEB_APP_FILE_HANDLING_POSITIVE_BUTTON)))
          .AddCancelButton(
              base::BindOnce(
                  &WebFileHandlersFileLaunchDialogDelegate::OnDialogClosed,
                  base::Unretained(bubble_delegate)),
              ui::DialogModel::Button::Params().SetLabel(
                  l10n_util::GetStringUTF16(
                      IDS_WEB_APP_FILE_HANDLING_NEGATIVE_BUTTON)))
          .SetCloseActionCallback(base::BindOnce(
              &WebFileHandlersFileLaunchDialogDelegate::SetActionCloseCallback,
              base::Unretained(bubble_delegate)))
          .Build();

  std::unique_ptr<views::BubbleDialogModelHost> dialog =
      views::BubbleDialogModelHost::CreateModal(std::move(dialog_model),
                                                ui::mojom::ModalType::kWindow);
  dialog->SetOwnedByWidget(views::WidgetDelegate::OwnedByWidgetPassKey());
  views::Widget* modal_dialog = views::DialogDelegate::CreateDialogWidget(
      std::move(dialog), /*context=*/nullptr, /*parent=*/nullptr);
  modal_dialog->Show();
}

void WebFileHandlersPermissionHandler::CallbackAfterDialog(
    const ExtensionId& extension_id,
    const std::vector<std::u16string>& file_types,
    CallbackType launch_callback,
    bool should_open,
    bool should_remember) {
  // Maybe remember the decision to open the file.
  if (should_remember) {
    auto* extension_prefs = GetExtensionPrefs(profile_);
    extension_prefs->SetBooleanPref(extension_id, kPrefShouldOpen, should_open);
  }

  // Maybe open the file.
  std::move(launch_callback).Run(should_open);
}

// static
const apps::FileHandlers WebFileHandlersPermissionHandler::GetAppsFileHandlers(
    const Extension& extension) {
  apps::FileHandlers web_file_handlers;
  auto* file_handlers = WebFileHandlers::GetFileHandlers(extension);

  if (!file_handlers) {
    return web_file_handlers;
  }

  for (const auto& web_file_handler : *file_handlers) {
    apps::FileHandler file_handler;
    file_handler.action = GURL(web_file_handler.file_handler.action);
    file_handler.display_name =
        base::UTF8ToUTF16(web_file_handler.file_handler.name);

    // Compute `accept`, which contains all mime types and file extensions.
    apps::FileHandler::Accept accept;
    for (const auto [mime_type, file_extension_list] :
         web_file_handler.file_handler.accept.additional_properties) {
      apps::FileHandler::AcceptEntry accept_entry;
      accept_entry.mime_type = mime_type;
      base::flat_set<std::string> file_extensions;
      for (const auto& file_extension : file_extension_list.GetList()) {
        file_extensions.insert(file_extension.GetString());
      }
      accept_entry.file_extensions = file_extensions;
      accept.emplace_back(accept_entry);
    }
    file_handler.accept = accept;

    // The default launch type is single client.
    file_handler.launch_type =
        web_file_handler.launch_type ==
                WebFileHandler::LaunchType::kSingleClient
            ? apps::FileHandler::LaunchType::kSingleClient
            : apps::FileHandler::LaunchType::kMultipleClients;

    web_file_handlers.emplace_back(file_handler);
  }

  return web_file_handlers;
}

}  // namespace extensions