]> khamidou — GitHub mirrors - electron.git/commitdiff
Use element instant id for GetGuestByInstanceID
authorCheng Zhao <zcbenz@gmail.com>
Tue, 9 Dec 2014 22:38:43 +0000 (14:38 -0800)
committerCheng Zhao <zcbenz@gmail.com>
Tue, 9 Dec 2014 22:38:43 +0000 (14:38 -0800)
atom/browser/api/atom_api_web_contents.cc
atom/browser/api/atom_api_web_contents.h
atom/browser/lib/guest-view-manager.coffee
atom/browser/web_view/web_view_manager.cc
atom/browser/web_view/web_view_manager.h
atom/renderer/lib/web-view/web-view.coffee

index 19b6d3276a270f15e3d0ee6f3859a067504faeab..7bd1095b8a9ff53bc48410556967618dc846f83c 100644 (file)
@@ -12,6 +12,7 @@
 #include "atom/common/native_mate_converters/value_converter.h"
 #include "base/strings/utf_string_conversions.h"
 #include "brightray/browser/inspectable_web_contents.h"
+#include "content/public/browser/navigation_details.h"
 #include "content/public/browser/render_frame_host.h"
 #include "content/public/browser/render_process_host.h"
 #include "content/public/browser/render_view_host.h"
@@ -190,6 +191,13 @@ void WebContents::DidGetRedirectForResourceRequest(
   Emit("did-get-redirect-request", args);
 }
 
+void WebContents::DidNavigateMainFrame(
+    const content::LoadCommittedDetails& details,
+    const content::FrameNavigateParams& params) {
+  if (details.is_navigation_to_different_page())
+    Emit("did-navigate-to-different-page");
+}
+
 bool WebContents::OnMessageReceived(const IPC::Message& message) {
   bool handled = true;
   IPC_BEGIN_MESSAGE_MAP(WebContents, message)
index 8371a65560b83cfc9f7ca53bb550660dadb29964..4bb798c90def66e99de454a44aa735f685419d6e 100644 (file)
@@ -118,24 +118,25 @@ class WebContents : public mate::EventEmitter,
       const content::NativeWebKeyboardEvent& event) override;
 
   // content::WebContentsObserver:
-  virtual void RenderViewDeleted(content::RenderViewHost*) override;
-  virtual void RenderProcessGone(base::TerminationStatus status) override;
-  virtual void DidFinishLoad(content::RenderFrameHost* render_frame_host,
-                             const GURL& validated_url) override;
-  virtual void DidFailLoad(content::RenderFrameHost* render_frame_host,
-                           const GURL& validated_url,
-                           int error_code,
-                           const base::string16& error_description) override;
-  virtual void DidStartLoading(
-      content::RenderViewHost* render_view_host) override;
-  virtual void DidStopLoading(
-      content::RenderViewHost* render_view_host) override;
-  virtual void DidGetRedirectForResourceRequest(
+  void RenderViewDeleted(content::RenderViewHost*) override;
+  void RenderProcessGone(base::TerminationStatus status) override;
+  void DidFinishLoad(content::RenderFrameHost* render_frame_host,
+                     const GURL& validated_url) override;
+  void DidFailLoad(content::RenderFrameHost* render_frame_host,
+                   const GURL& validated_url,
+                   int error_code,
+                   const base::string16& error_description) override;
+  void DidStartLoading(content::RenderViewHost* render_view_host) override;
+  void DidStopLoading(content::RenderViewHost* render_view_host) override;
+  void DidGetRedirectForResourceRequest(
       content::RenderViewHost* render_view_host,
       const content::ResourceRedirectDetails& details) override;
-  virtual bool OnMessageReceived(const IPC::Message& message) override;
-  virtual void RenderViewReady() override;
-  virtual void WebContentsDestroyed() override;
+  void DidNavigateMainFrame(
+      const content::LoadCommittedDetails& details,
+      const content::FrameNavigateParams& params) override;
+  bool OnMessageReceived(const IPC::Message& message) override;
+  void RenderViewReady() override;
+  void WebContentsDestroyed() override;
 
   // content::BrowserPluginGuestDelegate:
   void DidAttach(int guest_proxy_routing_id) final;
index 4f2af53f5765257a4e94424badc58150d8d383d6..eef36f1fa20c4211b6b7e461e76722547b67225e 100644 (file)
@@ -35,12 +35,13 @@ createGuest = (embedder, params) ->
     guestInstanceId: id
     storagePartitionId: params.storagePartitionId
   guestInstances[id] = {guest, embedder}
-  preload = params.preload ? ''
-  webViewManager.addGuest id, embedder, guest, params.nodeIntegration, params.plugins, preload
 
-  # Destroy guest when the embedder is gone.
-  embedder.once 'render-view-deleted', ->
+  # Destroy guest when the embedder is gone or navigated.
+  destroyEvents = ['destroyed', 'crashed', 'did-navigate-to-different-page']
+  destroy = ->
     destroyGuest id if guestInstances[id]?
+    embedder.removeListener event, destroy for event in destroyEvents
+  embedder.once event, destroy for event in destroyEvents
 
   # Init guest web view after attached.
   guest.once 'did-attach', ->
@@ -85,6 +86,11 @@ attachGuest = (embedder, elementInstanceId, guestInstanceId, params) ->
     return unless guestInstances[oldGuestInstanceId]?
     destroyGuest oldGuestInstanceId
 
+  webViewManager.addGuest guestInstanceId, elementInstanceId, embedder, guest,
+    nodeIntegration: params.nodeIntegration
+    plugins: params.plugins
+    preloadUrl: params.preload ? ''
+
   guest.attachParams = params
   embedderElementsMap[key] = guestInstanceId
   reverseEmbedderElementsMap[guestInstanceId] = key
index bc308debad88d8f6710da704732976d0914295fa..032be04e78205f6c9e69f6325f0bd9744fffddad 100644 (file)
@@ -32,6 +32,19 @@ struct Converter<content::WebContents*> {
   }
 };
 
+template<>
+struct Converter<atom::WebViewManager::WebViewOptions> {
+  static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val,
+                     atom::WebViewManager::WebViewOptions* out) {
+    Dictionary options;
+    if (!ConvertFromV8(isolate, val, &options))
+      return false;
+    return options.Get("nodeIntegration", &(out->node_integration)) &&
+           options.Get("plugins", &(out->plugins)) &&
+           options.Get("preloadUrl", &(out->preload_url));
+  }
+};
+
 }  // namespace mate
 
 namespace atom {
@@ -43,17 +56,16 @@ WebViewManager::~WebViewManager() {
 }
 
 void WebViewManager::AddGuest(int guest_instance_id,
+                              int element_instance_id,
                               content::WebContents* embedder,
                               content::WebContents* web_contents,
-                              bool node_integration,
-                              bool plugins,
-                              const GURL& preload_url) {
+                              const WebViewOptions& options) {
   web_contents_map_[guest_instance_id] = { web_contents, embedder };
 
   WebViewRendererState::WebViewInfo web_view_info = {
-    guest_instance_id, node_integration, plugins
+    guest_instance_id, options.node_integration, options.plugins
   };
-  net::FileURLToFilePath(preload_url, &web_view_info.preload_script);
+  net::FileURLToFilePath(options.preload_url, &web_view_info.preload_script);
   content::BrowserThread::PostTask(
       content::BrowserThread::IO,
       FROM_HERE,
@@ -61,6 +73,10 @@ void WebViewManager::AddGuest(int guest_instance_id,
                  base::Unretained(WebViewRendererState::GetInstance()),
                  web_contents->GetRenderProcessHost()->GetID(),
                  web_view_info));
+
+  // Map the element in embedder to guest.
+  ElementInstanceKey key(embedder, element_instance_id);
+  element_instance_id_to_guest_map_[key] = guest_instance_id;
 }
 
 void WebViewManager::RemoveGuest(int guest_instance_id) {
@@ -71,13 +87,24 @@ void WebViewManager::RemoveGuest(int guest_instance_id) {
           &WebViewRendererState::RemoveGuest,
           base::Unretained(WebViewRendererState::GetInstance()),
           web_contents->GetRenderProcessHost()->GetID()));
-
   web_contents_map_.erase(guest_instance_id);
+
+  // Remove the record of element in embedder too.
+  for (const auto& element : element_instance_id_to_guest_map_)
+    if (element.second == guest_instance_id) {
+      element_instance_id_to_guest_map_.erase(element.first);
+      break;
+    }
 }
 
 content::WebContents* WebViewManager::GetGuestByInstanceID(
-    content::WebContents* embedder_web_contents,
-    int guest_instance_id) {
+    content::WebContents* embedder,
+    int element_instance_id) {
+  ElementInstanceKey key(embedder, element_instance_id);
+  if (!ContainsKey(element_instance_id_to_guest_map_, key))
+    return nullptr;
+
+  int guest_instance_id = element_instance_id_to_guest_map_[key];
   if (ContainsKey(web_contents_map_, guest_instance_id))
     return web_contents_map_[guest_instance_id].web_contents;
   else
index fb118251e6bc28db9019a01f0db7a61aa6f8a84e..e14d31cec0567a3c8a1287f5cb9ae51c1d0bfe4f 100644 (file)
@@ -21,20 +21,25 @@ class WebViewManager : public content::BrowserPluginGuestManager {
   explicit WebViewManager(content::BrowserContext* context);
   virtual ~WebViewManager();
 
+  struct WebViewOptions {
+    bool node_integration;
+    bool plugins;
+    GURL preload_url;
+  };
+
   void AddGuest(int guest_instance_id,
+                int element_instance_id,
                 content::WebContents* embedder,
                 content::WebContents* web_contents,
-                bool node_integration,
-                bool plugins,
-                const GURL& preload_url);
+                const WebViewOptions& options);
   void RemoveGuest(int guest_instance_id);
 
  protected:
   // content::BrowserPluginGuestManager:
   content::WebContents* GetGuestByInstanceID(
       content::WebContents* embedder_web_contents,
-      int browser_plugin_instance_id) override;
-  bool ForEachGuest(content::WebContents* embedder_web_contents,
+      int element_instance_id) override;
+  bool ForEachGuest(content::WebContents* embedder,
                     const GuestCallback& callback) override;
 
  private:
@@ -44,6 +49,32 @@ class WebViewManager : public content::BrowserPluginGuestManager {
   };
   std::map<int, WebContentsWithEmbedder> web_contents_map_;
 
+  struct ElementInstanceKey {
+    content::WebContents* owner_web_contents;
+    int element_instance_id;
+
+    ElementInstanceKey()
+        : owner_web_contents(nullptr),
+          element_instance_id(0) {}
+
+    ElementInstanceKey(content::WebContents* owner_web_contents,
+                       int element_instance_id)
+        : owner_web_contents(owner_web_contents),
+          element_instance_id(element_instance_id) {}
+
+    bool operator<(const ElementInstanceKey& other) const {
+      if (owner_web_contents != other.owner_web_contents)
+        return owner_web_contents < other.owner_web_contents;
+      return element_instance_id < other.element_instance_id;
+    }
+
+    bool operator==(const ElementInstanceKey& other) const {
+      return (owner_web_contents == other.owner_web_contents) &&
+          (element_instance_id == other.element_instance_id);
+    }
+  };
+  std::map<ElementInstanceKey, int> element_instance_id_to_guest_map_;
+
   DISALLOW_COPY_AND_ASSIGN(WebViewManager);
 };
 
index 1ad9ba0f666ba2cbba22e10e376496e2cb6569f2..a68f8e728d84fd5d0f746cb48e1c478e7ddde70a 100644 (file)
@@ -138,19 +138,6 @@ class WebViewImpl
     return if @pendingGuestCreation
     params =
       storagePartitionId: @attributes[webViewConstants.ATTRIBUTE_PARTITION].getValue()
-      nodeIntegration: @webviewNode.hasAttribute webViewConstants.ATTRIBUTE_NODEINTEGRATION
-      plugins: @webviewNode.hasAttribute webViewConstants.ATTRIBUTE_PLUGINS
-    if @webviewNode.hasAttribute webViewConstants.ATTRIBUTE_PRELOAD
-      preload = @webviewNode.getAttribute webViewConstants.ATTRIBUTE_PRELOAD
-      # Get the full path.
-      a = document.createElement 'a'
-      a.href = preload
-      params.preload = a.href
-      # Only support file: or asar: protocol.
-      protocol = params.preload.substr 0, 5
-      unless protocol in ['file:', 'asar:']
-        delete params.preload
-        console.error webViewConstants.ERROR_MSG_INVALID_PRELOAD_ATTRIBUTE
     guestViewInternal.createGuest 'webview', params, (guestInstanceId) =>
       @pendingGuestCreation = false
       unless @elementAttached
@@ -193,8 +180,21 @@ class WebViewImpl
     params =
       instanceId: @viewInstanceId
       userAgentOverride: @userAgentOverride
+      nodeIntegration: @webviewNode.hasAttribute webViewConstants.ATTRIBUTE_NODEINTEGRATION
+      plugins: @webviewNode.hasAttribute webViewConstants.ATTRIBUTE_PLUGINS
     for attributeName, attribute of @attributes
       params[attributeName] = attribute.getValue()
+    if @webviewNode.hasAttribute webViewConstants.ATTRIBUTE_PRELOAD
+      preload = @webviewNode.getAttribute webViewConstants.ATTRIBUTE_PRELOAD
+      # Get the full path.
+      a = document.createElement 'a'
+      a.href = preload
+      params.preload = a.href
+      # Only support file: or asar: protocol.
+      protocol = params.preload.substr 0, 5
+      unless protocol in ['file:', 'asar:']
+        delete params.preload
+        console.error webViewConstants.ERROR_MSG_INVALID_PRELOAD_ATTRIBUTE
     params
 
   attachWindow: (guestInstanceId) ->