shared_resources_data_source_ios.mm 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #include "ios/web/webui/shared_resources_data_source_ios.h"
  5. #include <stddef.h>
  6. #include "base/check.h"
  7. #include "base/memory/ref_counted_memory.h"
  8. #include "base/strings/string_util.h"
  9. #import "ios/web/public/web_client.h"
  10. #include "net/base/mime_util.h"
  11. #include "ui/base/webui/resource_path.h"
  12. #include "ui/base/webui/web_ui_util.h"
  13. #include "ui/resources/grit/webui_generated_resources.h"
  14. #include "ui/resources/grit/webui_generated_resources_map.h"
  15. #if !defined(__has_feature) || !__has_feature(objc_arc)
  16. #error "This file requires ARC support."
  17. #endif
  18. namespace web {
  19. namespace {
  20. // Value duplicated from content/public/common/url_constants.h
  21. // TODO(stuartmorgan): Revisit how to share this in a more maintainable way.
  22. const char kWebUIResourcesHost[] = "resources";
  23. // Maps a path name (i.e. "/js/path.js") to a resource map entry. Returns
  24. // nullptr if not found.
  25. const webui::ResourcePath* PathToResource(const std::string& path) {
  26. for (size_t i = 0; i < kWebuiGeneratedResourcesSize; ++i) {
  27. if (path == kWebuiGeneratedResources[i].path)
  28. return &kWebuiGeneratedResources[i];
  29. }
  30. return nullptr;
  31. }
  32. } // namespace
  33. SharedResourcesDataSourceIOS::SharedResourcesDataSourceIOS() {}
  34. SharedResourcesDataSourceIOS::~SharedResourcesDataSourceIOS() {}
  35. std::string SharedResourcesDataSourceIOS::GetSource() const {
  36. return kWebUIResourcesHost;
  37. }
  38. void SharedResourcesDataSourceIOS::StartDataRequest(
  39. const std::string& path,
  40. URLDataSourceIOS::GotDataCallback callback) {
  41. const webui::ResourcePath* resource = PathToResource(path);
  42. DCHECK(resource) << " path: " << path;
  43. scoped_refptr<base::RefCountedMemory> bytes;
  44. WebClient* web_client = GetWebClient();
  45. int idr = resource ? resource->id : -1;
  46. if (idr == IDR_WEBUI_CSS_TEXT_DEFAULTS_CSS) {
  47. std::string css = webui::GetWebUiCssTextDefaults();
  48. bytes = base::RefCountedString::TakeString(&css);
  49. } else {
  50. bytes = web_client->GetDataResourceBytes(idr);
  51. }
  52. std::move(callback).Run(bytes.get());
  53. }
  54. std::string SharedResourcesDataSourceIOS::GetMimeType(
  55. const std::string& path) const {
  56. std::string mime_type;
  57. net::GetMimeTypeFromFile(base::FilePath().AppendASCII(path), &mime_type);
  58. return mime_type;
  59. }
  60. } // namespace web