summaryrefslogtreecommitdiffstats
path: root/chrome_frame/simple_resource_loader.h
blob: 9f10934ed64b429366c697aeabee0199cfeb53aa (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright (c) 2010 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.
//
// This class implements a simplified and much stripped down version of
// Chrome's app/resource_bundle machinery. It notably avoids unwanted
// dependencies on things like Skia.
//

#ifndef CHROME_FRAME_SIMPLE_RESOURCE_LOADER_H_
#define CHROME_FRAME_SIMPLE_RESOURCE_LOADER_H_

#include <windows.h>
#include <string>
#include <vector>

#include "base/file_path.h"
#include "base/gtest_prod_util.h"
#include "base/singleton.h"

class SimpleResourceLoader {
 public:

  static SimpleResourceLoader* instance() {
    return Singleton<SimpleResourceLoader>::get();
  }

  // Returns the language tag for the active language.
  static std::wstring GetLanguage();

  // Helper method to return the string resource identified by message_id
  // from the currently loaded locale dll.
  static std::wstring Get(int message_id);

  // Retrieves the HINSTANCE of the loaded module handle. May be NULL if a
  // resource DLL could not be loaded.
  HMODULE GetResourceModuleHandle();

  // Retrieves the preferred languages for the current thread, adding them to
  // |language_tags|.
  static void GetPreferredLanguages(std::vector<std::wstring>* language_tags);

  // Retrieves the thread/process/user/system preferred languages on Vista+,
  // adding them and their fallbacks to |language_tags|.  Returns |false| if the
  // platform does not support such (i.e., XP).
  static bool GetThreadPreferredUILanguages(
      std::vector<std::wstring>* language_tags);

  // Retrieves the system language and the region using ICU (used on XP).
  static void GetICUSystemLanguage(std::wstring* language,
                                   std::wstring* region);

  // Populates |locales_path| with the path to the "Locales" directory.
  static void DetermineLocalesDirectory(FilePath* locales_path);

  // Returns false if |language_tag| is malformed.
  static bool IsValidLanguageTag(const std::wstring& language_tag);

 private:
  SimpleResourceLoader();
  ~SimpleResourceLoader();

  // Finds the most-preferred resource DLL for the laguages in |language_tags|
  // in |locales_path|.
  //
  // Returns true on success with a handle to the DLL that was loaded in
  // |dll_handle| and its path in |file_path|.
  static bool LoadLocaleDll(const std::vector<std::wstring>& language_tags,
                            const FilePath& locales_path,
                            HMODULE* dll_handle,
                            FilePath* file_path);

  // Returns the string resource identified by message_id from the currently
  // loaded locale dll.
  std::wstring GetLocalizedResource(int message_id);

  friend struct DefaultSingletonTraits<SimpleResourceLoader>;

  FRIEND_TEST_ALL_PREFIXES(SimpleResourceLoaderTest, LoadLocaleDll);

  std::wstring language_;
  HINSTANCE locale_dll_handle_;
};

#endif  // CHROME_FRAME_SIMPLE_RESOURCE_LOADER_H_