• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "libcef/browser/extensions/component_extension_resource_manager.h"
6 
7 #include "base/containers/contains.h"
8 #include "base/logging.h"
9 #include "base/path_service.h"
10 #include "base/values.h"
11 #include "chrome/browser/pdf/pdf_extension_util.h"
12 #include "chrome/common/chrome_paths.h"
13 #include "chrome/grit/component_extension_resources_map.h"
14 #include "chrome/grit/pdf_resources_map.h"
15 #include "extensions/common/constants.h"
16 
17 namespace extensions {
18 
CefComponentExtensionResourceManager()19 CefComponentExtensionResourceManager::CefComponentExtensionResourceManager() {
20   AddComponentResourceEntries(kComponentExtensionResources,
21                               kComponentExtensionResourcesSize);
22   AddComponentResourceEntries(kPdfResources, kPdfResourcesSize);
23 
24   base::Value dict(base::Value::Type::DICTIONARY);
25   pdf_extension_util::AddStrings(
26       pdf_extension_util::PdfViewerContext::kPdfViewer, &dict);
27   pdf_extension_util::AddAdditionalData(/*enable_annotations=*/true, &dict);
28 
29   ui::TemplateReplacements pdf_viewer_replacements;
30   ui::TemplateReplacementsFromDictionaryValue(
31       base::Value::AsDictionaryValue(dict), &pdf_viewer_replacements);
32   template_replacements_[extension_misc::kPdfExtensionId] =
33       std::move(pdf_viewer_replacements);
34 }
35 
~CefComponentExtensionResourceManager()36 CefComponentExtensionResourceManager::~CefComponentExtensionResourceManager() {}
37 
IsComponentExtensionResource(const base::FilePath & extension_path,const base::FilePath & resource_path,int * resource_id) const38 bool CefComponentExtensionResourceManager::IsComponentExtensionResource(
39     const base::FilePath& extension_path,
40     const base::FilePath& resource_path,
41     int* resource_id) const {
42   base::FilePath directory_path = extension_path;
43   base::FilePath resources_dir;
44   base::FilePath relative_path;
45   if (!base::PathService::Get(chrome::DIR_RESOURCES, &resources_dir) ||
46       !resources_dir.AppendRelativePath(directory_path, &relative_path)) {
47     return false;
48   }
49   relative_path = relative_path.Append(resource_path);
50   relative_path = relative_path.NormalizePathSeparators();
51 
52   auto entry = path_to_resource_info_.find(relative_path);
53   if (entry != path_to_resource_info_.end()) {
54     *resource_id = entry->second;
55     return true;
56   }
57 
58   return false;
59 }
60 
61 const ui::TemplateReplacements*
GetTemplateReplacementsForExtension(const std::string & extension_id) const62 CefComponentExtensionResourceManager::GetTemplateReplacementsForExtension(
63     const std::string& extension_id) const {
64   auto it = template_replacements_.find(extension_id);
65   return it != template_replacements_.end() ? &it->second : nullptr;
66 }
67 
AddComponentResourceEntries(const webui::ResourcePath * entries,size_t size)68 void CefComponentExtensionResourceManager::AddComponentResourceEntries(
69     const webui::ResourcePath* entries,
70     size_t size) {
71   for (size_t i = 0; i < size; ++i) {
72     base::FilePath resource_path =
73         base::FilePath().AppendASCII(entries[i].path);
74     resource_path = resource_path.NormalizePathSeparators();
75 
76     DCHECK(!base::Contains(path_to_resource_info_, resource_path));
77     path_to_resource_info_[resource_path] = entries[i].id;
78   }
79 }
80 
81 }  // namespace extensions
82