1cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// found in the LICENSE file.
4cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
5cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#ifndef CONTENT_RENDERER_JAVA_GIN_JAVA_BRIDGE_DISPATCHER_H_
6cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#define CONTENT_RENDERER_JAVA_GIN_JAVA_BRIDGE_DISPATCHER_H_
7cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
8cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include <map>
9cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include <set>
10cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
11cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/id_map.h"
12cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
13cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/memory/weak_ptr.h"
14cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/values.h"
15116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "content/common/android/gin_java_bridge_errors.h"
16cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "content/public/renderer/render_frame_observer.h"
17cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
18cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)namespace blink {
19cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class WebFrame;
20cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
21cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)namespace content {
23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
24cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class GinJavaBridgeObject;
25cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
26cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// This class handles injecting Java objects into the main frame of a
27cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// RenderView. The 'add' and 'remove' messages received from the browser
28cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// process modify the entries in a map of 'pending' objects. These objects are
29cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// bound to the window object of the main frame when that window object is next
30cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// cleared. These objects remain bound until the window object is cleared
31cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// again.
32cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class GinJavaBridgeDispatcher
33cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    : public base::SupportsWeakPtr<GinJavaBridgeDispatcher>,
34cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      public RenderFrameObserver {
35cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) public:
36cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // GinJavaBridgeObjects are managed by gin. An object gets destroyed
37cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // when it is no more referenced from JS. As GinJavaBridgeObject reports
38cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // deletion of self to GinJavaBridgeDispatcher, we would not have stale
39cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // pointers here.
40cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  typedef IDMap<GinJavaBridgeObject, IDMapExternalPointer> ObjectMap;
41cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  typedef ObjectMap::KeyType ObjectID;
42cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
43cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  explicit GinJavaBridgeDispatcher(RenderFrame* render_frame);
44cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual ~GinJavaBridgeDispatcher();
45cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
46cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // RenderFrameObserver override:
47cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
48cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual void DidClearWindowObject() OVERRIDE;
49cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
50cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void GetJavaMethods(ObjectID object_id, std::set<std::string>* methods);
51cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  bool HasJavaMethod(ObjectID object_id, const std::string& method_name);
52cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  scoped_ptr<base::Value> InvokeJavaMethod(ObjectID object_id,
53cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                           const std::string& method_name,
54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                           const base::ListValue& arguments,
55116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                           GinJavaBridgeError* error);
56cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  GinJavaBridgeObject* GetObject(ObjectID object_id);
57cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void OnGinJavaBridgeObjectDeleted(ObjectID object_id);
58cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
59cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) private:
60cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void OnAddNamedObject(const std::string& name,
61cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                        ObjectID object_id);
62cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void OnRemoveNamedObject(const std::string& name);
63cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void OnSetAllowObjectContentsInspection(bool allow);
64cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
65cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  typedef std::map<std::string, ObjectID> NamedObjectMap;
66cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  NamedObjectMap named_objects_;
67cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ObjectMap objects_;
686d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  bool inside_did_clear_window_object_;
69cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
70cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(GinJavaBridgeDispatcher);
71cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)};
72cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
73cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}  // namespace content
74cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
75cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#endif  // CONTENT_RENDERER_JAVA_GIN_JAVA_BRIDGE_DISPATCHER_H_
76