Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / content / common / url_schemes.cc
blobc65bb14562de2f2c0f182c6882bc9d9d808e4205
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.
5 #include "content/common/url_schemes.h"
7 #include <string.h>
9 #include <algorithm>
10 #include <string>
11 #include <vector>
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"
20 namespace {
22 void AddStandardSchemeHelper(const std::string& scheme) {
23 url::AddStandardScheme(scheme.c_str());
26 } // namespace
28 namespace content {
30 void 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);
36 url::AddStandardScheme(kChromeDevToolsScheme);
37 url::AddStandardScheme(kChromeUIScheme);
38 url::AddStandardScheme(kGuestScheme);
39 url::AddStandardScheme(kMetadataScheme);
40 std::for_each(additional_standard_schemes.begin(),
41 additional_standard_schemes.end(),
42 AddStandardSchemeHelper);
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::LockStandardSchemes();
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;
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());
71 savable_schemes[default_schemes_count + schemes] = 0;
73 SetSavableSchemes(savable_schemes);
77 } // namespace content