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