summaryrefslogtreecommitdiff
path: root/chromium/extensions/common/manifest_handlers/mime_types_handler.cc
blob: 382855c1210d90ff18e17be8ada080132e28e7a2 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright 2014 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 "extensions/common/manifest_handlers/mime_types_handler.h"

#include <stddef.h>

#include "base/logging.h"
#include "base/stl_util.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "content/public/common/webplugininfo.h"
#include "extensions/common/constants.h"
#include "extensions/common/error_utils.h"
#include "extensions/common/manifest.h"
#include "extensions/common/manifest_constants.h"

namespace keys = extensions::manifest_keys;
namespace errors = extensions::manifest_errors;

namespace {

const char* const kMIMETypeHandlersWhitelist[] = {
    extension_misc::kPdfExtensionId,
    extension_misc::kQuickOfficeComponentExtensionId,
    extension_misc::kQuickOfficeInternalExtensionId,
    extension_misc::kQuickOfficeExtensionId,
    extension_misc::kMimeHandlerPrivateTestExtensionId};

constexpr SkColor kPdfExtensionBackgroundColor = SkColorSetRGB(82, 86, 89);
constexpr SkColor kQuickOfficeExtensionBackgroundColor =
    SkColorSetRGB(241, 241, 241);

// Stored on the Extension.
struct MimeTypesHandlerInfo : public extensions::Extension::ManifestData {
  MimeTypesHandler handler_;

  MimeTypesHandlerInfo();
  ~MimeTypesHandlerInfo() override;
};

MimeTypesHandlerInfo::MimeTypesHandlerInfo() {
}

MimeTypesHandlerInfo::~MimeTypesHandlerInfo() {
}

}  // namespace

// static
std::vector<std::string> MimeTypesHandler::GetMIMETypeWhitelist() {
  std::vector<std::string> whitelist;
  for (size_t i = 0; i < base::size(kMIMETypeHandlersWhitelist); ++i)
    whitelist.push_back(kMIMETypeHandlersWhitelist[i]);
  return whitelist;
}

MimeTypesHandler::MimeTypesHandler() {
}

MimeTypesHandler::~MimeTypesHandler() {
}

void MimeTypesHandler::AddMIMEType(const std::string& mime_type) {
  mime_type_set_.insert(mime_type);
}

bool MimeTypesHandler::CanHandleMIMEType(const std::string& mime_type) const {
  return mime_type_set_.find(mime_type) != mime_type_set_.end();
}

bool MimeTypesHandler::HasPlugin() const {
  return !handler_url_.empty();
}

SkColor MimeTypesHandler::GetBackgroundColor() const {
  if (extension_id_ == extension_misc::kPdfExtensionId) {
    return kPdfExtensionBackgroundColor;
  }
  if (extension_id_ == extension_misc::kQuickOfficeExtensionId ||
      extension_id_ == extension_misc::kQuickOfficeInternalExtensionId ||
      extension_id_ == extension_misc::kQuickOfficeComponentExtensionId) {
    return kQuickOfficeExtensionBackgroundColor;
  }
  return content::WebPluginInfo::kDefaultBackgroundColor;
}

base::FilePath MimeTypesHandler::GetPluginPath() const {
  // TODO(raymes): Storing the extension URL in a base::FilePath is really
  // nasty. We should probably just use the extension ID as the placeholder path
  // instead.
  return base::FilePath::FromUTF8Unsafe(
      std::string(extensions::kExtensionScheme) + "://" + extension_id_ + "/");
}

// static
MimeTypesHandler* MimeTypesHandler::GetHandler(
    const extensions::Extension* extension) {
  MimeTypesHandlerInfo* info = static_cast<MimeTypesHandlerInfo*>(
      extension->GetManifestData(keys::kMimeTypesHandler));
  if (info)
    return &info->handler_;
  return NULL;
}

MimeTypesHandlerParser::MimeTypesHandlerParser() {
}

MimeTypesHandlerParser::~MimeTypesHandlerParser() {
}

bool MimeTypesHandlerParser::Parse(extensions::Extension* extension,
                                   base::string16* error) {
  const base::Value* mime_types_value = nullptr;
  if (!extension->manifest()->GetList(keys::kMIMETypes, &mime_types_value)) {
    *error = base::ASCIIToUTF16(errors::kInvalidMimeTypesHandler);
    return false;
  }

  auto info = std::make_unique<MimeTypesHandlerInfo>();
  info->handler_.set_extension_id(extension->id());
  for (const auto& entry : mime_types_value->GetList()) {
    if (!entry.is_string()) {
      *error = base::ASCIIToUTF16(errors::kInvalidMIMETypes);
      return false;
    }
    info->handler_.AddMIMEType(entry.GetString());
  }

  std::string mime_types_handler;
  if (extension->manifest()->GetString(keys::kMimeTypesHandler,
                                       &mime_types_handler)) {
    info->handler_.set_handler_url(mime_types_handler);
  }

  extension->SetManifestData(keys::kMimeTypesHandler, std::move(info));
  return true;
}

base::span<const char* const> MimeTypesHandlerParser::Keys() const {
  static constexpr const char* kKeys[] = {keys::kMIMETypes,
                                          keys::kMimeTypesHandler};
#if !defined(__GNUC__) || __GNUC__ > 5
  return kKeys;
#else
  return base::make_span(kKeys, 2);
#endif
}