File: svg_icon_transcoder.h

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; 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 (99 lines) | stat: -rw-r--r-- 3,848 bytes parent folder | download | duplicates (4)
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
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_ICON_TRANSCODER_SVG_ICON_TRANSCODER_H_
#define CHROME_BROWSER_ICON_TRANSCODER_SVG_ICON_TRANSCODER_H_

#include <memory>
#include <string>
#include <vector>

#include "base/files/file_path.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_process_host_observer.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/image/image.h"
#include "url/gurl.h"

namespace content {
class BrowserContext;
class WebContents;
}  // namespace content

namespace apps {

using IconContentCallback = base::OnceCallback<void(std::string)>;

// SvgIconTranscoder uses WebContents to transform an svg icon (as a file or
// as string data) into an SkBitmap and thence into png compressed data which
// can be written to a png file. In principal, this technique should work for
// any data:image/ mime-types supported by WebContents, but svg is all we need
// right now. File handling happens in the browser process.
// Some validation of svg data is performed prior to asking a WebContents
// renderer process (which could potentially die on bad data) to render the
// image. Since a renderer process can be destroyed for many valid reasons,
// SvgIconTranscoder always checks if its WebContents must be recreated.
class SvgIconTranscoder : public content::RenderProcessHostObserver {
 public:
  explicit SvgIconTranscoder(content::BrowserContext* context);

  SvgIconTranscoder(const SvgIconTranscoder&) = delete;
  SvgIconTranscoder& operator=(const SvgIconTranscoder&) = delete;
  ~SvgIconTranscoder() override;

  // Reads the svg data at svg_path and invokes the string Transcode method.
  // |callback| is invoked with and empty string on failure. Blocking call.
  void Transcode(const base::FilePath&& svg_path,
                 const base::FilePath&& png_path,
                 gfx::Size preferred_size,
                 IconContentCallback callback);

  // Validates and trims the svg_data before base64 encoding and dispatching to
  // |web_contents_| in a data: URI.  |callback| is invoked with and empty
  // string on failure. Blocking call.
  void Transcode(const std::string& svg_data,
                 const base::FilePath&& png_path,
                 gfx::Size preferred_size,
                 IconContentCallback callback);

  base::WeakPtr<SvgIconTranscoder> GetWeakPtr() {
    return weak_ptr_factory_.GetWeakPtr();
  }

 private:
  void MaybeCreateWebContents();

  bool PrepareWebContents();

  void RemoveObserver();

  // content::RenderProcessHostObserver:
  void RenderProcessReady(content::RenderProcessHost* host) override;
  // content::RenderProcessHostObserver:
  void RenderProcessExited(
      content::RenderProcessHost* host,
      const content::ChildProcessTerminationInfo& info) override;

  // Compresses the first received bitmap and  saves compressed data to
  // |png_path| if non-empty. If the file can't be saved, that's not considered
  // and error. Next time lucky.
  void OnDownloadImage(base::FilePath png_path,
                       IconContentCallback callback,
                       int id,
                       int http_status_code,
                       const GURL& image_url,
                       const std::vector<SkBitmap>& bitmaps,
                       const std::vector<gfx::Size>& sizes);

  const raw_ptr<content::BrowserContext, DanglingUntriaged> browser_context_;
  std::unique_ptr<content::WebContents> web_contents_;
  bool web_contents_ready_{false};
  base::WeakPtrFactory<SvgIconTranscoder> weak_ptr_factory_{this};
};

}  // namespace apps

#endif  // CHROME_BROWSER_ICON_TRANSCODER_SVG_ICON_TRANSCODER_H_