File: gin_java_bridge_dispatcher.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 (128 lines) | stat: -rw-r--r-- 4,721 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
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/renderer/java/gin_java_bridge_dispatcher.h"

#include <utility>

#include "base/auto_reset.h"
#include "base/containers/contains.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "components/origin_matcher/origin_matcher.h"
#include "content/public/common/content_features.h"
#include "content/public/renderer/render_frame.h"
#include "content/renderer/java/gin_java_bridge_object.h"
#include "third_party/blink/public/platform/browser_interface_broker_proxy.h"
#include "third_party/blink/public/web/web_local_frame.h"
#include "third_party/blink/public/web/web_view.h"
#include "url/origin.h"

namespace content {

GinJavaBridgeDispatcher::GinJavaBridgeDispatcher(RenderFrame* render_frame)
    : RenderFrameObserver(render_frame) {}

GinJavaBridgeDispatcher::~GinJavaBridgeDispatcher() = default;

void GinJavaBridgeDispatcher::DidClearWindowObject() {
  // Accessing window object when adding properties to it may trigger
  // a nested call to DidClearWindowObject.
  if (inside_did_clear_window_object_)
    return;
  base::AutoReset<bool> flag_entry(&inside_did_clear_window_object_, true);
  if (!named_objects_.empty()) {
    // Ensure we have a `remote_` if we have named objects.
    CHECK(remote_);
  }
  for (NamedObjectMap::const_iterator iter = named_objects_.begin();
       iter != named_objects_.end(); ++iter) {
    // Always create a new GinJavaBridgeObject, so we don't pull any of the V8
    // wrapper's custom properties into the context of the page we have
    // navigated to. The old GinJavaBridgeObject will be automatically
    // deleted after its wrapper will be collected.
    // On the browser side, we ignore wrapper deletion events for named objects,
    // as they are only removed upon embedder's request (RemoveNamedObject).
    if (objects_.Lookup(iter->second.object_id)) {
      objects_.Remove(iter->second.object_id);
    }

    // We will always receive an allowlist of origins. Only inject
    // if the origin matches one of the rules.
    url::Origin security_origin = url::Origin(
        render_frame()->GetWebFrame()->GetDocument().GetSecurityOrigin());
    bool should_inject = iter->second.matcher.Matches(security_origin);

    if (should_inject) {
      GinJavaBridgeObject* object = GinJavaBridgeObject::InjectNamed(
          render_frame()->GetWebFrame(), weak_ptr_factory_.GetWeakPtr(),
          iter->first, iter->second.object_id);
      if (object) {
        objects_.AddWithID(object, iter->second.object_id);
      } else {
        GetRemoteObjectHost()->ObjectWrapperDeleted(iter->second.object_id);
      }
    }
  }
}

void GinJavaBridgeDispatcher::AddNamedObject(
    const std::string& name,
    ObjectID object_id,
    const origin_matcher::OriginMatcher& matcher) {
  // We should already have received the `remote_` via the SetHost method.
  CHECK(remote_);
  // Added objects only become available after page reload, so here they
  // are only added into the internal map.
  named_objects_.insert(std::make_pair(name, NamedObject{object_id, matcher}));
}

void GinJavaBridgeDispatcher::RemoveNamedObject(const std::string& name) {
  // Removal becomes in effect on next reload. We simply removing the entry
  // from the map here.
  DCHECK(base::Contains(named_objects_, name));
  named_objects_.erase(name);
}

void GinJavaBridgeDispatcher::SetHost(
    mojo::PendingRemote<mojom::GinJavaBridgeHost> host) {
  CHECK(!remote_);
  CHECK(named_objects_.empty());
  remote_.Bind(std::move(host));
}

GinJavaBridgeObject* GinJavaBridgeDispatcher::GetObject(ObjectID object_id) {
  GinJavaBridgeObject* result = objects_.Lookup(object_id);
  if (!result) {
    result = GinJavaBridgeObject::InjectAnonymous(
        render_frame()->GetWebFrame(), weak_ptr_factory_.GetWeakPtr(),
        object_id);
    if (result)
      objects_.AddWithID(result, object_id);
  }
  return result;
}

void GinJavaBridgeDispatcher::OnGinJavaBridgeObjectDeleted(
    GinJavaBridgeObject* object) {
  int object_id = object->object_id();
  // Ignore cleaning up of old object wrappers.
  if (objects_.Lookup(object_id) != object) return;
  objects_.Remove(object_id);

  GetRemoteObjectHost()->ObjectWrapperDeleted(object_id);
}

void GinJavaBridgeDispatcher::OnDestruct() {
  // This is a self owned receiver.
}

mojom::GinJavaBridgeHost* GinJavaBridgeDispatcher::GetRemoteObjectHost() {
  // Remote should always be sent because it is the first method sent to
  // this object.
  CHECK(remote_);
  return remote_.get();
}

}  // namespace content