Skip to content

Commit

Permalink
Add chrome.i18nPrivate.getStrings for translations in component exten…
Browse files Browse the repository at this point in the history
…sions

This common API will allow the deduplication of several existing
getStrings APIs. This CL also replaces the existing
chrome.identityPrivate.getStrings API with the common API.

BUG=529546
NOPRESUBMIT=true
(Disabling to allow a rename in the histogram enum)

Review URL: https://codereview.chromium.org/1319293004

Cr-Commit-Position: refs/heads/master@{#349341}
  • Loading branch information
tgsergeant authored and Commit bot committed Sep 17, 2015
1 parent 47d5ce8 commit e420a14
Show file tree
Hide file tree
Showing 23 changed files with 183 additions and 74 deletions.

This file was deleted.

This file was deleted.

1 change: 1 addition & 0 deletions chrome/browser/extensions/api/resources_private/OWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tsergeant@chromium.org
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/api/resources_private/resources_private_api.h"

#include <string>

#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/common/extensions/api/resources_private.h"
#include "chrome/grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/webui/web_ui_util.h"

// To add a new component to this API, simply:
// 1. Add your component to the Component enum in
// chrome/common/extensions/api/resources_private.idl
// 2. Create an AddStringsForMyComponent(base::DictionaryValue * dict) method.
// 3. Tie in that method to the switch statement in Run()

namespace extensions {

namespace {

void AddStringsForIdentity(base::DictionaryValue* dict) {
dict->SetString("window-title",
l10n_util::GetStringUTF16(IDS_EXTENSION_CONFIRM_PERMISSIONS));
}

} // namespace

namespace get_strings = api::resources_private::GetStrings;

ResourcesPrivateGetStringsFunction::ResourcesPrivateGetStringsFunction() {}

ResourcesPrivateGetStringsFunction::~ResourcesPrivateGetStringsFunction() {}

ExtensionFunction::ResponseAction ResourcesPrivateGetStringsFunction::Run() {
scoped_ptr<get_strings::Params> params(get_strings::Params::Create(*args_));
scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);

api::resources_private::Component component = params->component;

switch (component) {
case api::resources_private::COMPONENT_IDENTITY:
AddStringsForIdentity(dict.get());
break;
case api::resources_private::COMPONENT_NONE:
NOTREACHED();
}

const std::string& app_locale = g_browser_process->GetApplicationLocale();
webui::SetLoadTimeDataDefaults(app_locale, dict.get());

return RespondNow(OneArgument(dict.Pass()));
}

} // namespace extensions
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_EXTENSIONS_API_RESOURCES_PRIVATE_RESOURCES_PRIVATE_API_H_
#define CHROME_BROWSER_EXTENSIONS_API_RESOURCES_PRIVATE_RESOURCES_PRIVATE_API_H_

#include "extensions/browser/extension_function.h"

namespace extensions {

class ResourcesPrivateGetStringsFunction : public UIThreadExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("resourcesPrivate.getStrings",
RESOURCESPRIVATE_GETSTRINGS);
ResourcesPrivateGetStringsFunction();

protected:
~ResourcesPrivateGetStringsFunction() override;

// Override from ExtensionFunction:
ExtensionFunction::ResponseAction Run() override;

private:
DISALLOW_COPY_AND_ASSIGN(ResourcesPrivateGetStringsFunction);
};

} // namespace extensions

#endif // CHROME_BROWSER_EXTENSIONS_API_RESOURCES_PRIVATE_RESOURCES_PRIVATE_API_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/extension_apitest.h"

class ResourcesPrivateApiTest : public ExtensionApiTest {
public:
ResourcesPrivateApiTest() {}

private:
DISALLOW_COPY_AND_ASSIGN(ResourcesPrivateApiTest);
};

IN_PROC_BROWSER_TEST_F(ResourcesPrivateApiTest, GetStrings) {
ASSERT_TRUE(RunComponentExtensionTest("resources_private/get_strings"));
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"permissions": [
"chrome://theme/",
"identityPrivate",
"resourcesPrivate",
"webview"
],

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ document.addEventListener('DOMContentLoaded', function() {
window.close();
};

chrome.identityPrivate.getStrings(function(strings) {
chrome.resourcesPrivate.getStrings('identity', function(strings) {
document.title = strings['window-title'];
});
});
Expand Down
4 changes: 2 additions & 2 deletions chrome/chrome_browser_extensions.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,6 @@
'browser/extensions/api/identity/identity_signin_flow.h',
'browser/extensions/api/identity/web_auth_flow.cc',
'browser/extensions/api/identity/web_auth_flow.h',
'browser/extensions/api/identity_private/identity_private_api.cc',
'browser/extensions/api/identity_private/identity_private_api.h',
'browser/extensions/api/idltest/idltest_api.cc',
'browser/extensions/api/idltest/idltest_api.h',
'browser/extensions/api/image_writer_private/destroy_partitions_operation.cc',
Expand Down Expand Up @@ -441,6 +439,8 @@
'browser/extensions/api/proxy/proxy_api_helpers.h',
'browser/extensions/api/reading_list_private/reading_list_private_api.cc',
'browser/extensions/api/reading_list_private/reading_list_private_api.h',
'browser/extensions/api/resources_private/resources_private_api.cc',
'browser/extensions/api/resources_private/resources_private_api.h',
'browser/extensions/api/runtime/chrome_runtime_api_delegate.cc',
'browser/extensions/api/runtime/chrome_runtime_api_delegate.h',
'browser/extensions/api/screenlock_private/screenlock_private_api.cc',
Expand Down
1 change: 1 addition & 0 deletions chrome/chrome_tests.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@
'browser/extensions/api/processes/processes_apitest.cc',
'browser/extensions/api/proxy/proxy_apitest.cc',
'browser/extensions/api/reading_list_private/reading_list_private_apitest.cc',
'browser/extensions/api/resources_private/resources_private_apitest.cc',
'browser/extensions/api/screenlock_private/screenlock_private_apitest.cc',
'browser/extensions/api/search_engines_private/search_engines_private_apitest.cc',
'browser/extensions/api/sessions/sessions_apitest.cc',
Expand Down
4 changes: 4 additions & 0 deletions chrome/common/extensions/api/_api_features.json
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,10 @@
"dependencies": ["permission:readingListPrivate"],
"contexts": ["blessed_extension"]
},
"resourcesPrivate": {
"dependencies": ["permission:resourcesPrivate"],
"contexts": ["blessed_extension"]
},
"rtcPrivate": {
"dependencies": ["permission:rtcPrivate"],
"contexts": ["blessed_extension"]
Expand Down
7 changes: 7 additions & 0 deletions chrome/common/extensions/api/_permission_features.json
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,13 @@
"3F65507A3B39259B38C8173C6FFA3D12DF64CCE9" // http://crbug.com/371562
]
},
"resourcesPrivate": {
"channel": "stable",
"extension_types": [
"extension", "legacy_packaged_app", "platform_app"
],
"location": "component"
},
"rtcPrivate": {
"channel": "stable",
"extension_types": ["extension", "legacy_packaged_app"],
Expand Down
9 changes: 0 additions & 9 deletions chrome/common/extensions/api/identity_private.idl
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@
// identityPrivate.
namespace identityPrivate {

callback GetStringsCallback = void (object result);

interface Functions {
// Gets localized strings required to render the API.
//
// |callback| : Called with a dictionary mapping names to resource strings.
static void getStrings(GetStringsCallback callback);
};

interface Events {
// Fired when a web flow dialog should be displayed.
static void onWebFlowRequest(DOMString key, DOMString url, DOMString mode);
Expand Down
23 changes: 23 additions & 0 deletions chrome/common/extensions/api/resources_private.idl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// resourcesPrivate.
namespace resourcesPrivate {
enum Component { identity };

callback GetStringsCallback = void (object result);

interface Functions {
// Gets localized strings for a component extension. Includes default WebUI
// loadTimeData values for text and language settings (fontsize, fontfamily,
// language, textdirection). See
// chrome/browser/extensions/api/resources_private/resources_private_api.cc
// for instructions on adding a new component to this API.
//
// |component| : Internal chrome component to get strings for.
// |callback| : Called with a dictionary mapping names to strings.
static void getStrings(Component component,
GetStringsCallback callback);
};
};
1 change: 1 addition & 0 deletions chrome/common/extensions/api/schemas.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
'permissions.json',
'preferences_private.json',
'reading_list_private.json',
'resources_private.idl',
'screenlock_private.idl',
'search_engines_private.idl',
'sessions.json',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ std::vector<APIPermissionInfo*> ChromeAPIPermissions::GetAllPermissions()
APIPermissionInfo::kFlagCannotBeOptional},
{APIPermission::kLanguageSettingsPrivate, "languageSettingsPrivate",
APIPermissionInfo::kFlagCannotBeOptional},
{APIPermission::kResourcesPrivate, "resourcesPrivate",
APIPermissionInfo::kFlagCannotBeOptional},

// Full url access permissions.
{APIPermission::kDebugger, "debugger",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,7 @@ TEST(PermissionsTest, PermissionMessages) {
skip.insert(APIPermission::kPrincipalsPrivate);
skip.insert(APIPermission::kImageWriterPrivate);
skip.insert(APIPermission::kReadingListPrivate);
skip.insert(APIPermission::kResourcesPrivate);
skip.insert(APIPermission::kRtcPrivate);
skip.insert(APIPermission::kStreamsPrivate);
skip.insert(APIPermission::kSystemPrivate);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

chrome.test.runTests([
function testGetStrings() {
chrome.resourcesPrivate.getStrings(
"identity", chrome.test.callbackPass(function(data) {
// String from the identity component.
chrome.test.assertTrue('window-title' in data);
// String from the loadTimeData defaults.
chrome.test.assertTrue('language' in data);
chrome.test.succeed();
}));
}
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDHKqRS2RUwmWJCzsOFjSRfd7f69w/yEWuddTD61QwwfeOpaA2+ZiQugaMb5HpLCbFNVBwHXKO1UxWF18YPHYMzMtDfbgjIxtQg3YRPCuicHR0qoo4jfzn9gY+6KW4csS4AH6/Tj700s5SvG25Ty6P6v9p9ZHPEVX9DeYeSllCWdwIDAQAB",
"name": "chrome.resourcesPrivate.getStrings test",
"version": "1",
"manifest_version": 2,
"background": {
"scripts": ["background.js"]
},
"permissions": ["resourcesPrivate"]
}
3 changes: 2 additions & 1 deletion extensions/browser/extension_function_histogram_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ enum HistogramValue {
RUNTIME_GETPLATFORMINFO,
DELETED_WEBVIEW_INSERTCSS,
METRICSPRIVATE_GETISCRASHRECORDINGENABLED,
IDENTITYPRIVATE_GETSTRINGS,
DELETED_IDENTITYPRIVATE_GETSTRINGS,
NOTIFICATIONS_GET_ALL,
USB_LISTINTERFACES,
FILESYSTEM_RETAINENTRY,
Expand Down Expand Up @@ -1142,6 +1142,7 @@ enum HistogramValue {
CERTIFICATEPROVIDERINTERNAL_REPORTCERTIFICATES,
BLUETOOTHPRIVATE_PAIR,
EASYUNLOCKPRIVATE_FINDSETUPCONNECTION,
RESOURCESPRIVATE_GETSTRINGS,
// Last entry: Add new entries above, then run:
// python tools/metrics/histograms/update_extension_histograms.py
ENUM_BOUNDARY
Expand Down
1 change: 1 addition & 0 deletions extensions/common/permissions/api_permission.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ class APIPermission {
kLanguageSettingsPrivate,
kEnterpriseDeviceAttributes,
kCertificateProvider,
kResourcesPrivate,
// Last entry: Add new entries above and ensure to update the
// "ExtensionPermission3" enum in tools/metrics/histograms/histograms.xml
// (by running update_extension_permission.py).
Expand Down
5 changes: 4 additions & 1 deletion tools/metrics/histograms/histograms.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58659,7 +58659,7 @@ http://cs/file:chrome/histograms.xml - but prefer this file for new entries.
<int value="467" label="RUNTIME_GETPLATFORMINFO"/>
<int value="468" label="DELETED_WEBVIEW_INSERTCSS"/>
<int value="469" label="METRICSPRIVATE_GETISCRASHRECORDINGENABLED"/>
<int value="470" label="IDENTITYPRIVATE_GETSTRINGS"/>
<int value="470" label="DELETED_IDENTITYPRIVATE_GETSTRINGS"/>
<int value="471" label="NOTIFICATIONS_GET_ALL"/>
<int value="472" label="USB_LISTINTERFACES"/>
<int value="473" label="FILESYSTEM_RETAINENTRY"/>
Expand Down Expand Up @@ -59296,6 +59296,7 @@ http://cs/file:chrome/histograms.xml - but prefer this file for new entries.
<int value="1081" label="CERTIFICATEPROVIDERINTERNAL_REPORTCERTIFICATES"/>
<int value="1082" label="BLUETOOTHPRIVATE_PAIR"/>
<int value="1083" label="EASYUNLOCKPRIVATE_FINDSETUPCONNECTION"/>
<int value="1084" label="RESOURCESPRIVATE_GETSTRINGS"/>
</enum>

<enum name="ExtensionInstallCause" type="int">
Expand Down Expand Up @@ -59701,6 +59702,8 @@ http://cs/file:chrome/histograms.xml - but prefer this file for new entries.
<int value="194" label="kPasswordsPrivate"/>
<int value="195" label="kLanguageSettingsPrivate"/>
<int value="196" label="kEnterpriseDeviceAttributes"/>
<int value="197" label="kCertificateProvider"/>
<int value="198" label="kResourcesPrivate"/>
</enum>

<enum name="ExtensionServiceVerifyAllSuccess" type="int">
Expand Down

0 comments on commit e420a14

Please sign in to comment.