File: share_target_utils.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 (188 lines) | stat: -rw-r--r-- 6,705 bytes parent folder | download | duplicates (3)
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
// 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/web_applications/share_target_utils.h"

#include <algorithm>
#include <optional>

#include "base/memory/scoped_refptr.h"
#include "base/notimplemented.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "build/build_config.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/web_share_target/target_util.h"
#include "components/services/app_service/public/cpp/intent_util.h"
#include "components/services/app_service/public/cpp/share_target.h"
#include "extensions/common/constants.h"
#include "net/base/filename_util.h"
#include "net/base/mime_util.h"
#include "services/network/public/cpp/resource_request_body.h"
#include "storage/browser/file_system/file_system_context.h"
#include "storage/browser/file_system/file_system_url.h"
#include "ui/base/page_transition_types.h"
#include "url/gurl.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/ash/file_manager/fileapi_util.h"
#include "chrome/browser/profiles/profile.h"
#endif

namespace web_app {

std::vector<SharedField> ExtractSharedFields(
    const apps::ShareTarget& share_target,
    const apps::Intent& intent) {
  std::vector<SharedField> result;

  if (!share_target.params.title.empty() && intent.share_title.has_value() &&
      !intent.share_title->empty()) {
    result.push_back(
        {.name = share_target.params.title, .value = *intent.share_title});
  }

  if (!intent.share_text.has_value()) {
    return result;
  }

  apps_util::SharedText extracted_text =
      apps_util::ExtractSharedText(*intent.share_text);

  if (!share_target.params.text.empty() && !extracted_text.text.empty()) {
    result.push_back(
        {.name = share_target.params.text, .value = extracted_text.text});
  }

  if (!share_target.params.url.empty() && !extracted_text.url.is_empty()) {
    result.push_back(
        {.name = share_target.params.url, .value = extracted_text.url.spec()});
  }

  return result;
}

NavigateParams NavigateParamsForShareTarget(
    Browser* browser,
    const apps::ShareTarget& share_target,
    const apps::Intent& intent,
    const std::vector<base::FilePath>& launch_files) {
  NavigateParams nav_params(browser, share_target.action,
                            ui::PAGE_TRANSITION_AUTO_TOPLEVEL);

#if BUILDFLAG(IS_CHROMEOS)
  std::vector<std::string> names;
  std::vector<std::string> values;
  std::vector<bool> is_value_file_uris;
  std::vector<std::string> filenames;
  std::vector<std::string> types;

  if (intent.mime_type.has_value() && !intent.files.empty()) {
    if (!launch_files.empty()) {
      DCHECK_EQ(launch_files.size(), intent.files.size());
    }

    // Files for Web Share intents are created by the browser in
    // a .WebShare directory, with generated file names and file urls - see
    // //chrome/browser/webshare/chromeos/sharesheet_client.cc
    for (size_t i = 0; i < intent.files.size(); ++i) {
      const apps::IntentFilePtr& file = intent.files[i];

      const std::string& mime_type = file->mime_type.has_value()
                                         ? file->mime_type.value()
                                         : intent.mime_type.value();
      std::string name;
      for (const apps::ShareTarget::Files& files : share_target.params.files) {
        // Filter on MIME types. Chrome OS does not filter on file extensions.
        // https://w3c.github.io/web-share-target/level-2/#dfn-accepted
        if (std::ranges::any_of(
                files.accept, [&mime_type](const auto& criteria) {
                  return !base::StartsWith(criteria, ".") &&
                         net::MatchesMimeType(criteria, mime_type);
                })) {
          name = files.name;
          break;
        }
      }
      if (name.empty()) {
        continue;
      }

      storage::FileSystemURL file_system_url;

#if BUILDFLAG(IS_CHROMEOS)
      storage::FileSystemContext* file_system_context =
          file_manager::util::GetFileManagerFileSystemContext(
              browser->profile());
      file_system_url =
          file_system_context->CrackURLInFirstPartyContext(file->url);
#endif  // BUILDFLAG(IS_CHROMEOS)

      const std::string filename =
          (file->file_name.has_value() && !file->file_name->path().empty())
              ? (file->file_name->path().AsUTF8Unsafe())
              : file_system_url.path().BaseName().AsUTF8Unsafe();

      names.push_back(name);

      if (launch_files.empty()) {
        if (file->url.SchemeIsFile()) {
          base::FilePath file_path;
          net::FileURLToFilePath(file->url, &file_path);
          values.push_back(file_path.value());
        } else {
          values.push_back(file_system_url.path().AsUTF8Unsafe());
        }
      } else {
        values.push_back(launch_files[i].value());
      }

      is_value_file_uris.push_back(true);
      filenames.push_back(filename);
      types.push_back(mime_type);
    }
  }

  std::vector<SharedField> shared_fields =
      ExtractSharedFields(share_target, intent);
  for (const auto& shared_field : shared_fields) {
    DCHECK(!shared_field.value.empty());
    names.push_back(shared_field.name);
    values.push_back(shared_field.value);
    is_value_file_uris.push_back(false);
    filenames.emplace_back();
    types.push_back("text/plain");
  }

  if (share_target.enctype == apps::ShareTarget::Enctype::kMultipartFormData) {
    const std::string boundary = net::GenerateMimeMultipartBoundary();
    nav_params.extra_headers = base::StringPrintf(
        "Content-Type: multipart/form-data; boundary=%s\r\n", boundary.c_str());
    nav_params.post_data = web_share_target::ComputeMultipartBody(
        names, values, is_value_file_uris, filenames, types, boundary);
  } else {
    const std::string serialization =
        web_share_target::ComputeUrlEncodedBody(names, values);
    if (share_target.method == apps::ShareTarget::Method::kPost) {
      nav_params.extra_headers =
          "Content-Type: application/x-www-form-urlencoded\r\n";
      nav_params.post_data =
          network::ResourceRequestBody::CreateFromCopyOfBytes(
              base::as_byte_span(serialization));
    } else {
      GURL::Replacements replacements;
      replacements.SetQueryStr(serialization);
      nav_params.url = nav_params.url.ReplaceComponents(replacements);
    }
  }
#else
  // TODO(crbug.com/40158988): Support Web Share Target on Windows.
  // TODO(crbug.com/40734106): Support Web Share Target on Mac.
  NOTIMPLEMENTED();
#endif  // BUILDFLAG(IS_CHROMEOS)

  return nav_params;
}

}  // namespace web_app