Revert 199284 "[Downloads] Clear current_path_ when deleting int..."
[chromium-blink-merge.git] / content / common / url_schemes.cc
blob3c64243c808fa992819b6a2972f2905364347492
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/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 "googleurl/src/url_util.h"
20 namespace {
22 void AddStandardSchemeHelper(const std::string& scheme) {
23 url_util::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(
34 &additional_standard_schemes,
35 &additional_savable_schemes);
37 // Don't need "chrome-internal" which was used in old versions of Chrome for
38 // the new tab page.
39 url_util::AddStandardScheme(chrome::kChromeDevToolsScheme);
40 url_util::AddStandardScheme(chrome::kChromeUIScheme);
41 url_util::AddStandardScheme(chrome::kMetadataScheme);
42 url_util::AddStandardScheme(chrome::kGuestScheme);
43 std::for_each(additional_standard_schemes.begin(),
44 additional_standard_schemes.end(),
45 AddStandardSchemeHelper);
47 // Prevent future modification of the standard schemes list. This is to
48 // prevent accidental creation of data races in the program. AddStandardScheme
49 // isn't threadsafe so must be called when GURL isn't used on any other
50 // thread. This is really easy to mess up, so we say that all calls to
51 // AddStandardScheme in Chrome must be inside this function.
52 if (lock_standard_schemes)
53 url_util::LockStandardSchemes();
55 // We rely on the above lock to protect this part from being invoked twice.
56 if (!additional_savable_schemes.empty()) {
57 const char* const* default_schemes = GetSavableSchemesInternal();
58 const char* const* default_schemes_end = NULL;
59 for (default_schemes_end = default_schemes; *default_schemes_end;
60 ++default_schemes_end) {}
61 const int default_schemes_count = default_schemes_end - default_schemes;
63 int schemes = static_cast<int>(additional_savable_schemes.size());
64 // The array, and the copied schemes won't be freed, but will remain
65 // reachable.
66 char **savable_schemes = new char*[schemes + default_schemes_count + 1];
67 memcpy(savable_schemes,
68 default_schemes,
69 default_schemes_count * sizeof(default_schemes[0]));
70 for (int i = 0; i < schemes; ++i) {
71 savable_schemes[default_schemes_count + i] =
72 base::strdup(additional_savable_schemes[i].c_str());
74 savable_schemes[default_schemes_count + schemes] = 0;
76 SetSavableSchemes(savable_schemes);
80 } // namespace content