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