File: file_handler.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 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 (102 lines) | stat: -rw-r--r-- 3,559 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
// 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 "components/services/app_service/public/cpp/file_handler.h"

#include <tuple>

#include "base/strings/string_util.h"

namespace apps {

FileHandler::FileHandler() = default;
FileHandler::~FileHandler() = default;
FileHandler::FileHandler(const FileHandler& file_handler) = default;

FileHandler::AcceptEntry::AcceptEntry() = default;
FileHandler::AcceptEntry::~AcceptEntry() = default;
FileHandler::AcceptEntry::AcceptEntry(const AcceptEntry& accept_entry) =
    default;

base::Value FileHandler::AcceptEntry::AsDebugValue() const {
  base::Value::Dict root;

  root.Set("mime_type", mime_type);
  base::Value::List& file_extensions_json = *root.EnsureList("file_extensions");
  for (const std::string& file_extension : file_extensions)
    file_extensions_json.Append(file_extension);

  return base::Value(std::move(root));
}

base::Value FileHandler::AsDebugValue() const {
  base::Value::Dict root;

  base::Value::List& accept_json = *root.EnsureList("accept");
  for (const AcceptEntry& entry : accept)
    accept_json.Append(entry.AsDebugValue());
  root.Set("action", action.spec());
  base::Value::List& icons_json = *root.EnsureList("downloaded_icons");
  for (const IconInfo& entry : downloaded_icons)
    icons_json.Append(entry.AsDebugValue());
  root.Set("name", display_name);
  root.Set("launch_type", launch_type == LaunchType::kSingleClient
                              ? "kSingleClient"
                              : "kMultipleClients");

  return base::Value(std::move(root));
}

std::set<std::string> GetMimeTypesFromFileHandlers(
    const FileHandlers& file_handlers) {
  std::set<std::string> mime_types;
  for (const auto& file_handler : file_handlers) {
    std::set<std::string> file_handler_mime_types =
        GetMimeTypesFromFileHandler(file_handler);
    mime_types.insert(file_handler_mime_types.begin(),
                      file_handler_mime_types.end());
  }
  return mime_types;
}

std::set<std::string> GetMimeTypesFromFileHandler(
    const FileHandler& file_handler) {
  std::set<std::string> mime_types;
  for (const auto& accept_entry : file_handler.accept)
    mime_types.insert(accept_entry.mime_type);
  return mime_types;
}

std::set<std::string> GetFileExtensionsFromFileHandlers(
    const FileHandlers& file_handlers) {
  std::set<std::string> file_extensions;
  for (const auto& file_handler : file_handlers) {
    std::set<std::string> file_handler_file_extensions =
        GetFileExtensionsFromFileHandler(file_handler);
    file_extensions.insert(file_handler_file_extensions.begin(),
                           file_handler_file_extensions.end());
  }
  return file_extensions;
}

std::set<std::string> GetFileExtensionsFromFileHandler(
    const FileHandler& file_handler) {
  std::set<std::string> file_extensions;
  for (const auto& accept_entry : file_handler.accept) {
    for (const std::string& extension : accept_entry.file_extensions) {
      file_extensions.insert(base::ToLowerASCII(extension));
    }
  }
  return file_extensions;
}

bool operator==(const FileHandler& file_handler1,
                const FileHandler& file_handler2) {
  return std::tie(file_handler1.action, file_handler1.accept,
                  file_handler1.display_name, file_handler1.launch_type) ==
         std::tie(file_handler2.action, file_handler2.accept,
                  file_handler2.display_name, file_handler2.launch_type);
}

}  // namespace apps