<commit_before>// Copyright (c) 2010 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 "chrome/browser/geolocation/geolocation_permission_context.h"

#include "app/l10n_util.h"
#include "app/resource_bundle.h"
#include "chrome/browser/browser.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/extensions/extensions_service.h"
#include "chrome/browser/geolocation/geolocation_content_settings_map.h"
#include "chrome/browser/geolocation/geolocation_dispatcher_host.h"
#include "chrome/browser/geolocation/location_arbitrator.h"
#include "chrome/browser/pref_service.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/renderer_host/render_process_host.h"
#include "chrome/browser/renderer_host/render_view_host.h"
#include "chrome/browser/renderer_host/render_view_host_notification_task.h"
#include "chrome/browser/tab_contents/infobar_delegate.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/browser/tab_contents/tab_util.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/render_messages.h"
#include "grit/generated_resources.h"
#include "grit/locale_settings.h"
#include "grit/theme_resources.h"
#include "net/base/net_util.h"

// This class controls the geolocation infobar queue per profile, and it's an
// internal class to GeolocationPermissionContext.
// An alternate approach would be to have this queue per tab, and use
// notifications to broadcast when permission is set / listen to notification to
// cancel pending requests. This may be specially useful if there are other
// things listening for such notifications.
// For the time being this class is self-contained and it doesn't seem pulling
// the notification infrastructure would simplify.
class GeolocationInfoBarQueueController {
 public:
  GeolocationInfoBarQueueController(
      GeolocationPermissionContext* geolocation_permission_context,
      Profile* profile);
  ~GeolocationInfoBarQueueController();

  // The InfoBar will be displayed immediately if the tab is not already
  // displaying one, otherwise it'll be queued.
  void CreateInfoBarRequest(
      int render_process_id, int render_view_id, int bridge_id,
      const GURL& requesting_frame, const GURL& emebedder);

  // Cancels a specific infobar request.
  void CancelInfoBarRequest(
      int render_process_id, int render_view_id, int bridge_id);

  // Called by the InfoBarDelegate to notify it's closed. It'll display a new
  // InfoBar if there's any request pending for this tab.
  void OnInfoBarClosed(
      int render_process_id, int render_view_id, int bridge_id);

  // Called by the InfoBarDelegate to notify permission has been set.
  // It'll notify and dismiss any other pending InfoBar request for the same
  // |requesting_frame| and embedder.
  void OnPermissionSet(
      int render_process_id, int render_view_id, int bridge_id,
      const GURL& requesting_frame, const GURL& embedder, bool allowed);

 private:
  struct PendingInfoBarRequest;
  typedef std::vector<PendingInfoBarRequest> PendingInfoBarRequests;

  // Shows the first pending infobar for this tab.
  void ShowQueuedInfoBar(int render_process_id, int render_view_id);

  // Cancels an InfoBar request and returns the next iterator position.
  std::vector<PendingInfoBarRequest>::iterator CancelInfoBarRequestInternal(
      std::vector<PendingInfoBarRequest>::iterator i);

  GeolocationPermissionContext* const geolocation_permission_context_;
  Profile* const profile_;
  // Contains all pending infobar requests.
  PendingInfoBarRequests pending_infobar_requests_;
};

namespace {

// This is the delegate used to display the confirmation info bar.
class GeolocationConfirmInfoBarDelegate : public ConfirmInfoBarDelegate {
 public:
  GeolocationConfirmInfoBarDelegate(
      TabContents* tab_contents, GeolocationInfoBarQueueController* controller,
      int render_process_id, int render_view_id, int bridge_id,
      const GURL& requesting_frame_url,
      const std::wstring& display_languages)
      : ConfirmInfoBarDelegate(tab_contents),
        tab_contents_(tab_contents),
        controller_(controller),
        render_process_id_(render_process_id),
        render_view_id_(render_view_id),
        bridge_id_(bridge_id),
        requesting_frame_url_(requesting_frame_url),
        display_languages_(display_languages) {
  }

  // ConfirmInfoBarDelegate
  virtual void InfoBarClosed() {
    controller_->OnInfoBarClosed(render_process_id_, render_view_id_,
                                 bridge_id_);
    delete this;
  }
  virtual Type GetInfoBarType() { return INFO_TYPE; }
  virtual bool Accept() { return OnPermissionSet(true); }
  virtual bool Cancel() { return OnPermissionSet(false); }
  virtual int GetButtons() const { return BUTTON_OK | BUTTON_CANCEL; }
  virtual std::wstring GetButtonLabel(InfoBarButton button) const {
    switch (button) {
      case BUTTON_OK:
        return l10n_util::GetString(IDS_GEOLOCATION_ALLOW_BUTTON);
      case BUTTON_CANCEL:
        return l10n_util::GetString(IDS_GEOLOCATION_DENY_BUTTON);
      default:
        // All buttons are labeled above.
        NOTREACHED() << "Bad button id " << button;
        return L"";
    }
  }
  virtual std::wstring GetMessageText() const {
    return l10n_util::GetStringF(
        IDS_GEOLOCATION_INFOBAR_QUESTION,
        net::FormatUrl(requesting_frame_url_.GetOrigin(), display_languages_));
  }<commit_msg>geolocation: Fix the InfoBarType by changing it to PAGE_ACTION_TYPE.<commit_after>// Copyright (c) 2010 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 "chrome/browser/geolocation/geolocation_permission_context.h"

#include "app/l10n_util.h"
#include "app/resource_bundle.h"
#include "chrome/browser/browser.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/extensions/extensions_service.h"
#include "chrome/browser/geolocation/geolocation_content_settings_map.h"
#include "chrome/browser/geolocation/geolocation_dispatcher_host.h"
#include "chrome/browser/geolocation/location_arbitrator.h"
#include "chrome/browser/pref_service.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/renderer_host/render_process_host.h"
#include "chrome/browser/renderer_host/render_view_host.h"
#include "chrome/browser/renderer_host/render_view_host_notification_task.h"
#include "chrome/browser/tab_contents/infobar_delegate.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/browser/tab_contents/tab_util.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/render_messages.h"
#include "grit/generated_resources.h"
#include "grit/locale_settings.h"
#include "grit/theme_resources.h"
#include "net/base/net_util.h"

// This class controls the geolocation infobar queue per profile, and it's an
// internal class to GeolocationPermissionContext.
// An alternate approach would be to have this queue per tab, and use
// notifications to broadcast when permission is set / listen to notification to
// cancel pending requests. This may be specially useful if there are other
// things listening for such notifications.
// For the time being this class is self-contained and it doesn't seem pulling
// the notification infrastructure would simplify.
class GeolocationInfoBarQueueController {
 public:
  GeolocationInfoBarQueueController(
      GeolocationPermissionContext* geolocation_permission_context,
      Profile* profile);
  ~GeolocationInfoBarQueueController();

  // The InfoBar will be displayed immediately if the tab is not already
  // displaying one, otherwise it'll be queued.
  void CreateInfoBarRequest(
      int render_process_id, int render_view_id, int bridge_id,
      const GURL& requesting_frame, const GURL& emebedder);

  // Cancels a specific infobar request.
  void CancelInfoBarRequest(
      int render_process_id, int render_view_id, int bridge_id);

  // Called by the InfoBarDelegate to notify it's closed. It'll display a new
  // InfoBar if there's any request pending for this tab.
  void OnInfoBarClosed(
      int render_process_id, int render_view_id, int bridge_id);

  // Called by the InfoBarDelegate to notify permission has been set.
  // It'll notify and dismiss any other pending InfoBar request for the same
  // |requesting_frame| and embedder.
  void OnPermissionSet(
      int render_process_id, int render_view_id, int bridge_id,
      const GURL& requesting_frame, const GURL& embedder, bool allowed);

 private:
  struct PendingInfoBarRequest;
  typedef std::vector<PendingInfoBarRequest> PendingInfoBarRequests;

  // Shows the first pending infobar for this tab.
  void ShowQueuedInfoBar(int render_process_id, int render_view_id);

  // Cancels an InfoBar request and returns the next iterator position.
  std::vector<PendingInfoBarRequest>::iterator CancelInfoBarRequestInternal(
      std::vector<PendingInfoBarRequest>::iterator i);

  GeolocationPermissionContext* const geolocation_permission_context_;
  Profile* const profile_;
  // Contains all pending infobar requests.
  PendingInfoBarRequests pending_infobar_requests_;
};

namespace {

// This is the delegate used to display the confirmation info bar.
class GeolocationConfirmInfoBarDelegate : public ConfirmInfoBarDelegate {
 public:
  GeolocationConfirmInfoBarDelegate(
      TabContents* tab_contents, GeolocationInfoBarQueueController* controller,
      int render_process_id, int render_view_id, int bridge_id,
      const GURL& requesting_frame_url,
      const std::wstring& display_languages)
      : ConfirmInfoBarDelegate(tab_contents),
        tab_contents_(tab_contents),
        controller_(controller),
        render_process_id_(render_process_id),
        render_view_id_(render_view_id),
        bridge_id_(bridge_id),
        requesting_frame_url_(requesting_frame_url),
        display_languages_(display_languages) {
  }

  // ConfirmInfoBarDelegate
  virtual void InfoBarClosed() {
    controller_->OnInfoBarClosed(render_process_id_, render_view_id_,
                                 bridge_id_);
    delete this;
  }
  virtual Type GetInfoBarType() { return PAGE_ACTION_TYPE; }
  virtual bool Accept() { return OnPermissionSet(true); }
  virtual bool Cancel() { return OnPermissionSet(false); }
  virtual int GetButtons() const { return BUTTON_OK | BUTTON_CANCEL; }
  virtual std::wstring GetButtonLabel(InfoBarButton button) const {
    switch (button) {
      case BUTTON_OK:
        return l10n_util::GetString(IDS_GEOLOCATION_ALLOW_BUTTON);
      case BUTTON_CANCEL:
        return l10n_util::GetString(IDS_GEOLOCATION_DENY_BUTTON);
      default:
        // All buttons are labeled above.
        NOTREACHED() << "Bad button id " << button;
        return L"";
    }
  }
  virtual std::wstring GetMessageText() const {
    return l10n_util::GetStringF(
        IDS_GEOLOCATION_INFOBAR_QUESTION,
        net::FormatUrl(requesting_frame_url_.GetOrigin(), display_languages_));
  }
{'fraction_non_alphanumeric': 0.08035714285714286, 'fraction_numerical': 0.002142857142857143, 'mean_word_length': 4.606106106106106, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7658262', 'n_tokens_mistral': 3300, 'n_tokens_neox': 3226, 'n_words': 872}