OSDN Git Service

Merge WebKit at r78450: Initial merge by git.
[android-x86/external-webkit.git] / Source / WebKit2 / UIProcess / win / WebPageProxyWin.cpp
1 /*
2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27 #include "WebPageProxy.h"
28
29 #include <tchar.h>
30 #include <WebCore/Language.h>
31 #include <WebCore/WebCoreInstanceHandle.h>
32 #include <wtf/StdLibExtras.h>
33 #include <wtf/text/StringConcatenate.h>
34
35 using namespace WebCore;
36
37 namespace WebKit {
38
39 static String windowsVersion()
40 {
41    String osVersion;
42    OSVERSIONINFO versionInfo = { 0 };
43    versionInfo.dwOSVersionInfoSize = sizeof(versionInfo);
44    ::GetVersionEx(&versionInfo);
45
46    if (versionInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
47        if (versionInfo.dwMajorVersion == 4) {
48            if (versionInfo.dwMinorVersion == 0)
49                osVersion = "Windows 95";
50            else if (versionInfo.dwMinorVersion == 10)
51                osVersion = "Windows 98";
52            else if (versionInfo.dwMinorVersion == 90)
53                osVersion = "Windows 98; Win 9x 4.90";
54        }
55    } else if (versionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT)
56        osVersion = makeString("Windows NT ", String::number(versionInfo.dwMajorVersion), '.', String::number(versionInfo.dwMinorVersion));
57
58    if (!osVersion.length())
59        osVersion = makeString("Windows ", String::number(versionInfo.dwMajorVersion), '.', String::number(versionInfo.dwMinorVersion));
60    return osVersion;
61 }
62
63 static String userVisibleWebKitVersionString()
64 {
65    String versionStr = "420+";
66    void* data = 0;
67
68    struct LANGANDCODEPAGE {
69        WORD wLanguage;
70        WORD wCodePage;
71    } *lpTranslate;
72
73    TCHAR path[MAX_PATH];
74    ::GetModuleFileName(instanceHandle(), path, WTF_ARRAY_LENGTH(path));
75    DWORD handle;
76    DWORD versionSize = ::GetFileVersionInfoSize(path, &handle);
77    if (!versionSize)
78        goto exit;
79    data = fastMalloc(versionSize);
80    if (!data)
81        goto exit;
82    if (!::GetFileVersionInfo(path, 0, versionSize, data))
83        goto exit;
84    UINT cbTranslate;
85    if (!::VerQueryValue(data, TEXT("\\VarFileInfo\\Translation"), (LPVOID*)&lpTranslate, &cbTranslate))
86        goto exit;
87    TCHAR key[256];
88    _stprintf_s(key, WTF_ARRAY_LENGTH(key), TEXT("\\StringFileInfo\\%04x%04x\\ProductVersion"), lpTranslate[0].wLanguage, lpTranslate[0].wCodePage);
89    LPCTSTR productVersion;
90    UINT productVersionLength;
91    if (!::VerQueryValue(data, (LPTSTR)(LPCTSTR)key, (void**)&productVersion, &productVersionLength))
92        goto exit;
93    versionStr = String(productVersion, productVersionLength - 1);
94
95 exit:
96    if (data)
97        fastFree(data);
98    return versionStr;
99 }
100
101 String WebPageProxy::standardUserAgent(const String& applicationNameForUserAgent)
102 {
103    DEFINE_STATIC_LOCAL(String, osVersion, (windowsVersion()));
104    DEFINE_STATIC_LOCAL(String, webKitVersion, (userVisibleWebKitVersionString()));
105
106    // FIXME: We should upate the user agent if the default language changes.
107    String language = defaultLanguage();
108
109    if (applicationNameForUserAgent.isEmpty())
110        return makeString("Mozilla/5.0 (Windows; U; ", osVersion, "; ", language, ") AppleWebKit/", webKitVersion, " (KHTML, like Gecko)");
111    return makeString("Mozilla/5.0 (Windows; U; ", osVersion, "; ", language, ") AppleWebKit/", webKitVersion, " (KHTML, like Gecko) ", applicationNameForUserAgent);
112 }
113
114 } // namespace WebKit