| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/common/extensions/manifest_handlers/settings_overrides_handler.
h" | 5 #include "chrome/common/extensions/manifest_handlers/settings_overrides_handler.
h" |
| 6 | 6 |
| 7 #include "base/strings/string_util.h" |
| 7 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/common/extensions/permissions/permissions_data.h" |
| 10 #include "chrome/common/extensions/permissions/settings_override_permission.h" |
| 8 #include "extensions/common/error_utils.h" | 11 #include "extensions/common/error_utils.h" |
| 9 #include "extensions/common/manifest_constants.h" | 12 #include "extensions/common/manifest_constants.h" |
| 13 #include "extensions/common/permissions/api_permission_set.h" |
| 14 #include "extensions/common/permissions/permissions_info.h" |
| 15 #include "url/gurl.h" |
| 10 | 16 |
| 11 using extensions::api::manifest_types::ChromeSettingsOverrides; | 17 using extensions::api::manifest_types::ChromeSettingsOverrides; |
| 12 | 18 |
| 13 namespace extensions { | 19 namespace extensions { |
| 14 namespace { | 20 namespace { |
| 15 | 21 |
| 22 const char* kWwwPrefix = "www."; |
| 23 |
| 16 scoped_ptr<GURL> CreateManifestURL(const std::string& url) { | 24 scoped_ptr<GURL> CreateManifestURL(const std::string& url) { |
| 17 scoped_ptr<GURL> manifest_url(new GURL(url)); | 25 scoped_ptr<GURL> manifest_url(new GURL(url)); |
| 18 if (!manifest_url->is_valid() || | 26 if (!manifest_url->is_valid() || |
| 19 !manifest_url->SchemeIsHTTPOrHTTPS()) | 27 !manifest_url->SchemeIsHTTPOrHTTPS()) |
| 20 return scoped_ptr<GURL>(); | 28 return scoped_ptr<GURL>(); |
| 21 return manifest_url.Pass(); | 29 return manifest_url.Pass(); |
| 22 } | 30 } |
| 23 | 31 |
| 24 scoped_ptr<GURL> ParseHomepage(const ChromeSettingsOverrides& overrides, | 32 scoped_ptr<GURL> ParseHomepage(const ChromeSettingsOverrides& overrides, |
| 25 string16* error) { | 33 string16* error) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 47 *error = extensions::ErrorUtils::FormatErrorMessageUTF16( | 55 *error = extensions::ErrorUtils::FormatErrorMessageUTF16( |
| 48 manifest_errors::kInvalidStartupOverrideURL, *i); | 56 manifest_errors::kInvalidStartupOverrideURL, *i); |
| 49 } else { | 57 } else { |
| 50 urls.push_back(GURL()); | 58 urls.push_back(GURL()); |
| 51 urls.back().Swap(manifest_url.get()); | 59 urls.back().Swap(manifest_url.get()); |
| 52 } | 60 } |
| 53 } | 61 } |
| 54 return urls; | 62 return urls; |
| 55 } | 63 } |
| 56 | 64 |
| 65 // A www. prefix is not informative and thus not worth the limited real estate |
| 66 // in the permissions UI. |
| 67 std::string RemoveWwwPrefix(const std::string& url) { |
| 68 if (StartsWithASCII(url, kWwwPrefix, false)) |
| 69 return url.substr(strlen(kWwwPrefix)); |
| 70 return url; |
| 71 } |
| 72 |
| 57 } // namespace | 73 } // namespace |
| 58 | 74 |
| 59 SettingsOverrides::SettingsOverrides() {} | 75 SettingsOverrides::SettingsOverrides() {} |
| 60 | 76 |
| 61 SettingsOverrides::~SettingsOverrides() {} | 77 SettingsOverrides::~SettingsOverrides() {} |
| 62 | 78 |
| 63 const SettingsOverrides* SettingsOverrides::Get( | 79 const SettingsOverrides* SettingsOverrides::Get( |
| 64 const Extension* extension) { | 80 const Extension* extension) { |
| 65 return static_cast<SettingsOverrides*>( | 81 return static_cast<SettingsOverrides*>( |
| 66 extension->GetManifestData(manifest_keys::kSettingsOverride)); | 82 extension->GetManifestData(manifest_keys::kSettingsOverride)); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 79 return false; | 95 return false; |
| 80 | 96 |
| 81 scoped_ptr<SettingsOverrides> info(new SettingsOverrides); | 97 scoped_ptr<SettingsOverrides> info(new SettingsOverrides); |
| 82 info->homepage = ParseHomepage(*settings, error); | 98 info->homepage = ParseHomepage(*settings, error); |
| 83 info->search_engine = settings->search_provider.Pass(); | 99 info->search_engine = settings->search_provider.Pass(); |
| 84 info->startup_pages = ParseStartupPage(*settings, error); | 100 info->startup_pages = ParseStartupPage(*settings, error); |
| 85 if (!info->homepage && !info->search_engine && info->startup_pages.empty()) { | 101 if (!info->homepage && !info->search_engine && info->startup_pages.empty()) { |
| 86 *error = ASCIIToUTF16(manifest_errors::kInvalidEmptySettingsOverrides); | 102 *error = ASCIIToUTF16(manifest_errors::kInvalidEmptySettingsOverrides); |
| 87 return false; | 103 return false; |
| 88 } | 104 } |
| 105 APIPermissionSet* permission_set = |
| 106 PermissionsData::GetInitialAPIPermissions(extension); |
| 107 DCHECK(permission_set); |
| 108 if (info->search_engine) { |
| 109 permission_set->insert(new SettingsOverrideAPIPermission( |
| 110 PermissionsInfo::GetInstance()->GetByID(APIPermission::kSearchProvider), |
| 111 RemoveWwwPrefix(CreateManifestURL(info->search_engine->search_url)-> |
| 112 GetOrigin().host()))); |
| 113 } |
| 114 if (!info->startup_pages.empty()) { |
| 115 permission_set->insert(new SettingsOverrideAPIPermission( |
| 116 PermissionsInfo::GetInstance()->GetByID(APIPermission::kStartupPages), |
| 117 // We only support one startup page even though the type of the manifest |
| 118 // property is a list, only the first one is used. |
| 119 RemoveWwwPrefix(info->startup_pages[0].GetContent()))); |
| 120 } |
| 121 if (info->homepage) { |
| 122 permission_set->insert(new SettingsOverrideAPIPermission( |
| 123 PermissionsInfo::GetInstance()->GetByID(APIPermission::kHomePage), |
| 124 RemoveWwwPrefix(info->homepage.get()->GetContent()))); |
| 125 } |
| 89 extension->SetManifestData(manifest_keys::kSettingsOverride, | 126 extension->SetManifestData(manifest_keys::kSettingsOverride, |
| 90 info.release()); | 127 info.release()); |
| 91 return true; | 128 return true; |
| 92 } | 129 } |
| 93 | 130 |
| 94 const std::vector<std::string> SettingsOverridesHandler::Keys() const { | 131 const std::vector<std::string> SettingsOverridesHandler::Keys() const { |
| 95 return SingleKey(manifest_keys::kSettingsOverride); | 132 return SingleKey(manifest_keys::kSettingsOverride); |
| 96 } | 133 } |
| 97 | 134 |
| 98 } // namespace extensions | 135 } // namespace extensions |
| OLD | NEW |