summaryrefslogtreecommitdiffstats
path: root/chrome/common/extensions/extension_sidebar_utils.cc
blob: d2abfa942f37567b955c0c49b055c60c2f970cad (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
61
62
63
64
// Copyright (c) 2010 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/common/extensions/extension_sidebar_utils.h"

#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_error_utils.h"
#include "chrome/common/url_constants.h"
#include "googleurl/src/gurl.h"

namespace {

// Errors.
const char kInvalidUrlError[] = "Invalid url: \"*\".";

bool CanUseHost(const Extension* extension,
                const GURL& url,
                std::string* error) {
  if (extension->HasHostPermission(url))
    return true;

  *error = ExtensionErrorUtils::FormatErrorMessage(
      extension_manifest_errors::kCannotAccessPage, url.spec());
  return false;
}

}  // namespace

namespace extension_sidebar_utils {

std::string GetExtensionIdByContentId(const std::string& content_id) {
  // At the moment, content_id == extension_id.
  return content_id;
}

GURL ResolveAndVerifyUrl(const std::string& url_string,
                         const Extension* extension,
                         std::string* error) {
  // Resolve possibly relative URL.
  GURL url(url_string);
  if (!url.is_valid())
    url = extension->GetResourceURL(url_string);

  if (!url.is_valid()) {
    *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidUrlError,
                                                     url_string);
    return GURL();
  }
  if (!url.SchemeIs(chrome::kExtensionScheme) &&
      !CanUseHost(extension, url, error)) {
    return GURL();
  }
  // Disallow requests outside of the requesting extension view's extension.
  if (url.SchemeIs(chrome::kExtensionScheme)) {
    std::string extension_id(url.host());
    if (extension_id != extension->id())
      return GURL();
  }

  return url;
}

}  // namespace extension_sidebar_utils