1 // Copyright (c) 2011 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.
5 #ifndef CHROME_FRAME_MODULE_UTILS_H_
6 #define CHROME_FRAME_MODULE_UTILS_H_
11 #include "base/basictypes.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/singleton.h"
14 #include "base/shared_memory.h"
18 class CSecurityAttributes
;
22 // A singleton class that provides a facility to register the version of the
23 // current module as the only version that should be loaded system-wide. If
24 // this module is not the first instance loaded in the system, then the version
25 // that loaded first will be delegated to. This makes a few assumptions:
26 // 1) That different versions of the module this code is in reside in
27 // neighbouring versioned directories, e.g.
28 // C:\foo\bar\1.2.3.4\my_module.dll
29 // C:\foo\bar\1.2.3.5\my_module.dll
30 // 2) That the instance of this class will outlive the module that may be
31 // delegated to. That is to say, that this class only guarantees that the
32 // module is loaded as long as this instance is active.
33 // 3) The module this is compiled into is built with version info.
36 // Returns the singleton instance.
37 static DllRedirector
* GetInstance();
39 virtual ~DllRedirector();
41 // Attempts to register this Chrome Frame version as the first loaded version
42 // on the system. If this succeeds, return true. If it fails, it returns
43 // false meaning that there is another version already loaded somewhere and
44 // the caller should delegate to that version instead.
45 bool DllRedirector::RegisterAsFirstCFModule();
47 // Unregisters the well known window class if we registered it earlier.
48 // This is intended to be called from DllMain under PROCESS_DETACH.
49 void DllRedirector::UnregisterAsFirstCFModule();
51 // Helper function to return the DllGetClassObject function pointer from
52 // the given module. On success, the return value is non-null and module
53 // will have had its reference count incremented.
54 LPFNGETCLASSOBJECT
GetDllGetClassObjectPtr();
58 friend struct DefaultSingletonTraits
<DllRedirector
>;
60 // Constructor used for tests.
61 explicit DllRedirector(const char* shared_memory_name
);
63 // Returns an HMODULE to the version of the module that should be loaded.
64 virtual HMODULE
GetFirstModule();
66 // Returns the version of the current module or NULL if none can be found.
67 // The caller must free the Version.
68 virtual Version
* GetCurrentModuleVersion();
70 // Attempt to load the specified version dll. Finds it by walking up one
71 // directory from our current module's location, then appending the newly
72 // found version number. The Version class in base will have ensured that we
73 // actually have a valid version and not e.g. ..\..\..\..\MyEvilFolder\.
74 virtual HMODULE
LoadVersionedModule(Version
* version
);
76 // Builds the necessary SECURITY_ATTRIBUTES to allow low integrity access
77 // to an object. Returns true on success, false otherwise.
78 virtual bool BuildSecurityAttributesForLock(
79 ATL::CSecurityAttributes
* sec_attr
);
81 // Attempts to change the permissions on the given file mapping to read only.
82 // Returns true on success, false otherwise.
83 virtual bool SetFileMappingToReadOnly(base::SharedMemoryHandle mapping
);
85 // Shared memory segment that contains the version beacon.
86 scoped_ptr
<base::SharedMemory
> shared_memory_
;
88 // The current version of the DLL to be loaded.
89 scoped_ptr
<Version
> dll_version_
;
91 // The handle to the first version of this module that was loaded. This
92 // may refer to the current module, or another version of the same module
93 // that we go and load.
94 HMODULE first_module_handle_
;
96 // Used for tests to override the name of the shared memory segment.
97 std::string shared_memory_name_
;
99 friend class ModuleUtilsTest
;
101 DISALLOW_COPY_AND_ASSIGN(DllRedirector
);
104 #endif // CHROME_FRAME_MODULE_UTILS_H_