// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "ios/web/webui/shared_resources_data_source_ios.h"

#import <stddef.h>

#import "base/check.h"
#import "base/memory/ref_counted_memory.h"
#import "base/strings/string_util.h"
#import "ios/web/grit/ios_web_resources.h"
#import "ios/web/grit/ios_web_resources_map.h"
#import "ios/web/public/web_client.h"
#import "ios/web/webui/web_ui_constants.h"
#import "mojo/public/js/grit/mojo_bindings_resources.h"
#import "mojo/public/js/grit/mojo_bindings_resources_map.h"
#import "net/base/mime_util.h"
#import "ui/base/webui/resource_path.h"
#import "ui/base/webui/web_ui_util.h"
#import "ui/webui/resources/grit/webui_resources.h"
#import "ui/webui/resources/grit/webui_resources_map.h"

namespace web {

namespace {

// Maps a path name (i.e. "/js/path.js") to a resource map entry. Returns
// nullptr if not found.
const webui::ResourcePath* PathToResource(std::string_view path) {
  for (const auto& resource : kWebuiResources) {
    if (path == resource.path) {
      return &resource;
    }
  }
  for (const auto& resource : kMojoBindingsResources) {
    if (path == resource.path) {
      return &resource;
    }
  }
  for (const auto& resource : kIosWebResources) {
    if (path == resource.path) {
      return &resource;
    }
  }

  return nullptr;
}

}  // namespace

SharedResourcesDataSourceIOS::SharedResourcesDataSourceIOS() {}

SharedResourcesDataSourceIOS::~SharedResourcesDataSourceIOS() {}

std::string SharedResourcesDataSourceIOS::GetSource() const {
  return kWebUIResourcesHost;
}

void SharedResourcesDataSourceIOS::StartDataRequest(
    std::string_view path,
    URLDataSourceIOS::GotDataCallback callback) {
  const webui::ResourcePath* resource = PathToResource(path);
  DCHECK(resource) << " path: " << path;
  scoped_refptr<base::RefCountedMemory> bytes;

  WebClient* web_client = GetWebClient();

  int idr = resource ? resource->id : -1;
  if (idr == IDR_WEBUI_CSS_TEXT_DEFAULTS_CSS) {
    bytes = base::MakeRefCounted<base::RefCountedString>(
        webui::GetWebUiCssTextDefaults());
  } else {
    bytes = web_client->GetDataResourceBytes(idr);
  }

  std::move(callback).Run(bytes.get());
}

std::string SharedResourcesDataSourceIOS::GetMimeType(
    std::string_view path) const {
  std::string mime_type;
  net::GetMimeTypeFromFile(base::FilePath().AppendASCII(path), &mime_type);
  return mime_type;
}

}  // namespace web
