Changeset 86089 in webkit


Ignore:
Timestamp:
May 9, 2011 2:47:10 PM (13 years ago)
Author:
andersca@apple.com
Message:

2011-05-09 Anders Carlsson <andersca@apple.com>

Reviewed by Sam Weinig.

Move EnvironmentVariables out into its own .cpp file
https://bugs.webkit.org/show_bug.cgi?id=60507

  • UIProcess/Launcher/mac/EnvironmentVariables.cpp: Added. (WebKit::EnvironmentVariables::EnvironmentVariables): (WebKit::EnvironmentVariables::~EnvironmentVariables): (WebKit::EnvironmentVariables::set): (WebKit::EnvironmentVariables::get): (WebKit::EnvironmentVariables::appendValue): (WebKit::EnvironmentVariables::valueIfVariableHasName): (WebKit::EnvironmentVariables::createStringForVariable): (WebKit::EnvironmentVariables::copyEnvironmentVariables):
  • UIProcess/Launcher/mac/EnvironmentVariables.h: Added. (WebKit::EnvironmentVariables::environmentPointer):
  • UIProcess/Launcher/mac/ProcessLauncherMac.mm:
  • WebKit2.xcodeproj/project.pbxproj:
Location:
trunk/Source/WebKit2
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebKit2/ChangeLog

    r86077 r86089  
     12011-05-09  Anders Carlsson  <andersca@apple.com>
     2
     3        Reviewed by Sam Weinig.
     4
     5        Move EnvironmentVariables out into its own .cpp file
     6        https://bugs.webkit.org/show_bug.cgi?id=60507
     7
     8        * UIProcess/Launcher/mac/EnvironmentVariables.cpp: Added.
     9        (WebKit::EnvironmentVariables::EnvironmentVariables):
     10        (WebKit::EnvironmentVariables::~EnvironmentVariables):
     11        (WebKit::EnvironmentVariables::set):
     12        (WebKit::EnvironmentVariables::get):
     13        (WebKit::EnvironmentVariables::appendValue):
     14        (WebKit::EnvironmentVariables::valueIfVariableHasName):
     15        (WebKit::EnvironmentVariables::createStringForVariable):
     16        (WebKit::EnvironmentVariables::copyEnvironmentVariables):
     17        * UIProcess/Launcher/mac/EnvironmentVariables.h: Added.
     18        (WebKit::EnvironmentVariables::environmentPointer):
     19        * UIProcess/Launcher/mac/ProcessLauncherMac.mm:
     20        * WebKit2.xcodeproj/project.pbxproj:
     21
    1222011-05-09  Adam Roben  <aroben@apple.com>
    223
  • trunk/Source/WebKit2/UIProcess/Launcher/mac/ProcessLauncherMac.mm

    r80646 r86089  
    2727#import "ProcessLauncher.h"
    2828
     29#import "EnvironmentVariables.h"
    2930#import "RunLoop.h"
    3031#import "WebProcess.h"
     
    6667#endif
    6768}
    68 
    69 class EnvironmentVariables {
    70     WTF_MAKE_NONCOPYABLE(EnvironmentVariables);
    71 
    72 public:
    73     EnvironmentVariables()
    74         : m_environmentPointer(*_NSGetEnviron())
    75     {
    76     }
    77 
    78     ~EnvironmentVariables()
    79     {
    80         size_t size = m_allocatedStrings.size();
    81         for (size_t i = 0; i < size; ++i)
    82             fastFree(m_allocatedStrings[i]);
    83     }
    84 
    85     void set(const char* name, const char* value)
    86     {
    87         // Check if we need to copy the environment.
    88         if (m_environmentPointer == *_NSGetEnviron())
    89             copyEnvironmentVariables();
    90 
    91         // Allocate a string for the name and value.
    92         const char* nameAndValue = createStringForVariable(name, value);
    93 
    94         for (size_t i = 0; i < m_environmentVariables.size() - 1; ++i) {
    95             if (valueIfVariableHasName(m_environmentVariables[i], name)) {
    96                 // Just replace the environment variable.
    97                 m_environmentVariables[i] = const_cast<char*>(nameAndValue);
    98                 return;
    99             }
    100         }
    101 
    102         // Append the new string.
    103         ASSERT(!m_environmentVariables.last());
    104         m_environmentVariables.last() = const_cast<char*>(nameAndValue);
    105         m_environmentVariables.append(static_cast<char*>(0));
    106 
    107         m_environmentPointer = m_environmentVariables.data();
    108     }
    109 
    110     const char* get(const char* name) const
    111     {
    112         for (size_t i = 0; m_environmentPointer[i]; ++i) {
    113             if (const char* value = valueIfVariableHasName(m_environmentPointer[i], name))
    114                 return value;
    115         }
    116         return 0;
    117     }
    118 
    119     // Will append the value with the given separator if the environment variable already exists.
    120     void appendValue(const char* name, const char* value, char separator)
    121     {
    122         const char* existingValue = get(name);
    123         if (!existingValue) {
    124             set(name, value);
    125             return;
    126         }
    127 
    128         Vector<char, 128> newValue;
    129         newValue.append(existingValue, strlen(existingValue));
    130         newValue.append(separator);
    131         newValue.append(value, strlen(value) + 1);
    132 
    133         set(name, newValue.data());
    134     }
    135 
    136     char** environmentPointer() const { return m_environmentPointer; }
    137 
    138 private:
    139     const char* valueIfVariableHasName(const char* environmentVariable, const char* name) const
    140     {
    141         // Find the environment variable name.
    142         const char* equalsLocation = strchr(environmentVariable, '=');
    143         ASSERT(equalsLocation);
    144 
    145         size_t nameLength = equalsLocation - environmentVariable;
    146         if (strlen(name) != nameLength)
    147             return 0;
    148         if (memcmp(environmentVariable, name, nameLength))
    149             return 0;
    150 
    151         return equalsLocation + 1;
    152     }
    153 
    154     const char* createStringForVariable(const char* name, const char* value)
    155     {
    156         int nameLength = strlen(name);
    157         int valueLength = strlen(value);
    158 
    159         // Allocate enough room to hold 'name=value' and the null character.
    160         char* string = static_cast<char*>(fastMalloc(nameLength + 1 + valueLength + 1));
    161         memcpy(string, name, nameLength);
    162         string[nameLength] = '=';
    163         memcpy(string + nameLength + 1, value, valueLength);
    164         string[nameLength + 1 + valueLength] = '\0';
    165 
    166         m_allocatedStrings.append(string);
    167 
    168         return string;
    169     }
    170 
    171     void copyEnvironmentVariables()
    172     {
    173         for (size_t i = 0; (*_NSGetEnviron())[i]; i++)
    174             m_environmentVariables.append((*_NSGetEnviron())[i]);
    175 
    176         // Null-terminate the array.
    177         m_environmentVariables.append(static_cast<char*>(0));
    178 
    179         // Update the environment pointer.
    180         m_environmentPointer = m_environmentVariables.data();
    181     }
    182 
    183     char** m_environmentPointer;
    184     Vector<char*> m_environmentVariables;
    185 
    186     // These allocated strings will be freed in the destructor.
    187     Vector<char*> m_allocatedStrings;
    188 };
    18969
    19070void ProcessLauncher::launchProcess()
  • trunk/Source/WebKit2/WebKit2.xcodeproj/project.pbxproj

    r86021 r86089  
    151151                1A6FBD2811E69BC200DB1371 /* NetscapePlugin.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A6FBD2611E69BC200DB1371 /* NetscapePlugin.h */; };
    152152                1A6FBD2911E69BC200DB1371 /* NetscapePlugin.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1A6FBD2711E69BC200DB1371 /* NetscapePlugin.cpp */; };
     153                1A7C6CDA1378950800B9C04D /* EnvironmentVariables.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1A7C6CD81378950800B9C04D /* EnvironmentVariables.cpp */; };
     154                1A7C6CDB1378950800B9C04D /* EnvironmentVariables.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A7C6CD91378950800B9C04D /* EnvironmentVariables.h */; };
    153155                1A8EF4CB1252403700F7067F /* PluginControllerProxy.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A8EF4C91252403700F7067F /* PluginControllerProxy.h */; };
    154156                1A8EF4CC1252403700F7067F /* PluginControllerProxy.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1A8EF4CA1252403700F7067F /* PluginControllerProxy.cpp */; };
     
    10251027                1A6FBD2611E69BC200DB1371 /* NetscapePlugin.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NetscapePlugin.h; sourceTree = "<group>"; };
    10261028                1A6FBD2711E69BC200DB1371 /* NetscapePlugin.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = NetscapePlugin.cpp; sourceTree = "<group>"; };
     1029                1A7C6CD81378950800B9C04D /* EnvironmentVariables.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = EnvironmentVariables.cpp; sourceTree = "<group>"; };
     1030                1A7C6CD91378950800B9C04D /* EnvironmentVariables.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EnvironmentVariables.h; sourceTree = "<group>"; };
    10271031                1A8EF4C91252403700F7067F /* PluginControllerProxy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PluginControllerProxy.h; sourceTree = "<group>"; };
    10281032                1A8EF4CA1252403700F7067F /* PluginControllerProxy.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PluginControllerProxy.cpp; sourceTree = "<group>"; };
     
    27672771                        isa = PBXGroup;
    27682772                        children = (
     2773                                1A7C6CD81378950800B9C04D /* EnvironmentVariables.cpp */,
     2774                                1A7C6CD91378950800B9C04D /* EnvironmentVariables.h */,
    27692775                                BC111B1B112F5FE600337BAB /* ProcessLauncherMac.mm */,
    27702776                                BCF501B3123EF602005955AE /* ThreadLauncherMac.mm */,
     
    37133719                                6EE849C81368D9390038D481 /* WKInspectorMac.h in Headers */,
    37143720                                37F90DE31376560E0051CF68 /* HTTPCookieAcceptPolicy.h in Headers */,
     3721                                1A7C6CDB1378950800B9C04D /* EnvironmentVariables.h in Headers */,
    37153722                        );
    37163723                        runOnlyForDeploymentPostprocessing = 0;
     
    43444351                                51C4032C136749D800DC972D /* AuthenticationManager.mac.mm in Sources */,
    43454352                                DF58C6361371ACA000F9A37C /* NativeWebWheelEventMac.mm in Sources */,
     4353                                1A7C6CDA1378950800B9C04D /* EnvironmentVariables.cpp in Sources */,
    43464354                        );
    43474355                        runOnlyForDeploymentPostprocessing = 0;
Note: See TracChangeset for help on using the changeset viewer.