<commit_before>// Copyright (c) 2012 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/extensions/extension_resource_protocols.h"

#include "base/files/file_path.h"
#include "base/path_service.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/extensions/extension_file_util.h"
#include "content/public/browser/browser_thread.h"
#include "net/url_request/url_request_file_job.h"

namespace {

class ExtensionResourcesJob : public net::URLRequestFileJob {
 public:
  ExtensionResourcesJob(net::URLRequest* request,
                        net::NetworkDelegate* network_delegate)
    : net::URLRequestFileJob(request, network_delegate, base::FilePath()),
      thread_id_(content::BrowserThread::UI) {
  }

  virtual void Start() OVERRIDE;

 protected:
  virtual ~ExtensionResourcesJob() {}

  void ResolvePath();
  void ResolvePathDone();

 private:
  content::BrowserThread::ID thread_id_;
};

void ExtensionResourcesJob::Start() {
  bool result =
      content::BrowserThread::GetCurrentThreadIdentifier(&thread_id_);
  CHECK(result) << "Can not get thread id.";
  content::BrowserThread::PostTask(
      content::BrowserThread::FILE, FROM_HERE,
      base::Bind(&ExtensionResourcesJob::ResolvePath, this));
}

void ExtensionResourcesJob::ResolvePath() {
  base::FilePath root_path;
  PathService::Get(chrome::DIR_RESOURCES_EXTENSION, &root_path);
  file_path_ = extension_file_util::ExtensionResourceURLToFilePath(
      request()->url(), root_path);
  content::BrowserThread::PostTask(
      thread_id_, FROM_HERE,
      base::Bind(&ExtensionResourcesJob::ResolvePathDone, this));
}

void ExtensionResourcesJob::ResolvePathDone() {
  net::URLRequestFileJob::Start();
}

class ExtensionResourceProtocolHandler
    : public net::URLRequestJobFactory::ProtocolHandler {
 public:
  ExtensionResourceProtocolHandler() {}
  virtual ~ExtensionResourceProtocolHandler() {}

  virtual net::URLRequestJob* MaybeCreateJob(
      net::URLRequest* request,
      net::NetworkDelegate* network_delegate) const OVERRIDE;

 private:
  DISALLOW_COPY_AND_ASSIGN(ExtensionResourceProtocolHandler);
};

// Creates URLRequestJobs for chrome-extension-resource:// URLs.
net::URLRequestJob*
ExtensionResourceProtocolHandler::MaybeCreateJob(
    net::URLRequest* request, net::NetworkDelegate* network_delegate) const {
  return new ExtensionResourcesJob(request, network_delegate);
}

}  // namespace
<commit_msg>Fix a rare crash in ExtensionResourcesJob::ResolvePath.<commit_after>// Copyright (c) 2012 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/extensions/extension_resource_protocols.h"

#include "base/files/file_path.h"
#include "base/memory/weak_ptr.h"
#include "base/path_service.h"
#include "base/threading/thread_checker.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/extensions/extension_file_util.h"
#include "content/public/browser/browser_thread.h"
#include "net/url_request/url_request_file_job.h"

namespace {

base::FilePath ResolvePath(const GURL& url) {
  base::FilePath root_path;
  PathService::Get(chrome::DIR_RESOURCES_EXTENSION, &root_path);
  return extension_file_util::ExtensionResourceURLToFilePath(url, root_path);
}

class ExtensionResourcesJob : public net::URLRequestFileJob {
 public:
  ExtensionResourcesJob(net::URLRequest* request,
                        net::NetworkDelegate* network_delegate)
    : net::URLRequestFileJob(request, network_delegate, base::FilePath()),
      weak_ptr_factory_(this) {
  }

  virtual void Start() OVERRIDE;

 protected:
  virtual ~ExtensionResourcesJob() {}

  void ResolvePathDone(const base::FilePath& resolved_path);

 private:
  base::WeakPtrFactory<ExtensionResourcesJob> weak_ptr_factory_;

  base::ThreadChecker thread_checker_;

  DISALLOW_COPY_AND_ASSIGN(ExtensionResourcesJob);
};

void ExtensionResourcesJob::Start() {
  DCHECK(thread_checker_.CalledOnValidThread());
  content::BrowserThread::PostTaskAndReplyWithResult(
      content::BrowserThread::FILE, FROM_HERE,
      base::Bind(&ResolvePath, request()->url()),
      base::Bind(&ExtensionResourcesJob::ResolvePathDone,
          weak_ptr_factory_.GetWeakPtr()));
}

void ExtensionResourcesJob::ResolvePathDone(
    const base::FilePath& resolved_path) {
  DCHECK(thread_checker_.CalledOnValidThread());
  file_path_ = resolved_path;
  net::URLRequestFileJob::Start();
}

class ExtensionResourceProtocolHandler
    : public net::URLRequestJobFactory::ProtocolHandler {
 public:
  ExtensionResourceProtocolHandler() {}
  virtual ~ExtensionResourceProtocolHandler() {}

  virtual net::URLRequestJob* MaybeCreateJob(
      net::URLRequest* request,
      net::NetworkDelegate* network_delegate) const OVERRIDE;

 private:
  DISALLOW_COPY_AND_ASSIGN(ExtensionResourceProtocolHandler);
};

// Creates URLRequestJobs for chrome-extension-resource:// URLs.
net::URLRequestJob*
ExtensionResourceProtocolHandler::MaybeCreateJob(
    net::URLRequest* request, net::NetworkDelegate* network_delegate) const {
  return new ExtensionResourcesJob(request, network_delegate);
}

}  // namespace

{'fraction_non_alphanumeric': 0.11085887249337874, 'fraction_numerical': 0.001513431706394249, 'mean_word_length': 6.068181818181818, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, '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': '933706', 'n_tokens_mistral': 1525, 'n_tokens_neox': 1495, 'n_words': 290}