<commit_before>// Copyright (c) 2013 Intel Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "xwalk/application/browser/application_tizen.h"

#include <set>
#include <string>
#include <vector>

#include "content/public/browser/web_contents.h"
#include "content/public/browser/render_process_host.h"

#include "xwalk/runtime/browser/ui/native_app_window.h"
#include "xwalk/runtime/common/xwalk_common_messages.h"

#if defined(USE_OZONE)
#include "base/message_loop/message_pump_ozone.h"
#include "content/public/browser/render_view_host.h"
#include "ui/events/event.h"
#include "ui/events/event_constants.h"
#include "ui/events/keycodes/keyboard_codes_posix.h"
#include "xwalk/application/common/manifest_handlers/tizen_setting_handler.h"
#endif

#include "xwalk/application/common/application_manifest_constants.h"
#include "xwalk/application/common/manifest_handlers/navigation_handler.h"

namespace xwalk {

namespace widget_keys = application_widget_keys;

namespace application {

namespace {
const char kAsterisk[] = "*";
}  // namespace


ApplicationTizen::ApplicationTizen(
    scoped_refptr<ApplicationData> data,
    RuntimeContext* runtime_context,
    Application::Observer* observer)
    : Application(data, runtime_context, observer) {
#if defined(USE_OZONE)
  base::MessagePumpOzone::Current()->AddObserver(this);
#endif
}

ApplicationTizen::~ApplicationTizen() {
#if defined(USE_OZONE)
  base::MessagePumpOzone::Current()->RemoveObserver(this);
#endif
}

void ApplicationTizen::Hide() {
  DCHECK(runtimes_.size());
  std::set<Runtime*>::iterator it = runtimes_.begin();
  for (; it != runtimes_.end(); ++it) {
    if ((*it)->window())
      (*it)->window()->Hide();
  }
}

void ApplicationTizen::InitSecurityPolicy() {
  // On Tizen, CSP mode has higher priority, and WARP will be disabled
  // if the application is under CSP mode.
  if (!data_->HasCSPDefined()) {
    Application::InitSecurityPolicy();
    return;
  }

  if (data_->GetPackageType() != Package::WGT)
    return;

  // Always enable security mode when under CSP mode.
  security_mode_enabled_ = true;
  NavigationInfo* info = static_cast<NavigationInfo*>(
      data_->GetManifestData(widget_keys::kAllowNavigationKey));
  if (info) {
    const std::vector<std::string>& allowed_list = info->GetAllowedDomains();
    for (std::vector<std::string>::const_iterator it = allowed_list.begin();
         it != allowed_list.end(); ++it) {
      // If the policy is "*", it represents that any external link is allowed
      // to navigate to.
      if ((*it) == kAsterisk) {
        security_mode_enabled_ = false;
        return;
      }

      // If the policy start with "*.", like this: *.domain,
      // means that can access to all subdomains for 'domain',
      // otherwise, the host of request url should exactly the same
      // as policy.
      bool subdomains = ((*it).find("*.") == 0);<commit_msg>[Tizen][Runtime] Add default csp support for tizen.<commit_after>// Copyright (c) 2013 Intel Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "xwalk/application/browser/application_tizen.h"

#include <set>
#include <string>
#include <vector>

#include "content/public/browser/web_contents.h"
#include "content/public/browser/render_process_host.h"

#include "xwalk/runtime/browser/ui/native_app_window.h"
#include "xwalk/runtime/common/xwalk_common_messages.h"

#if defined(USE_OZONE)
#include "base/message_loop/message_pump_ozone.h"
#include "content/public/browser/render_view_host.h"
#include "ui/events/event.h"
#include "ui/events/event_constants.h"
#include "ui/events/keycodes/keyboard_codes_posix.h"
#include "xwalk/application/common/manifest_handlers/tizen_setting_handler.h"
#endif

#include "xwalk/application/common/application_manifest_constants.h"
#include "xwalk/application/common/manifest_handlers/csp_handler.h"
#include "xwalk/application/common/manifest_handlers/navigation_handler.h"

namespace xwalk {

namespace widget_keys = application_widget_keys;

namespace application {

namespace {
const char kAsterisk[] = "*";

const char kDirectiveValueSelf[] = "'self'";
const char kDirectiveValueNone[] = "'none'";

const char kDirectiveNameDefault[] = "default-src";
const char kDirectiveNameScript[] = "script-src";
const char kDirectiveNameStyle[] = "style-src";
const char kDirectiveNameObject[] = "object-src";

CSPInfo* GetDefaultCSPInfo() {
  static CSPInfo default_csp_info;
  if (default_csp_info.GetDirectives().empty()) {
    std::vector<std::string> directive_all;
    std::vector<std::string> directive_self;
    std::vector<std::string> directive_none;
    directive_all.push_back(kAsterisk);
    directive_self.push_back(kDirectiveValueSelf);
    directive_none.push_back(kDirectiveValueNone);

    default_csp_info.SetDirective(kDirectiveNameDefault, directive_all);
    default_csp_info.SetDirective(kDirectiveNameScript, directive_self);
    default_csp_info.SetDirective(kDirectiveNameStyle, directive_self);
    default_csp_info.SetDirective(kDirectiveNameObject, directive_none);
  }

  return (new CSPInfo(default_csp_info));
}

}  // namespace


ApplicationTizen::ApplicationTizen(
    scoped_refptr<ApplicationData> data,
    RuntimeContext* runtime_context,
    Application::Observer* observer)
    : Application(data, runtime_context, observer) {
#if defined(USE_OZONE)
  base::MessagePumpOzone::Current()->AddObserver(this);
#endif
}

ApplicationTizen::~ApplicationTizen() {
#if defined(USE_OZONE)
  base::MessagePumpOzone::Current()->RemoveObserver(this);
#endif
}

void ApplicationTizen::Hide() {
  DCHECK(runtimes_.size());
  std::set<Runtime*>::iterator it = runtimes_.begin();
  for (; it != runtimes_.end(); ++it) {
    if ((*it)->window())
      (*it)->window()->Hide();
  }
}

void ApplicationTizen::InitSecurityPolicy() {
  // On Tizen, CSP mode has higher priority, and WARP will be disabled
  // if the application is under CSP mode.
  if (!data_->HasCSPDefined()) {
    Application::InitSecurityPolicy();
    return;
  }

  if (data_->GetPackageType() != Package::WGT)
    return;

  CSPInfo* csp_info =
      static_cast<CSPInfo*>(data_->GetManifestData(widget_keys::kCSPKey));
  if (!csp_info || csp_info->GetDirectives().empty())
    data_->SetManifestData(widget_keys::kCSPKey, GetDefaultCSPInfo());

  // Always enable security mode when under CSP mode.
  security_mode_enabled_ = true;
  NavigationInfo* info = static_cast<NavigationInfo*>(
      data_->GetManifestData(widget_keys::kAllowNavigationKey));
  if (info) {
    const std::vector<std::string>& allowed_list = info->GetAllowedDomains();
    for (std::vector<std::string>::const_iterator it = allowed_list.begin();
         it != allowed_list.end(); ++it) {
      // If the policy is "*", it represents that any external link is allowed
      // to navigate to.
      if ((*it) == kAsterisk) {
        security_mode_enabled_ = false;
        return;
      }

      // If the policy start with "*.", like this: *.domain,
      // means that can access to all subdomains for 'domain',
      // otherwise, the host of request url should exactly the same
      // as policy.
      bool subdomains = ((*it).find("*.") == 0);
{'fraction_non_alphanumeric': 0.1330150068212824, 'fraction_numerical': 0.001364256480218281, 'mean_word_length': 4.763364779874214, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6027023', 'n_tokens_mistral': 2335, 'n_tokens_neox': 2300, 'n_words': 500}