summaryrefslogtreecommitdiffstats
path: root/content/test/test_content_client.cc
blob: d59a91187e838790a767f84c79c01ef2f8656c78 (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
// Copyright (c) 2012 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 "content/test/test_content_client.h"

#include <utility>

#include "base/base_paths.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "base/strings/string_piece.h"
#include "build/build_config.h"

#if defined(OS_ANDROID)
#include "base/android/apk_assets.h"
#endif

namespace content {

TestContentClient::TestContentClient()
    : data_pack_(ui::SCALE_FACTOR_100P) {
  // content_shell.pak is not built on iOS as it is not required.
  base::FilePath content_shell_pack_path;
  base::File pak_file;
  base::MemoryMappedFile::Region pak_region;

#if defined(OS_ANDROID)
  // Tests that don't yet use .isolate files require loading from within .apk.
  pak_file = base::File(
      base::android::OpenApkAsset("assets/content_shell.pak", &pak_region));

  // on Android all pak files are inside the paks folder.
  PathService::Get(base::DIR_ANDROID_APP_DATA, &content_shell_pack_path);
  content_shell_pack_path = content_shell_pack_path.Append(
      FILE_PATH_LITERAL("paks"));
#else
  PathService::Get(base::DIR_MODULE, &content_shell_pack_path);
#endif  // defined(OS_ANDROID)

  if (pak_file.IsValid()) {
    data_pack_.LoadFromFileRegion(std::move(pak_file), pak_region);
  } else {
    content_shell_pack_path = content_shell_pack_path.Append(
        FILE_PATH_LITERAL("content_shell.pak"));
    data_pack_.LoadFromPath(content_shell_pack_path);
  }
}

TestContentClient::~TestContentClient() {
}

std::string TestContentClient::GetUserAgent() const {
  return std::string("TestContentClient");
}

base::StringPiece TestContentClient::GetDataResource(
    int resource_id,
    ui::ScaleFactor scale_factor) const {
  base::StringPiece resource;
  data_pack_.GetStringPiece(resource_id, &resource);
  return resource;
}

}  // namespace content