• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that
3 // can be found in the LICENSE file.
4 
5 #include "tests/shared/browser/resource_util.h"
6 
7 #include <stdio.h>
8 
9 namespace client {
10 
11 namespace {
12 
FileExists(const char * path)13 bool FileExists(const char* path) {
14   FILE* f = fopen(path, "rb");
15   if (f) {
16     fclose(f);
17     return true;
18   }
19   return false;
20 }
21 
ReadFileToString(const char * path,std::string & data)22 bool ReadFileToString(const char* path, std::string& data) {
23   // Implementation adapted from base/file_util.cc
24   FILE* file = fopen(path, "rb");
25   if (!file)
26     return false;
27 
28   char buf[1 << 16];
29   size_t len;
30   while ((len = fread(buf, 1, sizeof(buf), file)) > 0)
31     data.append(buf, len);
32   fclose(file);
33 
34   return true;
35 }
36 
37 }  // namespace
38 
LoadBinaryResource(const char * resource_name,std::string & resource_data)39 bool LoadBinaryResource(const char* resource_name, std::string& resource_data) {
40   std::string path;
41   if (!GetResourceDir(path))
42     return false;
43 
44   path.append("/");
45   path.append(resource_name);
46 
47   return ReadFileToString(path.c_str(), resource_data);
48 }
49 
GetBinaryResourceReader(const char * resource_name)50 CefRefPtr<CefStreamReader> GetBinaryResourceReader(const char* resource_name) {
51   std::string path;
52   if (!GetResourceDir(path))
53     return nullptr;
54 
55   path.append("/");
56   path.append(resource_name);
57 
58   if (!FileExists(path.c_str()))
59     return nullptr;
60 
61   return CefStreamReader::CreateForFile(path);
62 }
63 
64 }  // namespace client
65