Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(358)

Side by Side Diff: extensions/renderer/i18n_custom_bindings.cc

Issue 1899083003: Convert //extensions/renderer from scoped_ptr to std::unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "extensions/renderer/i18n_custom_bindings.h" 5 #include "extensions/renderer/i18n_custom_bindings.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/macros.h" 13 #include "base/macros.h"
14 #include "base/memory/ptr_util.h"
14 #include "content/public/child/v8_value_converter.h" 15 #include "content/public/child/v8_value_converter.h"
15 #include "content/public/renderer/render_frame.h" 16 #include "content/public/renderer/render_frame.h"
16 #include "content/public/renderer/render_thread.h" 17 #include "content/public/renderer/render_thread.h"
17 #include "extensions/common/extension_messages.h" 18 #include "extensions/common/extension_messages.h"
18 #include "extensions/common/message_bundle.h" 19 #include "extensions/common/message_bundle.h"
19 #include "extensions/renderer/script_context.h" 20 #include "extensions/renderer/script_context.h"
20 #include "extensions/renderer/v8_helpers.h" 21 #include "extensions/renderer/v8_helpers.h"
21 #include "third_party/cld_2/src/public/compact_lang_det.h" 22 #include "third_party/cld_2/src/public/compact_lang_det.h"
22 #include "third_party/cld_2/src/public/encodings.h" 23 #include "third_party/cld_2/src/public/encodings.h"
23 24
24 namespace extensions { 25 namespace extensions {
25 26
26 using namespace v8_helpers; 27 using namespace v8_helpers;
27 28
28 namespace { 29 namespace {
29 30
30 // Max number of languages detected by CLD2. 31 // Max number of languages detected by CLD2.
31 const int kCldNumLangs = 3; 32 const int kCldNumLangs = 3;
32 33
33 struct DetectedLanguage { 34 struct DetectedLanguage {
34 DetectedLanguage(const std::string& language, int percentage) 35 DetectedLanguage(const std::string& language, int percentage)
35 : language(language), percentage(percentage) {} 36 : language(language), percentage(percentage) {}
36 ~DetectedLanguage() {} 37 ~DetectedLanguage() {}
37 38
38 // Returns a new v8::Local<v8::Value> representing the serialized form of 39 // Returns a new v8::Local<v8::Value> representing the serialized form of
39 // this DetectedLanguage object. 40 // this DetectedLanguage object.
40 scoped_ptr<base::DictionaryValue> ToDictionary() const; 41 std::unique_ptr<base::DictionaryValue> ToDictionary() const;
41 42
42 std::string language; 43 std::string language;
43 int percentage; 44 int percentage;
44 45
45 private: 46 private:
46 DISALLOW_COPY_AND_ASSIGN(DetectedLanguage); 47 DISALLOW_COPY_AND_ASSIGN(DetectedLanguage);
47 }; 48 };
48 49
49 // LanguageDetectionResult object that holds detected langugae reliability and 50 // LanguageDetectionResult object that holds detected langugae reliability and
50 // array of DetectedLanguage 51 // array of DetectedLanguage
51 struct LanguageDetectionResult { 52 struct LanguageDetectionResult {
52 explicit LanguageDetectionResult(bool is_reliable) 53 explicit LanguageDetectionResult(bool is_reliable)
53 : is_reliable(is_reliable) {} 54 : is_reliable(is_reliable) {}
54 ~LanguageDetectionResult() {} 55 ~LanguageDetectionResult() {}
55 56
56 // Returns a new v8::Local<v8::Value> representing the serialized form of 57 // Returns a new v8::Local<v8::Value> representing the serialized form of
57 // this Result object. 58 // this Result object.
58 v8::Local<v8::Value> ToValue(ScriptContext* context); 59 v8::Local<v8::Value> ToValue(ScriptContext* context);
59 60
60 // CLD detected language reliability 61 // CLD detected language reliability
61 bool is_reliable; 62 bool is_reliable;
62 63
63 // Array of detectedLanguage of size 1-3. The null is returned if 64 // Array of detectedLanguage of size 1-3. The null is returned if
64 // there were no languages detected 65 // there were no languages detected
65 std::vector<scoped_ptr<DetectedLanguage>> languages; 66 std::vector<std::unique_ptr<DetectedLanguage>> languages;
66 67
67 private: 68 private:
68 DISALLOW_COPY_AND_ASSIGN(LanguageDetectionResult); 69 DISALLOW_COPY_AND_ASSIGN(LanguageDetectionResult);
69 }; 70 };
70 71
71 scoped_ptr<base::DictionaryValue> DetectedLanguage::ToDictionary() const { 72 std::unique_ptr<base::DictionaryValue> DetectedLanguage::ToDictionary() const {
72 scoped_ptr<base::DictionaryValue> dict_value(new base::DictionaryValue()); 73 std::unique_ptr<base::DictionaryValue> dict_value(
74 new base::DictionaryValue());
73 dict_value->SetString("language", language.c_str()); 75 dict_value->SetString("language", language.c_str());
74 dict_value->SetInteger("percentage", percentage); 76 dict_value->SetInteger("percentage", percentage);
75 return dict_value; 77 return dict_value;
76 } 78 }
77 79
78 v8::Local<v8::Value> LanguageDetectionResult::ToValue(ScriptContext* context) { 80 v8::Local<v8::Value> LanguageDetectionResult::ToValue(ScriptContext* context) {
79 base::DictionaryValue dict_value; 81 base::DictionaryValue dict_value;
80 dict_value.SetBoolean("isReliable", is_reliable); 82 dict_value.SetBoolean("isReliable", is_reliable);
81 scoped_ptr<base::ListValue> languages_list(new base::ListValue()); 83 std::unique_ptr<base::ListValue> languages_list(new base::ListValue());
82 for (const auto& language : languages) 84 for (const auto& language : languages)
83 languages_list->Append(language->ToDictionary()); 85 languages_list->Append(language->ToDictionary());
84 dict_value.Set("languages", std::move(languages_list)); 86 dict_value.Set("languages", std::move(languages_list));
85 87
86 v8::Local<v8::Context> v8_context = context->v8_context(); 88 v8::Local<v8::Context> v8_context = context->v8_context();
87 v8::Isolate* isolate = v8_context->GetIsolate(); 89 v8::Isolate* isolate = v8_context->GetIsolate();
88 v8::EscapableHandleScope handle_scope(isolate); 90 v8::EscapableHandleScope handle_scope(isolate);
89 91
90 scoped_ptr<content::V8ValueConverter> converter( 92 std::unique_ptr<content::V8ValueConverter> converter(
91 content::V8ValueConverter::create()); 93 content::V8ValueConverter::create());
92 v8::Local<v8::Value> result = converter->ToV8Value(&dict_value, v8_context); 94 v8::Local<v8::Value> result = converter->ToV8Value(&dict_value, v8_context);
93 return handle_scope.Escape(result); 95 return handle_scope.Escape(result);
94 } 96 }
95 97
96 void InitDetectedLanguages( 98 void InitDetectedLanguages(
97 CLD2::Language* languages, 99 CLD2::Language* languages,
98 int* percents, 100 int* percents,
99 std::vector<scoped_ptr<DetectedLanguage>>* detected_languages) { 101 std::vector<std::unique_ptr<DetectedLanguage>>* detected_languages) {
100 for (int i = 0; i < kCldNumLangs; i++) { 102 for (int i = 0; i < kCldNumLangs; i++) {
101 std::string language_code; 103 std::string language_code;
102 // Convert LanguageCode 'zh' to 'zh-CN' and 'zh-Hant' to 'zh-TW' for 104 // Convert LanguageCode 'zh' to 'zh-CN' and 'zh-Hant' to 'zh-TW' for
103 // Translate server usage. see DetermineTextLanguage in 105 // Translate server usage. see DetermineTextLanguage in
104 // components/translate/core/language_detection/language_detection_util.cc 106 // components/translate/core/language_detection/language_detection_util.cc
105 if (languages[i] == CLD2::UNKNOWN_LANGUAGE) { 107 if (languages[i] == CLD2::UNKNOWN_LANGUAGE) {
106 // Break from the loop since there is no need to save 108 // Break from the loop since there is no need to save
107 // unknown languages 109 // unknown languages
108 break; 110 break;
109 } else { 111 } else {
110 language_code = 112 language_code =
111 CLD2::LanguageCode(static_cast<CLD2::Language>(languages[i])); 113 CLD2::LanguageCode(static_cast<CLD2::Language>(languages[i]));
112 } 114 }
113 detected_languages->push_back( 115 detected_languages->push_back(
114 make_scoped_ptr(new DetectedLanguage(language_code, percents[i]))); 116 base::WrapUnique(new DetectedLanguage(language_code, percents[i])));
115 } 117 }
116 } 118 }
117 119
118 } // namespace 120 } // namespace
119 121
120 I18NCustomBindings::I18NCustomBindings(ScriptContext* context) 122 I18NCustomBindings::I18NCustomBindings(ScriptContext* context)
121 : ObjectBackedNativeHandler(context) { 123 : ObjectBackedNativeHandler(context) {
122 RouteFunction( 124 RouteFunction(
123 "GetL10nMessage", "i18n", 125 "GetL10nMessage", "i18n",
124 base::Bind(&I18NCustomBindings::GetL10nMessage, base::Unretained(this))); 126 base::Bind(&I18NCustomBindings::GetL10nMessage, base::Unretained(this)));
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 } 238 }
237 239
238 LanguageDetectionResult result(is_reliable); 240 LanguageDetectionResult result(is_reliable);
239 // populate LanguageDetectionResult with languages and percents 241 // populate LanguageDetectionResult with languages and percents
240 InitDetectedLanguages(languages, percents, &result.languages); 242 InitDetectedLanguages(languages, percents, &result.languages);
241 243
242 args.GetReturnValue().Set(result.ToValue(context())); 244 args.GetReturnValue().Set(result.ToValue(context()));
243 } 245 }
244 246
245 } // namespace extensions 247 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698