Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / browser / ui / taskbar_util_win.cc
1 // Copyright (c) 2013 Intel Corporation. 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 #include "xwalk/runtime/browser/ui/taskbar_util.h"
6
7 #include <string>
8 #include "base/command_line.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "content/public/common/content_switches.h"
13 #include "crypto/sha2.h"
14 #include "url/gurl.h"
15
16 #if defined(OS_WIN)
17 #include <shobjidl.h>  // NOLINT(build/include_order)
18 #endif
19
20 namespace xwalk {
21
22 const size_t kIdSize = 16;
23
24 // Converts a normal hexadecimal string into the alphabet.
25 // We use the characters 'a'-'p' instead of '0'-'f' to avoid ever having a
26 // completely numeric host, since some software interprets that as an IP
27 // address.
28 void ConvertHexadecimalToIDAlphabet(std::string* id) {
29   for (size_t i = 0; i < id->size(); ++i) {
30     int val;
31     if (base::HexStringToInt(base::StringPiece(id->begin() + i,
32                                                id->begin() + i + 1),
33                              &val)) {
34       (*id)[i] = val + 'a';
35     } else {
36       (*id)[i] = 'a';
37     }
38   }
39 }
40
41 // Generates an ID from arbitrary input. The same input string will
42 // always generate the same output ID.
43 void GenerateId(const std::string& input, std::string* output) {
44   DCHECK(output);
45   uint8 hash[kIdSize];
46   crypto::SHA256HashString(input, hash, sizeof(hash));
47   *output = base::StringToLowerASCII(base::HexEncode(hash, sizeof(hash)));
48   ConvertHexadecimalToIDAlphabet(output);
49 }
50
51 void SetTaskbarGroupIdForProcess() {
52   CommandLine* command_line = CommandLine::ForCurrentProcess();
53   const CommandLine::StringVector& args = command_line->GetArgs();
54
55   if (args.empty())
56     return;
57
58   GURL url(args[0]);
59   if (url.is_valid() && url.has_scheme()) {
60     std::string appid;
61     GenerateId(url.spec(), &appid);
62     ::SetCurrentProcessExplicitAppUserModelID(base::ASCIIToWide(appid).c_str());
63   }
64 }
65
66 }  // namespace xwalk