<commit_before>// Copyright (c) 2012 Intel Corp
// Copyright (c) 2012 The Chromium Authors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell co
// pies of the Software, and to permit persons to whom the Software is furnished
//  to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in al
// l copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IM
// PLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNES
// S FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
//  OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WH
// ETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
//  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#define V8_USE_UNSAFE_HANDLES

#include "content/nw/src/renderer/shell_content_renderer_client.h"

#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/posix/global_descriptors.h"
#include "base/strings/string16.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/content/renderer/page_click_tracker.h"
#include "components/autofill/content/renderer/autofill_agent.h"
#include "components/autofill/content/renderer/password_autofill_agent.h"
#include "components/autofill/content/renderer/password_generation_agent.h"
#include "content/common/view_messages.h"
#include "content/nw/src/api/dispatcher.h"
#include "content/nw/src/api/api_messages.h"
#include "content/nw/src/api/window_bindings.h"
#include "content/nw/src/common/shell_switches.h"
#include "content/nw/src/nw_package.h"
#include "content/nw/src/nw_version.h"
#include "components/autofill/content/renderer/autofill_agent.h"
#include "components/autofill/content/renderer/password_autofill_agent.h"
#include "content/nw/src/renderer/nw_render_view_observer.h"
#include "content/nw/src/renderer/prerenderer/prerenderer_client.h"
#include "content/nw/src/renderer/printing/print_web_view_helper.h"
#include "content/nw/src/renderer/shell_render_process_observer.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/content_constants.h"
#include "content/public/renderer/render_frame_observer_tracker.h"
#include "content/public/renderer/render_thread.h"
#include "content/public/renderer/render_view.h"
#include "content/renderer/render_view_impl.h"
#include "ipc/ipc_descriptors.h"
#include "net/proxy/proxy_bypass_rules.h"
#include "third_party/node/src/node.h"
#undef CHECK
#include "third_party/node/src/node_internals.h"
#include "third_party/node/src/req_wrap.h"
#include "third_party/WebKit/public/web/WebDocument.h"
#include "third_party/WebKit/public/web/WebLocalFrame.h"
#include "third_party/WebKit/public/web/WebSecurityOrigin.h"
#include "third_party/WebKit/public/web/WebSecurityPolicy.h"
#include "third_party/WebKit/public/web/WebView.h"
//#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
#include "content/common/dom_storage/dom_storage_map.h"

#include "nw/id/commit.h"

#include "extensions/common/extensions_client.h"
#include "extensions/common/switches.h"
#include "extensions/renderer/default_dispatcher_delegate.h"
#include "extensions/renderer/dispatcher.h"
#include "extensions/renderer/extension_helper.h"
#include "extensions/renderer/guest_view/extensions_guest_view_container.h"
#include "extensions/renderer/guest_view/mime_handler_view/mime_handler_view_container.h"
#include "content/nw/src/common/shell_extensions_client.h"
#include "content/nw/src/renderer/shell_extensions_renderer_client.h"


using content::RenderView;
using content::RenderViewImpl;
using autofill::AutofillAgent;
using autofill::PasswordAutofillAgent;
using autofill::PasswordGenerationAgent;
using net::ProxyBypassRules;
using blink::WebFrame;
using blink::WebLocalFrame;
using blink::WebView;
using blink::WebString;
using blink::WebSecurityPolicy;
using extensions::ExtensionsClient;

namespace content {

namespace {

// TODO: promote ExtensionFrameHelper to a common place and share with this.
class ShellFrameHelper
    : public content::RenderFrameObserver,
      public content::RenderFrameObserverTracker<ShellFrameHelper> {
 public:
  ShellFrameHelper(content::RenderFrame* render_frame,
                   extensions::Dispatcher* extension_dispatcher);
  ~ShellFrameHelper() override;

  // RenderFrameObserver implementation.
  void WillReleaseScriptContext(v8::Handle<v8::Context>, int world_id) override;

 private:
  extensions::Dispatcher* extension_dispatcher_;

  DISALLOW_COPY_AND_ASSIGN(ShellFrameHelper);
};

ShellFrameHelper::ShellFrameHelper(content::RenderFrame* render_frame,
                                   extensions::Dispatcher* extension_dispatcher)
    : content::RenderFrameObserver(render_frame),
      content::RenderFrameObserverTracker<ShellFrameHelper>(render_frame),
      extension_dispatcher_(extension_dispatcher) {
}

ShellFrameHelper::~ShellFrameHelper() {
}

void ShellFrameHelper::WillReleaseScriptContext(v8::Handle<v8::Context> context,
                                                int world_id) {
  extension_dispatcher_->WillReleaseScriptContext(
      render_frame()->GetWebFrame(), context, world_id);
}

RenderView* GetCurrentRenderView() {
  WebLocalFrame* frame = WebLocalFrame::frameForCurrentContext();
  DCHECK(frame);
  if (!frame)
    return NULL;

  WebView* view = frame->view();
  if (!view)
    return NULL;  // can happen during closing.

  RenderView* render_view = RenderView::FromWebView(view);
  DCHECK(render_view);
  return render_view;
}

}  // namespace

ShellContentRendererClient::ShellContentRendererClient()
  :in_nav_cb_(false), creating_first_context_(true) {
}

ShellContentRendererClient::~ShellContentRendererClient() {
}

void ShellContentRendererClient::RenderThreadStarted() {
  content::RenderThread* thread = content::RenderThread::Get();

  // Change working directory.
  CommandLine* command_line = CommandLine::ForCurrentProcess();
  if (command_line->HasSwitch(switches::kWorkingDirectory)) {
    base::SetCurrentDirectory(
        command_line->GetSwitchValuePath(switches::kWorkingDirectory));
  }

  if (command_line->HasSwitch(extensions::switches::kExtensionProcess)) {
    extensions_client_.reset(CreateExtensionsClient());
    extensions::ExtensionsClient::Set(extensions_client_.get());

    extensions_renderer_client_.reset(new extensions::ShellExtensionsRendererClient);
    extensions::ExtensionsRendererClient::Set(extensions_renderer_client_.get());

    extension_dispatcher_delegate_.reset(new extensions::DefaultDispatcherDelegate());

    // Must be initialized after ExtensionsRendererClient.
    extension_dispatcher_.reset(
                                new extensions::Dispatcher(extension_dispatcher_delegate_.get()));
    thread->AddObserver(extension_dispatcher_.get());
  }

#if 0
  if (command_line->HasSwitch(switches::kDomStorageQuota)) {
    std::string quota_str = command_line->GetSwitchValueASCII(switches::kDomStorageQuota);
    int quota = 0;
    if (base::StringToInt(quota_str, &quota) && quota > 0) {
      content::DOMStorageMap::SetQuotaOverride(quota * 1024 * 1024);
    }
  }
#endif //0

  // Start observers.
  shell_observer_.reset(new ShellRenderProcessObserver());

  WebString file_scheme(base::ASCIIToUTF16("file"));
  WebString app_scheme(base::ASCIIToUTF16("app"));
  // file: resources should be allowed to receive CORS requests.
  WebSecurityPolicy::registerURLSchemeAsCORSEnabled(file_scheme);
  WebSecurityPolicy::registerURLSchemeAsCORSEnabled(app_scheme);

}

void ShellContentRendererClient::RenderViewCreated(RenderView* render_view) {
  new nwapi::Dispatcher(render_view);
  new nw::NwRenderViewObserver(render_view);
  new prerender::PrerendererClient(render_view);
#if defined(ENABLE_PRINTING)
  new printing::PrintWebViewHelper(render_view);
#endif

  if (extension_dispatcher_.get()) {
    new extensions::ExtensionHelper(render_view, extension_dispatcher_.get());
    extension_dispatcher_->OnRenderViewCreated(render_view);
  }

  PasswordGenerationAgent* password_generation_agent =
      new PasswordGenerationAgent(render_view);
  PasswordAutofillAgent* password_autofill_agent =
      new PasswordAutofillAgent(render_view);
  new AutofillAgent(render_view,
                    password_autofill_agent,<commit_msg>Fix #2854: dom_storage_quota<commit_after>// Copyright (c) 2012 Intel Corp
// Copyright (c) 2012 The Chromium Authors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell co
// pies of the Software, and to permit persons to whom the Software is furnished
//  to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in al
// l copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IM
// PLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNES
// S FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
//  OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WH
// ETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
//  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#define V8_USE_UNSAFE_HANDLES

#include "content/nw/src/renderer/shell_content_renderer_client.h"

#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/posix/global_descriptors.h"
#include "base/strings/string16.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/content/renderer/page_click_tracker.h"
#include "components/autofill/content/renderer/autofill_agent.h"
#include "components/autofill/content/renderer/password_autofill_agent.h"
#include "components/autofill/content/renderer/password_generation_agent.h"
#include "content/common/view_messages.h"
#include "content/nw/src/api/dispatcher.h"
#include "content/nw/src/api/api_messages.h"
#include "content/nw/src/api/window_bindings.h"
#include "content/nw/src/common/shell_switches.h"
#include "content/nw/src/nw_package.h"
#include "content/nw/src/nw_version.h"
#include "components/autofill/content/renderer/autofill_agent.h"
#include "components/autofill/content/renderer/password_autofill_agent.h"
#include "content/nw/src/renderer/nw_render_view_observer.h"
#include "content/nw/src/renderer/prerenderer/prerenderer_client.h"
#include "content/nw/src/renderer/printing/print_web_view_helper.h"
#include "content/nw/src/renderer/shell_render_process_observer.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/content_constants.h"
#include "content/public/renderer/render_frame_observer_tracker.h"
#include "content/public/renderer/render_thread.h"
#include "content/public/renderer/render_view.h"
#include "content/renderer/render_view_impl.h"
#include "ipc/ipc_descriptors.h"
#include "net/proxy/proxy_bypass_rules.h"
#include "third_party/node/src/node.h"
#undef CHECK
#include "third_party/node/src/node_internals.h"
#include "third_party/node/src/req_wrap.h"
#include "third_party/WebKit/public/web/WebDocument.h"
#include "third_party/WebKit/public/web/WebLocalFrame.h"
#include "third_party/WebKit/public/web/WebSecurityOrigin.h"
#include "third_party/WebKit/public/web/WebSecurityPolicy.h"
#include "third_party/WebKit/public/web/WebView.h"
//#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
#include "content/common/dom_storage/dom_storage_map.h"

#include "nw/id/commit.h"

#include "extensions/common/extensions_client.h"
#include "extensions/common/switches.h"
#include "extensions/renderer/default_dispatcher_delegate.h"
#include "extensions/renderer/dispatcher.h"
#include "extensions/renderer/extension_helper.h"
#include "extensions/renderer/guest_view/extensions_guest_view_container.h"
#include "extensions/renderer/guest_view/mime_handler_view/mime_handler_view_container.h"
#include "content/nw/src/common/shell_extensions_client.h"
#include "content/nw/src/renderer/shell_extensions_renderer_client.h"


using content::RenderView;
using content::RenderViewImpl;
using autofill::AutofillAgent;
using autofill::PasswordAutofillAgent;
using autofill::PasswordGenerationAgent;
using net::ProxyBypassRules;
using blink::WebFrame;
using blink::WebLocalFrame;
using blink::WebView;
using blink::WebString;
using blink::WebSecurityPolicy;
using extensions::ExtensionsClient;

namespace content {

namespace {

// TODO: promote ExtensionFrameHelper to a common place and share with this.
class ShellFrameHelper
    : public content::RenderFrameObserver,
      public content::RenderFrameObserverTracker<ShellFrameHelper> {
 public:
  ShellFrameHelper(content::RenderFrame* render_frame,
                   extensions::Dispatcher* extension_dispatcher);
  ~ShellFrameHelper() override;

  // RenderFrameObserver implementation.
  void WillReleaseScriptContext(v8::Handle<v8::Context>, int world_id) override;

 private:
  extensions::Dispatcher* extension_dispatcher_;

  DISALLOW_COPY_AND_ASSIGN(ShellFrameHelper);
};

ShellFrameHelper::ShellFrameHelper(content::RenderFrame* render_frame,
                                   extensions::Dispatcher* extension_dispatcher)
    : content::RenderFrameObserver(render_frame),
      content::RenderFrameObserverTracker<ShellFrameHelper>(render_frame),
      extension_dispatcher_(extension_dispatcher) {
}

ShellFrameHelper::~ShellFrameHelper() {
}

void ShellFrameHelper::WillReleaseScriptContext(v8::Handle<v8::Context> context,
                                                int world_id) {
  extension_dispatcher_->WillReleaseScriptContext(
      render_frame()->GetWebFrame(), context, world_id);
}

RenderView* GetCurrentRenderView() {
  WebLocalFrame* frame = WebLocalFrame::frameForCurrentContext();
  DCHECK(frame);
  if (!frame)
    return NULL;

  WebView* view = frame->view();
  if (!view)
    return NULL;  // can happen during closing.

  RenderView* render_view = RenderView::FromWebView(view);
  DCHECK(render_view);
  return render_view;
}

}  // namespace

ShellContentRendererClient::ShellContentRendererClient()
  :in_nav_cb_(false), creating_first_context_(true) {
}

ShellContentRendererClient::~ShellContentRendererClient() {
}

void ShellContentRendererClient::RenderThreadStarted() {
  content::RenderThread* thread = content::RenderThread::Get();

  // Change working directory.
  CommandLine* command_line = CommandLine::ForCurrentProcess();
  if (command_line->HasSwitch(switches::kWorkingDirectory)) {
    base::SetCurrentDirectory(
        command_line->GetSwitchValuePath(switches::kWorkingDirectory));
  }

  if (command_line->HasSwitch(switches::kDomStorageQuota)) {
    std::string quota_str = command_line->GetSwitchValueASCII(switches::kDomStorageQuota);
    int quota = 0;
    if (base::StringToInt(quota_str, &quota) && quota > 0) {
      content::DOMStorageMap::SetQuotaOverride(quota * 1024 * 1024);
    }
  }

  if (command_line->HasSwitch(extensions::switches::kExtensionProcess)) {
    extensions_client_.reset(CreateExtensionsClient());
    extensions::ExtensionsClient::Set(extensions_client_.get());

    extensions_renderer_client_.reset(new extensions::ShellExtensionsRendererClient);
    extensions::ExtensionsRendererClient::Set(extensions_renderer_client_.get());

    extension_dispatcher_delegate_.reset(new extensions::DefaultDispatcherDelegate());

    // Must be initialized after ExtensionsRendererClient.
    extension_dispatcher_.reset(
                                new extensions::Dispatcher(extension_dispatcher_delegate_.get()));
    thread->AddObserver(extension_dispatcher_.get());
  }

  // Start observers.
  shell_observer_.reset(new ShellRenderProcessObserver());

  WebString file_scheme(base::ASCIIToUTF16("file"));
  WebString app_scheme(base::ASCIIToUTF16("app"));
  // file: resources should be allowed to receive CORS requests.
  WebSecurityPolicy::registerURLSchemeAsCORSEnabled(file_scheme);
  WebSecurityPolicy::registerURLSchemeAsCORSEnabled(app_scheme);

}

void ShellContentRendererClient::RenderViewCreated(RenderView* render_view) {
  new nwapi::Dispatcher(render_view);
  new nw::NwRenderViewObserver(render_view);
  new prerender::PrerendererClient(render_view);
#if defined(ENABLE_PRINTING)
  new printing::PrintWebViewHelper(render_view);
#endif

  if (extension_dispatcher_.get()) {
    new extensions::ExtensionHelper(render_view, extension_dispatcher_.get());
    extension_dispatcher_->OnRenderViewCreated(render_view);
  }

  PasswordGenerationAgent* password_generation_agent =
      new PasswordGenerationAgent(render_view);
  PasswordAutofillAgent* password_autofill_agent =
      new PasswordAutofillAgent(render_view);
  new AutofillAgent(render_view,
                    password_autofill_agent,
{'fraction_non_alphanumeric': 0.10836330935251799, 'fraction_numerical': 0.0035971223021582736, 'mean_word_length': 5.8513669618790916, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 33, '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': '5822268', 'n_tokens_mistral': 5409, 'n_tokens_neox': 5268, 'n_words': 1011}