File: page_annotator.h

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 (120 lines) | stat: -rw-r--r-- 4,474 bytes parent folder | download | duplicates (9)
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_PAGE_IMAGE_ANNOTATION_CORE_PAGE_ANNOTATOR_H_
#define COMPONENTS_PAGE_IMAGE_ANNOTATION_CORE_PAGE_ANNOTATOR_H_

#include <map>
#include <string>
#include <utility>

#include "base/compiler_specific.h"
#include "base/functional/callback.h"
#include "base/observer_list.h"
#include "base/observer_list_types.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/image_annotation/public/cpp/image_processor.h"
#include "services/image_annotation/public/mojom/image_annotation.mojom.h"
#include "third_party/skia/include/core/SkBitmap.h"

namespace page_image_annotation {

// Notifies clients of page images that can be annotated and forwards annotation
// requests for these images to the image annotation service.
//
// TODO(crbug.com/41432474): this class is not yet complete - add more logic
// (e.g.
//                         communication with the service).
class PageAnnotator {
 public:
  struct ImageMetadata {
    // A unique ID identifying an image on this page. Two separate images (even
    // with the same URL / pixels) on one page will be given separate node IDs.
    uint64_t node_id;

    // The URL or a hash of the data URI of this image. Two (identical) images
    // can have the same source ID.
    std::string source_id;

    // TODO(crbug.com/41432474): add other useful info (e.g. image dimensions).
  };

  // Clients (i.e. classes that annotate page images) should implement this
  // interface.
  class Observer : public base::CheckedObserver {
   public:
    ~Observer() override;

    // These methods are called during page lifecycle to notify the observer
    // about changes to page images.

    // Called exactly once per image, at the point that the image appears on the
    // page (or at the point that the observer subscribes to the page annotator,
    // if the image already exists on page).
    virtual void OnImageAdded(const ImageMetadata& image) = 0;

    // Called at the point that an image source is updated.
    virtual void OnImageModified(const ImageMetadata& image) = 0;

    // Called at the point that an image disappears from the page.
    virtual void OnImageRemoved(uint64_t node_id) = 0;

    // Called when annotation is complete (either successfully or
    // unsuccessfully) for a request made by this client.
    virtual void OnImageAnnotated(
        uint64_t node_id,
        image_annotation::mojom::AnnotateImageResultPtr result) = 0;
  };

  explicit PageAnnotator(
      mojo::PendingRemote<image_annotation::mojom::Annotator> annotator);

  PageAnnotator(const PageAnnotator&) = delete;
  PageAnnotator& operator=(const PageAnnotator&) = delete;

  ~PageAnnotator();

  // Request annotation of the given image via the image annotation service.
  // When annotation is complete (or fails), the OnImageAnnotated() method of
  // the observer is called.
  //
  // Must be called on a valid (i.e. added and not yet removed) node ID.
  void AnnotateImage(Observer* observer, uint64_t node_id);

  // Called by platform drivers.
  void ImageAddedOrPossiblyModified(
      const ImageMetadata& metadata,
      base::RepeatingCallback<SkBitmap()> pixels_callback);
  void ImageRemoved(uint64_t node_id);

  // An observer must outlive the PageAnnotator, or be destructed synchronously
  // with the PageAnnotator (e.g. at the same point in the document lifecycle)
  // and not reference the PageAnnotator in its destructor.
  void AddObserver(Observer* observer);

 private:
  // Add a new entry to |images_|.
  //
  // The lack of copy/move constructor for ImageProcessor makes this difficult,
  // but we limit the complexity to this method.
  void AddNewImage(const ImageMetadata& metadata,
                   base::RepeatingCallback<SkBitmap()> pixels_callback);

  // Callback passed to the image annotation service to receive image annotation
  // results.
  void NotifyObserver(Observer* observer,
                      uint64_t node_id,
                      image_annotation::mojom::AnnotateImageResultPtr result);

  mojo::Remote<image_annotation::mojom::Annotator> annotator_;

  base::ObserverList<Observer> observers_;

  std::map<uint64_t, std::pair<ImageMetadata, image_annotation::ImageProcessor>>
      images_;
};

}  // namespace page_image_annotation

#endif  // COMPONENTS_PAGE_IMAGE_ANNOTATION_CORE_PAGE_ANNOTATOR_H_