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