summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_protocols.cc
blob: bc33592dfd86ff461a78278e783f78653e1b63d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright (c) 2006-2008 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_protocols.h"

#include "base/string_util.h"
#include "chrome/browser/extensions/extension.h"
#include "chrome/browser/net/chrome_url_request_context.h"
#include "googleurl/src/url_util.h"
#include "net/base/net_util.h"
#include "net/url_request/url_request_file_job.h"

// Defined in extension.h.
extern const char kExtensionURLScheme[];
extern const char kUserScriptURLScheme[];

// Factory registered with URLRequest to create URLRequestJobs for extension://
// URLs.
static URLRequestJob* CreateExtensionURLRequestJob(URLRequest* request,
                                                   const std::string& scheme) {
  ChromeURLRequestContext* context =
      static_cast<ChromeURLRequestContext*>(request->context());

  // chrome-extension://extension-id/resource/path.js
  FilePath directory_path = context->GetPathForExtension(request->url().host());
  if (directory_path.value().empty())
    return NULL;

  std::string resource = request->url().path();
  FilePath path = Extension::GetResourcePath(directory_path, resource);

  return new URLRequestFileJob(request, path);
}

// Factory registered with URLRequest to create URLRequestJobs for
// chrome-user-script:/ URLs.
static URLRequestJob* CreateUserScriptURLRequestJob(URLRequest* request,
                                                    const std::string& scheme) {
  ChromeURLRequestContext* context =
      static_cast<ChromeURLRequestContext*>(request->context());

  // chrome-user-script:/user-script-name.user.js
  FilePath directory_path = context->user_script_dir_path();
  std::string resource = request->url().path();

  FilePath path = Extension::GetResourcePath(directory_path, resource);
  return new URLRequestFileJob(request, path);
}

void RegisterExtensionProtocols() {
  // Being a standard scheme allows us to resolve relative paths. This is used
  // by extensions, but not by standalone user scripts.
  url_util::AddStandardScheme(kExtensionURLScheme);

  URLRequest::RegisterProtocolFactory(kExtensionURLScheme,
                                      &CreateExtensionURLRequestJob);
  URLRequest::RegisterProtocolFactory(kUserScriptURLScheme,
                                      &CreateUserScriptURLRequestJob);
}