1// Copyright (c) 2012 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#include "content/common/url_schemes.h"
6
7#include <string.h>
8
9#include <algorithm>
10#include <string>
11#include <vector>
12
13#include "base/basictypes.h"
14#include "base/strings/string_util.h"
15#include "content/common/savable_url_schemes.h"
16#include "content/public/common/content_client.h"
17#include "content/public/common/url_constants.h"
18#include "url/url_util.h"
19
20namespace {
21
22void AddStandardSchemeHelper(const std::string& scheme) {
23  url_util::AddStandardScheme(scheme.c_str());
24}
25
26}  // namespace
27
28namespace content {
29
30void RegisterContentSchemes(bool lock_standard_schemes) {
31  std::vector<std::string> additional_standard_schemes;
32  std::vector<std::string> additional_savable_schemes;
33  GetContentClient()->AddAdditionalSchemes(&additional_standard_schemes,
34                                           &additional_savable_schemes);
35
36  url_util::AddStandardScheme(chrome::kChromeDevToolsScheme);
37  url_util::AddStandardScheme(chrome::kChromeUIScheme);
38  url_util::AddStandardScheme(kGuestScheme);
39  url_util::AddStandardScheme(kMetadataScheme);
40  std::for_each(additional_standard_schemes.begin(),
41                additional_standard_schemes.end(),
42                AddStandardSchemeHelper);
43
44  // Prevent future modification of the standard schemes list. This is to
45  // prevent accidental creation of data races in the program. AddStandardScheme
46  // isn't threadsafe so must be called when GURL isn't used on any other
47  // thread. This is really easy to mess up, so we say that all calls to
48  // AddStandardScheme in Chrome must be inside this function.
49  if (lock_standard_schemes)
50    url_util::LockStandardSchemes();
51
52  // We rely on the above lock to protect this part from being invoked twice.
53  if (!additional_savable_schemes.empty()) {
54    const char* const* default_schemes = GetSavableSchemesInternal();
55    const char* const* default_schemes_end = NULL;
56    for (default_schemes_end = default_schemes; *default_schemes_end;
57         ++default_schemes_end) {}
58    const int default_schemes_count = default_schemes_end - default_schemes;
59
60    int schemes = static_cast<int>(additional_savable_schemes.size());
61    // The array, and the copied schemes won't be freed, but will remain
62    // reachable.
63    char **savable_schemes = new char*[schemes + default_schemes_count + 1];
64    memcpy(savable_schemes,
65           default_schemes,
66           default_schemes_count * sizeof(default_schemes[0]));
67    for (int i = 0; i < schemes; ++i) {
68      savable_schemes[default_schemes_count + i] =
69          base::strdup(additional_savable_schemes[i].c_str());
70    }
71    savable_schemes[default_schemes_count + schemes] = 0;
72
73    SetSavableSchemes(savable_schemes);
74  }
75}
76
77}  // namespace content
78