<commit_before>// Copyright (c) 2016 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

#include "atom/renderer/content_settings_manager.h"

#include <string>
#include <vector>
#include "atom/common/api/api_messages.h"
#include "base/values.h"
#include "components/content_settings/core/common/content_settings_pattern.h"
#include "content/public/common/url_constants.h"
#include "content/public/renderer/render_thread.h"
#include "url/url_constants.h"
#include "url/gurl.h"

namespace atom {

ContentSettingsManager::ContentSettingsManager() {
  content::RenderThread::Get()->AddObserver(this);
}

ContentSettingsManager::~ContentSettingsManager() {
  if (content::RenderThread::Get())
    content::RenderThread::Get()->RemoveObserver(this);
}

// static
ContentSettingsManager* ContentSettingsManager::GetInstance() {
  static base::LazyInstance<ContentSettingsManager> manager =
    LAZY_INSTANCE_INITIALIZER;
  return manager.Pointer();
}

bool ContentSettingsManager::OnControlMessageReceived(
    const IPC::Message& message) {
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP(ContentSettingsManager, message)
    IPC_MESSAGE_HANDLER(AtomMsg_UpdateContentSettings, OnUpdateContentSettings)
    IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP()
  return handled;
}

void ContentSettingsManager::OnUpdateContentSettings(
    const base::DictionaryValue& content_settings) {
  content_settings_ = content_settings.CreateDeepCopy();
  FOR_EACH_OBSERVER(
    ContentSettingsObserver,
    observers_,
    OnContentSettingsChanged(content_settings));
}

void ContentSettingsManager::AddObserver(
    ContentSettingsObserver* observer) {
  observers_.AddObserver(observer);
}

void ContentSettingsManager::RemoveObserver(
    ContentSettingsObserver* observer) {
  observers_.RemoveObserver(observer);
}

ContentSetting ContentSettingsManager::GetSetting(
    GURL primary_url,
    GURL secondary_url,
    std::string content_type,
    bool incognito) {
  return GetContentSettingFromRules(primary_url,
                                    secondary_url,
                                    content_type,
                                    true);
}

std::vector<std::string> ContentSettingsManager::GetContentTypes() {
  std::vector<std::string> content_types;
  const base::DictionaryValue* settings = nullptr;
  content_settings_->GetAsDictionary(&settings);
  for (base::DictionaryValue::Iterator it(*settings);
      !it.IsAtEnd();
      it.Advance()) {
    content_types.push_back(it.key());
  }
  return content_types;
}

ContentSetting ContentSettingsManager::GetContentSettingFromRules(
    const GURL& primary_url,
    const GURL& secondary_url,
    const std::string& content_type,
    const bool& default_value) {
  ContentSetting result = default_value
    ? ContentSetting::CONTENT_SETTING_ALLOW
    : ContentSetting::CONTENT_SETTING_BLOCK;

  const base::ListValue* rules = nullptr;
  if (!content_settings_->GetList(content_type, &rules))
    return result;

  // all rules are evaluated in order and the
  // most specific matching rule will apply
  for (base::ListValue::const_iterator it = rules->begin();
       it != rules->end(); ++it) {<commit_msg>Block active mixed content by default in content settings<commit_after>// Copyright (c) 2016 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

#include "atom/renderer/content_settings_manager.h"

#include <string>
#include <vector>
#include "atom/common/api/api_messages.h"
#include "base/values.h"
#include "components/content_settings/core/common/content_settings_pattern.h"
#include "content/public/common/url_constants.h"
#include "content/public/renderer/render_thread.h"
#include "url/url_constants.h"
#include "url/gurl.h"

namespace atom {

ContentSettingsManager::ContentSettingsManager() {
  content::RenderThread::Get()->AddObserver(this);
}

ContentSettingsManager::~ContentSettingsManager() {
  if (content::RenderThread::Get())
    content::RenderThread::Get()->RemoveObserver(this);
}

// static
ContentSettingsManager* ContentSettingsManager::GetInstance() {
  static base::LazyInstance<ContentSettingsManager> manager =
    LAZY_INSTANCE_INITIALIZER;
  return manager.Pointer();
}

bool ContentSettingsManager::OnControlMessageReceived(
    const IPC::Message& message) {
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP(ContentSettingsManager, message)
    IPC_MESSAGE_HANDLER(AtomMsg_UpdateContentSettings, OnUpdateContentSettings)
    IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP()
  return handled;
}

void ContentSettingsManager::OnUpdateContentSettings(
    const base::DictionaryValue& content_settings) {
  content_settings_ = content_settings.CreateDeepCopy();
  FOR_EACH_OBSERVER(
    ContentSettingsObserver,
    observers_,
    OnContentSettingsChanged(content_settings));
}

void ContentSettingsManager::AddObserver(
    ContentSettingsObserver* observer) {
  observers_.AddObserver(observer);
}

void ContentSettingsManager::RemoveObserver(
    ContentSettingsObserver* observer) {
  observers_.RemoveObserver(observer);
}

ContentSetting ContentSettingsManager::GetSetting(
    GURL primary_url,
    GURL secondary_url,
    std::string content_type,
    bool incognito) {
  return GetContentSettingFromRules(primary_url,
                                    secondary_url,
                                    content_type,
                                    content_type != "runInsecureContent"
                                      ? true : false);
}

std::vector<std::string> ContentSettingsManager::GetContentTypes() {
  std::vector<std::string> content_types;
  const base::DictionaryValue* settings = nullptr;
  content_settings_->GetAsDictionary(&settings);
  for (base::DictionaryValue::Iterator it(*settings);
      !it.IsAtEnd();
      it.Advance()) {
    content_types.push_back(it.key());
  }
  return content_types;
}

ContentSetting ContentSettingsManager::GetContentSettingFromRules(
    const GURL& primary_url,
    const GURL& secondary_url,
    const std::string& content_type,
    const bool& default_value) {
  ContentSetting result = default_value
    ? ContentSetting::CONTENT_SETTING_ALLOW
    : ContentSetting::CONTENT_SETTING_BLOCK;

  const base::ListValue* rules = nullptr;
  if (!content_settings_->GetList(content_type, &rules))
    return result;

  // all rules are evaluated in order and the
  // most specific matching rule will apply
  for (base::ListValue::const_iterator it = rules->begin();
       it != rules->end(); ++it) {
{'fraction_non_alphanumeric': 0.10282776349614396, 'fraction_numerical': 0.0012097383940722819, 'mean_word_length': 4.667523564695801, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 25, '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': '4614029', 'n_tokens_mistral': 1866, 'n_tokens_neox': 1864, 'n_words': 373}