<commit_before>// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/browser/guest_view/guest_view_manager.h"

#include "base/strings/stringprintf.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/user_metrics.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/common/result_codes.h"
#include "content/public/common/url_constants.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/guest_view/guest_view_base.h"
#include "extensions/browser/guest_view/guest_view_manager_factory.h"
#include "extensions/common/guest_view/guest_view_constants.h"
#include "net/base/escape.h"
#include "url/gurl.h"

using content::BrowserContext;
using content::SiteInstance;
using content::WebContents;

namespace extensions {

// static
GuestViewManagerFactory* GuestViewManager::factory_ = NULL;

GuestViewManager::GuestViewManager(content::BrowserContext* context)
    : current_instance_id_(0), last_instance_id_removed_(0), context_(context) {
}

GuestViewManager::~GuestViewManager() {}

// static.
GuestViewManager* GuestViewManager::FromBrowserContext(
    BrowserContext* context) {
  GuestViewManager* guest_manager =
      static_cast<GuestViewManager*>(context->GetUserData(
          guestview::kGuestViewManagerKeyName));
  if (!guest_manager) {
    if (factory_) {
      guest_manager = factory_->CreateGuestViewManager(context);
    } else {
      guest_manager = new GuestViewManager(context);
    }
    context->SetUserData(guestview::kGuestViewManagerKeyName, guest_manager);
  }
  return guest_manager;
}

content::WebContents* GuestViewManager::GetGuestByInstanceIDSafely(
    int guest_instance_id,
    int embedder_render_process_id) {
  if (!CanEmbedderAccessInstanceIDMaybeKill(embedder_render_process_id,
                                            guest_instance_id)) {
    return NULL;
  }
  return GetGuestByInstanceID(guest_instance_id);
}

void GuestViewManager::AttachGuest(
    int embedder_render_process_id,
    int embedder_routing_id,
    int element_instance_id,
    int guest_instance_id,
    const base::DictionaryValue& attach_params) {
  content::WebContents* guest_web_contents =
      GetGuestByInstanceIDSafely(guest_instance_id, embedder_render_process_id);
  if (!guest_web_contents)
    return;

  GuestViewBase* guest_view =
      GuestViewBase::FromWebContents(guest_web_contents);
  DCHECK(guest_view);

  content::RenderViewHost* rvh =
      content::RenderViewHost::FromID(embedder_render_process_id,
                                      embedder_routing_id);
  content::WebContents* embedder_web_contents =
      content::WebContents::FromRenderViewHost(rvh);
  if (!embedder_web_contents)
    return;
  ElementInstanceKey key(embedder_web_contents, element_instance_id);

  GuestInstanceIDMap::iterator it = instance_id_map_.find(key);
  if (it != instance_id_map_.end()) {
    int old_guest_instance_id = it->second;
    // Reattachment to the same guest is not currently supported.
    if (old_guest_instance_id == guest_instance_id)
      return;

    content::WebContents* old_guest_web_contents =
        GetGuestByInstanceIDSafely(old_guest_instance_id,
                                   embedder_render_process_id);
    if (!old_guest_web_contents)
      return;
<commit_msg>Fix null ptr dereference in webview.<commit_after>// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/browser/guest_view/guest_view_manager.h"

#include "base/strings/stringprintf.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/user_metrics.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/common/result_codes.h"
#include "content/public/common/url_constants.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/guest_view/guest_view_base.h"
#include "extensions/browser/guest_view/guest_view_manager_factory.h"
#include "extensions/common/guest_view/guest_view_constants.h"
#include "net/base/escape.h"
#include "url/gurl.h"

using content::BrowserContext;
using content::SiteInstance;
using content::WebContents;

namespace extensions {

// static
GuestViewManagerFactory* GuestViewManager::factory_ = NULL;

GuestViewManager::GuestViewManager(content::BrowserContext* context)
    : current_instance_id_(0), last_instance_id_removed_(0), context_(context) {
}

GuestViewManager::~GuestViewManager() {}

// static.
GuestViewManager* GuestViewManager::FromBrowserContext(
    BrowserContext* context) {
  GuestViewManager* guest_manager =
      static_cast<GuestViewManager*>(context->GetUserData(
          guestview::kGuestViewManagerKeyName));
  if (!guest_manager) {
    if (factory_) {
      guest_manager = factory_->CreateGuestViewManager(context);
    } else {
      guest_manager = new GuestViewManager(context);
    }
    context->SetUserData(guestview::kGuestViewManagerKeyName, guest_manager);
  }
  return guest_manager;
}

content::WebContents* GuestViewManager::GetGuestByInstanceIDSafely(
    int guest_instance_id,
    int embedder_render_process_id) {
  if (!CanEmbedderAccessInstanceIDMaybeKill(embedder_render_process_id,
                                            guest_instance_id)) {
    return NULL;
  }
  return GetGuestByInstanceID(guest_instance_id);
}

void GuestViewManager::AttachGuest(
    int embedder_render_process_id,
    int embedder_routing_id,
    int element_instance_id,
    int guest_instance_id,
    const base::DictionaryValue& attach_params) {
  content::WebContents* guest_web_contents =
      GetGuestByInstanceIDSafely(guest_instance_id, embedder_render_process_id);
  if (!guest_web_contents)
    return;

  GuestViewBase* guest_view =
      GuestViewBase::FromWebContents(guest_web_contents);
  DCHECK(guest_view);

  content::RenderViewHost* rvh =
      content::RenderViewHost::FromID(embedder_render_process_id,
                                      embedder_routing_id);
  // We need to check that rvh is not NULL because there may be a race between
  // AttachGuest and destroying the embedder (i.e. when the embedder is
  // destroyed immediately after the guest is created).
  if (!rvh)
    return;
  content::WebContents* embedder_web_contents =
      content::WebContents::FromRenderViewHost(rvh);
  if (!embedder_web_contents)
    return;
  ElementInstanceKey key(embedder_web_contents, element_instance_id);

  GuestInstanceIDMap::iterator it = instance_id_map_.find(key);
  if (it != instance_id_map_.end()) {
    int old_guest_instance_id = it->second;
    // Reattachment to the same guest is not currently supported.
    if (old_guest_instance_id == guest_instance_id)
      return;

    content::WebContents* old_guest_web_contents =
        GetGuestByInstanceIDSafely(old_guest_instance_id,
                                   embedder_render_process_id);
    if (!old_guest_web_contents)
      return;

{'fraction_non_alphanumeric': 0.09317443120260022, 'fraction_numerical': 0.0016251354279523294, 'mean_word_length': 4.69390902081727, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '929854', 'n_tokens_mistral': 2288, 'n_tokens_neox': 2228, 'n_words': 397}