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 "webkit/child/webkitplatformsupport_impl.h"
11 #include "base/allocator/allocator_extension.h"
12 #include "base/bind.h"
13 #include "base/files/file_path.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/singleton.h"
16 #include "base/message_loop/message_loop.h"
17 #include "base/metrics/histogram.h"
18 #include "base/metrics/sparse_histogram.h"
19 #include "base/metrics/stats_counters.h"
20 #include "base/platform_file.h"
21 #include "base/process/process_metrics.h"
22 #include "base/rand_util.h"
23 #include "base/strings/string_number_conversions.h"
24 #include "base/strings/string_util.h"
25 #include "base/strings/utf_string_conversions.h"
26 #include "base/synchronization/lock.h"
27 #include "base/sys_info.h"
28 #include "base/time/time.h"
29 #include "grit/blink_resources.h"
30 #include "grit/webkit_resources.h"
31 #include "grit/webkit_strings.h"
32 #include "net/base/data_url.h"
33 #include "net/base/mime_util.h"
34 #include "net/base/net_errors.h"
35 #include "third_party/WebKit/public/platform/WebData.h"
36 #include "third_party/WebKit/public/platform/WebString.h"
37 #include "ui/base/layout.h"
38 #include "webkit/child/webkit_child_helpers.h"
39 #include "webkit/child/websocketstreamhandle_impl.h"
40 #include "webkit/child/weburlloader_impl.h"
41 #include "webkit/common/user_agent/user_agent.h"
43 #if defined(OS_ANDROID)
44 #include "base/android/sys_utils.h"
47 #if !defined(NO_TCMALLOC) && defined(USE_TCMALLOC) && !defined(OS_WIN)
48 #include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h"
52 using blink::WebLocalizedString;
53 using blink::WebString;
54 using blink::WebSocketStreamHandle;
56 using blink::WebURLError;
57 using blink::WebURLLoader;
61 // A simple class to cache the memory usage for a given amount of time.
62 class MemoryUsageCache {
64 // Retrieves the Singleton.
65 static MemoryUsageCache* GetInstance() {
66 return Singleton<MemoryUsageCache>::get();
69 MemoryUsageCache() : memory_value_(0) { Init(); }
70 ~MemoryUsageCache() {}
73 const unsigned int kCacheSeconds = 1;
74 cache_valid_time_ = base::TimeDelta::FromSeconds(kCacheSeconds);
77 // Returns true if the cached value is fresh.
78 // Returns false if the cached value is stale, or if |cached_value| is NULL.
79 bool IsCachedValueValid(size_t* cached_value) {
80 base::AutoLock scoped_lock(lock_);
83 if (base::Time::Now() - last_updated_time_ > cache_valid_time_)
85 *cached_value = memory_value_;
89 // Setter for |memory_value_|, refreshes |last_updated_time_|.
90 void SetMemoryValue(const size_t value) {
91 base::AutoLock scoped_lock(lock_);
92 memory_value_ = value;
93 last_updated_time_ = base::Time::Now();
97 // The cached memory value.
100 // How long the cached value should remain valid.
101 base::TimeDelta cache_valid_time_;
103 // The last time the cached value was updated.
104 base::Time last_updated_time_;
109 } // anonymous namespace
111 namespace webkit_glue {
113 static int ToMessageID(WebLocalizedString::Name name) {
115 case WebLocalizedString::AXAMPMFieldText:
116 return IDS_AX_AM_PM_FIELD_TEXT;
117 case WebLocalizedString::AXButtonActionVerb:
118 return IDS_AX_BUTTON_ACTION_VERB;
119 case WebLocalizedString::AXCheckedCheckBoxActionVerb:
120 return IDS_AX_CHECKED_CHECK_BOX_ACTION_VERB;
121 case WebLocalizedString::AXDateTimeFieldEmptyValueText:
122 return IDS_AX_DATE_TIME_FIELD_EMPTY_VALUE_TEXT;
123 case WebLocalizedString::AXDayOfMonthFieldText:
124 return IDS_AX_DAY_OF_MONTH_FIELD_TEXT;
125 case WebLocalizedString::AXHeadingText:
126 return IDS_AX_ROLE_HEADING;
127 case WebLocalizedString::AXHourFieldText:
128 return IDS_AX_HOUR_FIELD_TEXT;
129 case WebLocalizedString::AXImageMapText:
130 return IDS_AX_ROLE_IMAGE_MAP;
131 case WebLocalizedString::AXLinkActionVerb:
132 return IDS_AX_LINK_ACTION_VERB;
133 case WebLocalizedString::AXLinkText:
134 return IDS_AX_ROLE_LINK;
135 case WebLocalizedString::AXListMarkerText:
136 return IDS_AX_ROLE_LIST_MARKER;
137 case WebLocalizedString::AXMediaDefault:
138 return IDS_AX_MEDIA_DEFAULT;
139 case WebLocalizedString::AXMediaAudioElement:
140 return IDS_AX_MEDIA_AUDIO_ELEMENT;
141 case WebLocalizedString::AXMediaVideoElement:
142 return IDS_AX_MEDIA_VIDEO_ELEMENT;
143 case WebLocalizedString::AXMediaMuteButton:
144 return IDS_AX_MEDIA_MUTE_BUTTON;
145 case WebLocalizedString::AXMediaUnMuteButton:
146 return IDS_AX_MEDIA_UNMUTE_BUTTON;
147 case WebLocalizedString::AXMediaPlayButton:
148 return IDS_AX_MEDIA_PLAY_BUTTON;
149 case WebLocalizedString::AXMediaPauseButton:
150 return IDS_AX_MEDIA_PAUSE_BUTTON;
151 case WebLocalizedString::AXMediaSlider:
152 return IDS_AX_MEDIA_SLIDER;
153 case WebLocalizedString::AXMediaSliderThumb:
154 return IDS_AX_MEDIA_SLIDER_THUMB;
155 case WebLocalizedString::AXMediaCurrentTimeDisplay:
156 return IDS_AX_MEDIA_CURRENT_TIME_DISPLAY;
157 case WebLocalizedString::AXMediaTimeRemainingDisplay:
158 return IDS_AX_MEDIA_TIME_REMAINING_DISPLAY;
159 case WebLocalizedString::AXMediaStatusDisplay:
160 return IDS_AX_MEDIA_STATUS_DISPLAY;
161 case WebLocalizedString::AXMediaEnterFullscreenButton:
162 return IDS_AX_MEDIA_ENTER_FULL_SCREEN_BUTTON;
163 case WebLocalizedString::AXMediaExitFullscreenButton:
164 return IDS_AX_MEDIA_EXIT_FULL_SCREEN_BUTTON;
165 case WebLocalizedString::AXMediaShowClosedCaptionsButton:
166 return IDS_AX_MEDIA_SHOW_CLOSED_CAPTIONS_BUTTON;
167 case WebLocalizedString::AXMediaHideClosedCaptionsButton:
168 return IDS_AX_MEDIA_HIDE_CLOSED_CAPTIONS_BUTTON;
169 case WebLocalizedString::AXMediaAudioElementHelp:
170 return IDS_AX_MEDIA_AUDIO_ELEMENT_HELP;
171 case WebLocalizedString::AXMediaVideoElementHelp:
172 return IDS_AX_MEDIA_VIDEO_ELEMENT_HELP;
173 case WebLocalizedString::AXMediaMuteButtonHelp:
174 return IDS_AX_MEDIA_MUTE_BUTTON_HELP;
175 case WebLocalizedString::AXMediaUnMuteButtonHelp:
176 return IDS_AX_MEDIA_UNMUTE_BUTTON_HELP;
177 case WebLocalizedString::AXMediaPlayButtonHelp:
178 return IDS_AX_MEDIA_PLAY_BUTTON_HELP;
179 case WebLocalizedString::AXMediaPauseButtonHelp:
180 return IDS_AX_MEDIA_PAUSE_BUTTON_HELP;
181 case WebLocalizedString::AXMediaSliderHelp:
182 return IDS_AX_MEDIA_SLIDER_HELP;
183 case WebLocalizedString::AXMediaSliderThumbHelp:
184 return IDS_AX_MEDIA_SLIDER_THUMB_HELP;
185 case WebLocalizedString::AXMediaCurrentTimeDisplayHelp:
186 return IDS_AX_MEDIA_CURRENT_TIME_DISPLAY_HELP;
187 case WebLocalizedString::AXMediaTimeRemainingDisplayHelp:
188 return IDS_AX_MEDIA_TIME_REMAINING_DISPLAY_HELP;
189 case WebLocalizedString::AXMediaStatusDisplayHelp:
190 return IDS_AX_MEDIA_STATUS_DISPLAY_HELP;
191 case WebLocalizedString::AXMediaEnterFullscreenButtonHelp:
192 return IDS_AX_MEDIA_ENTER_FULL_SCREEN_BUTTON_HELP;
193 case WebLocalizedString::AXMediaExitFullscreenButtonHelp:
194 return IDS_AX_MEDIA_EXIT_FULL_SCREEN_BUTTON_HELP;
195 case WebLocalizedString::AXMediaShowClosedCaptionsButtonHelp:
196 return IDS_AX_MEDIA_SHOW_CLOSED_CAPTIONS_BUTTON_HELP;
197 case WebLocalizedString::AXMediaHideClosedCaptionsButtonHelp:
198 return IDS_AX_MEDIA_HIDE_CLOSED_CAPTIONS_BUTTON_HELP;
199 case WebLocalizedString::AXMillisecondFieldText:
200 return IDS_AX_MILLISECOND_FIELD_TEXT;
201 case WebLocalizedString::AXMinuteFieldText:
202 return IDS_AX_MINUTE_FIELD_TEXT;
203 case WebLocalizedString::AXMonthFieldText:
204 return IDS_AX_MONTH_FIELD_TEXT;
205 case WebLocalizedString::AXRadioButtonActionVerb:
206 return IDS_AX_RADIO_BUTTON_ACTION_VERB;
207 case WebLocalizedString::AXSecondFieldText:
208 return IDS_AX_SECOND_FIELD_TEXT;
209 case WebLocalizedString::AXTextFieldActionVerb:
210 return IDS_AX_TEXT_FIELD_ACTION_VERB;
211 case WebLocalizedString::AXUncheckedCheckBoxActionVerb:
212 return IDS_AX_UNCHECKED_CHECK_BOX_ACTION_VERB;
213 case WebLocalizedString::AXWebAreaText:
214 return IDS_AX_ROLE_WEB_AREA;
215 case WebLocalizedString::AXWeekOfYearFieldText:
216 return IDS_AX_WEEK_OF_YEAR_FIELD_TEXT;
217 case WebLocalizedString::AXYearFieldText:
218 return IDS_AX_YEAR_FIELD_TEXT;
219 case WebLocalizedString::CalendarClear:
220 return IDS_FORM_CALENDAR_CLEAR;
221 case WebLocalizedString::CalendarToday:
222 return IDS_FORM_CALENDAR_TODAY;
223 case WebLocalizedString::DateFormatDayInMonthLabel:
224 return IDS_FORM_DATE_FORMAT_DAY_IN_MONTH;
225 case WebLocalizedString::DateFormatMonthLabel:
226 return IDS_FORM_DATE_FORMAT_MONTH;
227 case WebLocalizedString::DateFormatYearLabel:
228 return IDS_FORM_DATE_FORMAT_YEAR;
229 case WebLocalizedString::DetailsLabel:
230 return IDS_DETAILS_WITHOUT_SUMMARY_LABEL;
231 case WebLocalizedString::FileButtonChooseFileLabel:
232 return IDS_FORM_FILE_BUTTON_LABEL;
233 case WebLocalizedString::FileButtonChooseMultipleFilesLabel:
234 return IDS_FORM_MULTIPLE_FILES_BUTTON_LABEL;
235 case WebLocalizedString::FileButtonNoFileSelectedLabel:
236 return IDS_FORM_FILE_NO_FILE_LABEL;
237 case WebLocalizedString::InputElementAltText:
238 return IDS_FORM_INPUT_ALT;
239 case WebLocalizedString::KeygenMenuHighGradeKeySize:
240 return IDS_KEYGEN_HIGH_GRADE_KEY;
241 case WebLocalizedString::KeygenMenuMediumGradeKeySize:
242 return IDS_KEYGEN_MED_GRADE_KEY;
243 case WebLocalizedString::MissingPluginText:
244 return IDS_PLUGIN_INITIALIZATION_ERROR;
245 case WebLocalizedString::MultipleFileUploadText:
246 return IDS_FORM_FILE_MULTIPLE_UPLOAD;
247 case WebLocalizedString::OtherColorLabel:
248 return IDS_FORM_OTHER_COLOR_LABEL;
249 case WebLocalizedString::OtherDateLabel:
250 return IDS_FORM_OTHER_DATE_LABEL;
251 case WebLocalizedString::OtherMonthLabel:
252 return IDS_FORM_OTHER_MONTH_LABEL;
253 case WebLocalizedString::OtherTimeLabel:
254 return IDS_FORM_OTHER_TIME_LABEL;
255 case WebLocalizedString::OtherWeekLabel:
256 return IDS_FORM_OTHER_WEEK_LABEL;
257 case WebLocalizedString::PlaceholderForDayOfMonthField:
258 return IDS_FORM_PLACEHOLDER_FOR_DAY_OF_MONTH_FIELD;
259 case WebLocalizedString::PlaceholderForMonthField:
260 return IDS_FORM_PLACEHOLDER_FOR_MONTH_FIELD;
261 case WebLocalizedString::PlaceholderForYearField:
262 return IDS_FORM_PLACEHOLDER_FOR_YEAR_FIELD;
263 case WebLocalizedString::ResetButtonDefaultLabel:
264 return IDS_FORM_RESET_LABEL;
265 case WebLocalizedString::SearchableIndexIntroduction:
266 return IDS_SEARCHABLE_INDEX_INTRO;
267 case WebLocalizedString::SearchMenuClearRecentSearchesText:
268 return IDS_RECENT_SEARCHES_CLEAR;
269 case WebLocalizedString::SearchMenuNoRecentSearchesText:
270 return IDS_RECENT_SEARCHES_NONE;
271 case WebLocalizedString::SearchMenuRecentSearchesText:
272 return IDS_RECENT_SEARCHES;
273 case WebLocalizedString::SubmitButtonDefaultLabel:
274 return IDS_FORM_SUBMIT_LABEL;
275 case WebLocalizedString::ThisMonthButtonLabel:
276 return IDS_FORM_THIS_MONTH_LABEL;
277 case WebLocalizedString::ThisWeekButtonLabel:
278 return IDS_FORM_THIS_WEEK_LABEL;
279 case WebLocalizedString::ValidationBadInputForDateTime:
280 return IDS_FORM_VALIDATION_BAD_INPUT_DATETIME;
281 case WebLocalizedString::ValidationBadInputForNumber:
282 return IDS_FORM_VALIDATION_BAD_INPUT_NUMBER;
283 case WebLocalizedString::ValidationPatternMismatch:
284 return IDS_FORM_VALIDATION_PATTERN_MISMATCH;
285 case WebLocalizedString::ValidationRangeOverflow:
286 return IDS_FORM_VALIDATION_RANGE_OVERFLOW;
287 case WebLocalizedString::ValidationRangeOverflowDateTime:
288 return IDS_FORM_VALIDATION_RANGE_OVERFLOW_DATETIME;
289 case WebLocalizedString::ValidationRangeUnderflow:
290 return IDS_FORM_VALIDATION_RANGE_UNDERFLOW;
291 case WebLocalizedString::ValidationRangeUnderflowDateTime:
292 return IDS_FORM_VALIDATION_RANGE_UNDERFLOW_DATETIME;
293 case WebLocalizedString::ValidationStepMismatch:
294 return IDS_FORM_VALIDATION_STEP_MISMATCH;
295 case WebLocalizedString::ValidationStepMismatchCloseToLimit:
296 return IDS_FORM_VALIDATION_STEP_MISMATCH_CLOSE_TO_LIMIT;
297 case WebLocalizedString::ValidationTooLong:
298 return IDS_FORM_VALIDATION_TOO_LONG;
299 case WebLocalizedString::ValidationTypeMismatch:
300 return IDS_FORM_VALIDATION_TYPE_MISMATCH;
301 case WebLocalizedString::ValidationTypeMismatchForEmail:
302 return IDS_FORM_VALIDATION_TYPE_MISMATCH_EMAIL;
303 case WebLocalizedString::ValidationTypeMismatchForEmailEmpty:
304 return IDS_FORM_VALIDATION_TYPE_MISMATCH_EMAIL_EMPTY;
305 case WebLocalizedString::ValidationTypeMismatchForEmailEmptyDomain:
306 return IDS_FORM_VALIDATION_TYPE_MISMATCH_EMAIL_EMPTY_DOMAIN;
307 case WebLocalizedString::ValidationTypeMismatchForEmailEmptyLocal:
308 return IDS_FORM_VALIDATION_TYPE_MISMATCH_EMAIL_EMPTY_LOCAL;
309 case WebLocalizedString::ValidationTypeMismatchForEmailInvalidDomain:
310 return IDS_FORM_VALIDATION_TYPE_MISMATCH_EMAIL_INVALID_DOMAIN;
311 case WebLocalizedString::ValidationTypeMismatchForEmailInvalidDots:
312 return IDS_FORM_VALIDATION_TYPE_MISMATCH_EMAIL_INVALID_DOTS;
313 case WebLocalizedString::ValidationTypeMismatchForEmailInvalidLocal:
314 return IDS_FORM_VALIDATION_TYPE_MISMATCH_EMAIL_INVALID_LOCAL;
315 case WebLocalizedString::ValidationTypeMismatchForEmailNoAtSign:
316 return IDS_FORM_VALIDATION_TYPE_MISMATCH_EMAIL_NO_AT_SIGN;
317 case WebLocalizedString::ValidationTypeMismatchForMultipleEmail:
318 return IDS_FORM_VALIDATION_TYPE_MISMATCH_MULTIPLE_EMAIL;
319 case WebLocalizedString::ValidationTypeMismatchForURL:
320 return IDS_FORM_VALIDATION_TYPE_MISMATCH_URL;
321 case WebLocalizedString::ValidationValueMissing:
322 return IDS_FORM_VALIDATION_VALUE_MISSING;
323 case WebLocalizedString::ValidationValueMissingForCheckbox:
324 return IDS_FORM_VALIDATION_VALUE_MISSING_CHECKBOX;
325 case WebLocalizedString::ValidationValueMissingForFile:
326 return IDS_FORM_VALIDATION_VALUE_MISSING_FILE;
327 case WebLocalizedString::ValidationValueMissingForMultipleFile:
328 return IDS_FORM_VALIDATION_VALUE_MISSING_MULTIPLE_FILE;
329 case WebLocalizedString::ValidationValueMissingForRadio:
330 return IDS_FORM_VALIDATION_VALUE_MISSING_RADIO;
331 case WebLocalizedString::ValidationValueMissingForSelect:
332 return IDS_FORM_VALIDATION_VALUE_MISSING_SELECT;
333 case WebLocalizedString::WeekFormatTemplate:
334 return IDS_FORM_INPUT_WEEK_TEMPLATE;
335 case WebLocalizedString::WeekNumberLabel:
336 return IDS_FORM_WEEK_NUMBER_LABEL;
337 // This "default:" line exists to avoid compile warnings about enum
338 // coverage when we add a new symbol to WebLocalizedString.h in WebKit.
339 // After a planned WebKit patch is landed, we need to add a case statement
340 // for the added symbol here.
347 WebKitPlatformSupportImpl::WebKitPlatformSupportImpl()
348 : main_loop_(base::MessageLoop::current()),
349 shared_timer_func_(NULL),
350 shared_timer_fire_time_(0.0),
351 shared_timer_fire_time_was_set_while_suspended_(false),
352 shared_timer_suspended_(0) {}
354 WebKitPlatformSupportImpl::~WebKitPlatformSupportImpl() {
357 WebURLLoader* WebKitPlatformSupportImpl::createURLLoader() {
358 return new WebURLLoaderImpl(this);
361 WebSocketStreamHandle* WebKitPlatformSupportImpl::createSocketStreamHandle() {
362 return new WebSocketStreamHandleImpl(this);
365 WebString WebKitPlatformSupportImpl::userAgent(const WebURL& url) {
366 return WebString::fromUTF8(webkit_glue::GetUserAgent(url));
369 WebData WebKitPlatformSupportImpl::parseDataURL(
371 WebString& mimetype_out,
372 WebString& charset_out) {
373 std::string mime_type, char_set, data;
374 if (net::DataURL::Parse(url, &mime_type, &char_set, &data)
375 && net::IsSupportedMimeType(mime_type)) {
376 mimetype_out = WebString::fromUTF8(mime_type);
377 charset_out = WebString::fromUTF8(char_set);
383 WebURLError WebKitPlatformSupportImpl::cancelledError(
384 const WebURL& unreachableURL) const {
385 return WebURLLoaderImpl::CreateError(unreachableURL, false, net::ERR_ABORTED);
388 void WebKitPlatformSupportImpl::decrementStatsCounter(const char* name) {
389 base::StatsCounter(name).Decrement();
392 void WebKitPlatformSupportImpl::incrementStatsCounter(const char* name) {
393 base::StatsCounter(name).Increment();
396 void WebKitPlatformSupportImpl::histogramCustomCounts(
397 const char* name, int sample, int min, int max, int bucket_count) {
398 // Copied from histogram macro, but without the static variable caching
399 // the histogram because name is dynamic.
400 base::HistogramBase* counter =
401 base::Histogram::FactoryGet(name, min, max, bucket_count,
402 base::HistogramBase::kUmaTargetedHistogramFlag);
403 DCHECK_EQ(name, counter->histogram_name());
404 counter->Add(sample);
407 void WebKitPlatformSupportImpl::histogramEnumeration(
408 const char* name, int sample, int boundary_value) {
409 // Copied from histogram macro, but without the static variable caching
410 // the histogram because name is dynamic.
411 base::HistogramBase* counter =
412 base::LinearHistogram::FactoryGet(name, 1, boundary_value,
413 boundary_value + 1, base::HistogramBase::kUmaTargetedHistogramFlag);
414 DCHECK_EQ(name, counter->histogram_name());
415 counter->Add(sample);
418 void WebKitPlatformSupportImpl::histogramSparse(const char* name, int sample) {
419 // For sparse histograms, we can use the macro, as it does not incorporate a
421 UMA_HISTOGRAM_SPARSE_SLOWLY(name, sample);
424 const unsigned char* WebKitPlatformSupportImpl::getTraceCategoryEnabledFlag(
425 const char* category_group) {
426 return TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_group);
429 long* WebKitPlatformSupportImpl::getTraceSamplingState(
430 const unsigned thread_bucket) {
431 switch (thread_bucket) {
433 return reinterpret_cast<long*>(&TRACE_EVENT_API_THREAD_BUCKET(0));
435 return reinterpret_cast<long*>(&TRACE_EVENT_API_THREAD_BUCKET(1));
437 return reinterpret_cast<long*>(&TRACE_EVENT_API_THREAD_BUCKET(2));
439 NOTREACHED() << "Unknown thread bucket type.";
445 sizeof(blink::Platform::TraceEventHandle) ==
446 sizeof(base::debug::TraceEventHandle),
447 TraceEventHandle_types_must_be_same_size);
449 blink::Platform::TraceEventHandle WebKitPlatformSupportImpl::addTraceEvent(
451 const unsigned char* category_group_enabled,
453 unsigned long long id,
455 const char** arg_names,
456 const unsigned char* arg_types,
457 const unsigned long long* arg_values,
458 unsigned char flags) {
459 base::debug::TraceEventHandle handle = TRACE_EVENT_API_ADD_TRACE_EVENT(
460 phase, category_group_enabled, name, id,
461 num_args, arg_names, arg_types, arg_values, NULL, flags);
462 blink::Platform::TraceEventHandle result;
463 memcpy(&result, &handle, sizeof(result));
467 void WebKitPlatformSupportImpl::updateTraceEventDuration(
468 const unsigned char* category_group_enabled,
470 TraceEventHandle handle) {
471 base::debug::TraceEventHandle traceEventHandle;
472 memcpy(&traceEventHandle, &handle, sizeof(handle));
473 TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(
474 category_group_enabled, name, traceEventHandle);
479 WebData loadAudioSpatializationResource(WebKitPlatformSupportImpl* platform,
481 #ifdef IDR_AUDIO_SPATIALIZATION_COMPOSITE
482 if (!strcmp(name, "Composite")) {
483 base::StringPiece resource =
484 platform->GetDataResource(IDR_AUDIO_SPATIALIZATION_COMPOSITE,
485 ui::SCALE_FACTOR_NONE);
486 return WebData(resource.data(), resource.size());
490 #ifdef IDR_AUDIO_SPATIALIZATION_T000_P000
491 const size_t kExpectedSpatializationNameLength = 31;
492 if (strlen(name) != kExpectedSpatializationNameLength) {
496 // Extract the azimuth and elevation from the resource name.
500 sscanf(name, "IRC_Composite_C_R0195_T%3d_P%3d", &azimuth, &elevation);
501 if (values_parsed != 2) {
505 // The resource index values go through the elevations first, then azimuths.
506 const int kAngleSpacing = 15;
508 // 0 <= elevation <= 90 (or 315 <= elevation <= 345)
509 // in increments of 15 degrees.
510 int elevation_index =
511 elevation <= 90 ? elevation / kAngleSpacing :
512 7 + (elevation - 315) / kAngleSpacing;
513 bool is_elevation_index_good = 0 <= elevation_index && elevation_index < 10;
515 // 0 <= azimuth < 360 in increments of 15 degrees.
516 int azimuth_index = azimuth / kAngleSpacing;
517 bool is_azimuth_index_good = 0 <= azimuth_index && azimuth_index < 24;
519 const int kNumberOfElevations = 10;
520 const int kNumberOfAudioResources = 240;
521 int resource_index = kNumberOfElevations * azimuth_index + elevation_index;
522 bool is_resource_index_good = 0 <= resource_index &&
523 resource_index < kNumberOfAudioResources;
525 if (is_azimuth_index_good && is_elevation_index_good &&
526 is_resource_index_good) {
527 const int kFirstAudioResourceIndex = IDR_AUDIO_SPATIALIZATION_T000_P000;
528 base::StringPiece resource =
529 platform->GetDataResource(kFirstAudioResourceIndex + resource_index,
530 ui::SCALE_FACTOR_NONE);
531 return WebData(resource.data(), resource.size());
533 #endif // IDR_AUDIO_SPATIALIZATION_T000_P000
539 struct DataResource {
542 ui::ScaleFactor scale_factor;
545 const DataResource kDataResources[] = {
546 { "missingImage", IDR_BROKENIMAGE, ui::SCALE_FACTOR_100P },
547 { "missingImage@2x", IDR_BROKENIMAGE, ui::SCALE_FACTOR_200P },
548 { "mediaplayerPause", IDR_MEDIAPLAYER_PAUSE_BUTTON, ui::SCALE_FACTOR_100P },
549 { "mediaplayerPauseHover",
550 IDR_MEDIAPLAYER_PAUSE_BUTTON_HOVER, ui::SCALE_FACTOR_100P },
551 { "mediaplayerPauseDown",
552 IDR_MEDIAPLAYER_PAUSE_BUTTON_DOWN, ui::SCALE_FACTOR_100P },
553 { "mediaplayerPlay", IDR_MEDIAPLAYER_PLAY_BUTTON, ui::SCALE_FACTOR_100P },
554 { "mediaplayerPlayHover",
555 IDR_MEDIAPLAYER_PLAY_BUTTON_HOVER, ui::SCALE_FACTOR_100P },
556 { "mediaplayerPlayDown",
557 IDR_MEDIAPLAYER_PLAY_BUTTON_DOWN, ui::SCALE_FACTOR_100P },
558 { "mediaplayerPlayDisabled",
559 IDR_MEDIAPLAYER_PLAY_BUTTON_DISABLED, ui::SCALE_FACTOR_100P },
560 { "mediaplayerSoundLevel3",
561 IDR_MEDIAPLAYER_SOUND_LEVEL3_BUTTON, ui::SCALE_FACTOR_100P },
562 { "mediaplayerSoundLevel3Hover",
563 IDR_MEDIAPLAYER_SOUND_LEVEL3_BUTTON_HOVER, ui::SCALE_FACTOR_100P },
564 { "mediaplayerSoundLevel3Down",
565 IDR_MEDIAPLAYER_SOUND_LEVEL3_BUTTON_DOWN, ui::SCALE_FACTOR_100P },
566 { "mediaplayerSoundLevel2",
567 IDR_MEDIAPLAYER_SOUND_LEVEL2_BUTTON, ui::SCALE_FACTOR_100P },
568 { "mediaplayerSoundLevel2Hover",
569 IDR_MEDIAPLAYER_SOUND_LEVEL2_BUTTON_HOVER, ui::SCALE_FACTOR_100P },
570 { "mediaplayerSoundLevel2Down",
571 IDR_MEDIAPLAYER_SOUND_LEVEL2_BUTTON_DOWN, ui::SCALE_FACTOR_100P },
572 { "mediaplayerSoundLevel1",
573 IDR_MEDIAPLAYER_SOUND_LEVEL1_BUTTON, ui::SCALE_FACTOR_100P },
574 { "mediaplayerSoundLevel1Hover",
575 IDR_MEDIAPLAYER_SOUND_LEVEL1_BUTTON_HOVER, ui::SCALE_FACTOR_100P },
576 { "mediaplayerSoundLevel1Down",
577 IDR_MEDIAPLAYER_SOUND_LEVEL1_BUTTON_DOWN, ui::SCALE_FACTOR_100P },
578 { "mediaplayerSoundLevel0",
579 IDR_MEDIAPLAYER_SOUND_LEVEL0_BUTTON, ui::SCALE_FACTOR_100P },
580 { "mediaplayerSoundLevel0Hover",
581 IDR_MEDIAPLAYER_SOUND_LEVEL0_BUTTON_HOVER, ui::SCALE_FACTOR_100P },
582 { "mediaplayerSoundLevel0Down",
583 IDR_MEDIAPLAYER_SOUND_LEVEL0_BUTTON_DOWN, ui::SCALE_FACTOR_100P },
584 { "mediaplayerSoundDisabled",
585 IDR_MEDIAPLAYER_SOUND_DISABLED, ui::SCALE_FACTOR_100P },
586 { "mediaplayerSliderThumb",
587 IDR_MEDIAPLAYER_SLIDER_THUMB, ui::SCALE_FACTOR_100P },
588 { "mediaplayerSliderThumbHover",
589 IDR_MEDIAPLAYER_SLIDER_THUMB_HOVER, ui::SCALE_FACTOR_100P },
590 { "mediaplayerSliderThumbDown",
591 IDR_MEDIAPLAYER_SLIDER_THUMB_DOWN, ui::SCALE_FACTOR_100P },
592 { "mediaplayerVolumeSliderThumb",
593 IDR_MEDIAPLAYER_VOLUME_SLIDER_THUMB, ui::SCALE_FACTOR_100P },
594 { "mediaplayerVolumeSliderThumbHover",
595 IDR_MEDIAPLAYER_VOLUME_SLIDER_THUMB_HOVER, ui::SCALE_FACTOR_100P },
596 { "mediaplayerVolumeSliderThumbDown",
597 IDR_MEDIAPLAYER_VOLUME_SLIDER_THUMB_DOWN, ui::SCALE_FACTOR_100P },
598 { "mediaplayerVolumeSliderThumbDisabled",
599 IDR_MEDIAPLAYER_VOLUME_SLIDER_THUMB_DISABLED, ui::SCALE_FACTOR_100P },
600 { "mediaplayerClosedCaption",
601 IDR_MEDIAPLAYER_CLOSEDCAPTION_BUTTON, ui::SCALE_FACTOR_100P },
602 { "mediaplayerClosedCaptionHover",
603 IDR_MEDIAPLAYER_CLOSEDCAPTION_BUTTON_HOVER, ui::SCALE_FACTOR_100P },
604 { "mediaplayerClosedCaptionDown",
605 IDR_MEDIAPLAYER_CLOSEDCAPTION_BUTTON_DOWN, ui::SCALE_FACTOR_100P },
606 { "mediaplayerClosedCaptionDisabled",
607 IDR_MEDIAPLAYER_CLOSEDCAPTION_BUTTON_DISABLED, ui::SCALE_FACTOR_100P },
608 { "mediaplayerFullscreen",
609 IDR_MEDIAPLAYER_FULLSCREEN_BUTTON, ui::SCALE_FACTOR_100P },
610 { "mediaplayerFullscreenHover",
611 IDR_MEDIAPLAYER_FULLSCREEN_BUTTON_HOVER, ui::SCALE_FACTOR_100P },
612 { "mediaplayerFullscreenDown",
613 IDR_MEDIAPLAYER_FULLSCREEN_BUTTON_DOWN, ui::SCALE_FACTOR_100P },
614 { "mediaplayerFullscreenDisabled",
615 IDR_MEDIAPLAYER_FULLSCREEN_BUTTON_DISABLED, ui::SCALE_FACTOR_100P },
616 #if defined(OS_ANDROID)
617 { "mediaplayerOverlayPlay",
618 IDR_MEDIAPLAYER_OVERLAY_PLAY_BUTTON, ui::SCALE_FACTOR_100P },
620 #if defined(OS_MACOSX)
621 { "overhangPattern", IDR_OVERHANG_PATTERN, ui::SCALE_FACTOR_100P },
622 { "overhangShadow", IDR_OVERHANG_SHADOW, ui::SCALE_FACTOR_100P },
624 { "panIcon", IDR_PAN_SCROLL_ICON, ui::SCALE_FACTOR_100P },
625 { "searchCancel", IDR_SEARCH_CANCEL, ui::SCALE_FACTOR_100P },
626 { "searchCancelPressed", IDR_SEARCH_CANCEL_PRESSED, ui::SCALE_FACTOR_100P },
627 { "searchMagnifier", IDR_SEARCH_MAGNIFIER, ui::SCALE_FACTOR_100P },
628 { "searchMagnifierResults",
629 IDR_SEARCH_MAGNIFIER_RESULTS, ui::SCALE_FACTOR_100P },
630 { "textAreaResizeCorner", IDR_TEXTAREA_RESIZER, ui::SCALE_FACTOR_100P },
631 { "textAreaResizeCorner@2x", IDR_TEXTAREA_RESIZER, ui::SCALE_FACTOR_200P },
632 { "inputSpeech", IDR_INPUT_SPEECH, ui::SCALE_FACTOR_100P },
633 { "inputSpeechRecording", IDR_INPUT_SPEECH_RECORDING, ui::SCALE_FACTOR_100P },
634 { "inputSpeechWaiting", IDR_INPUT_SPEECH_WAITING, ui::SCALE_FACTOR_100P },
635 { "americanExpressCC", IDR_AUTOFILL_CC_AMEX, ui::SCALE_FACTOR_100P },
636 { "dinersCC", IDR_AUTOFILL_CC_DINERS, ui::SCALE_FACTOR_100P },
637 { "discoverCC", IDR_AUTOFILL_CC_DISCOVER, ui::SCALE_FACTOR_100P },
638 { "genericCC", IDR_AUTOFILL_CC_GENERIC, ui::SCALE_FACTOR_100P },
639 { "jcbCC", IDR_AUTOFILL_CC_JCB, ui::SCALE_FACTOR_100P },
640 { "masterCardCC", IDR_AUTOFILL_CC_MASTERCARD, ui::SCALE_FACTOR_100P },
641 { "visaCC", IDR_AUTOFILL_CC_VISA, ui::SCALE_FACTOR_100P },
642 { "generatePassword", IDR_PASSWORD_GENERATION_ICON, ui::SCALE_FACTOR_100P },
643 { "generatePasswordHover",
644 IDR_PASSWORD_GENERATION_ICON_HOVER, ui::SCALE_FACTOR_100P },
645 { "syntheticTouchCursor",
646 IDR_SYNTHETIC_TOUCH_CURSOR, ui::SCALE_FACTOR_100P },
651 WebData WebKitPlatformSupportImpl::loadResource(const char* name) {
652 // Some clients will call into this method with an empty |name| when they have
653 // optional resources. For example, the PopupMenuChromium code can have icons
654 // for some Autofill items but not for others.
658 // Check the name prefix to see if it's an audio resource.
659 if (StartsWithASCII(name, "IRC_Composite", true) ||
660 StartsWithASCII(name, "Composite", true))
661 return loadAudioSpatializationResource(this, name);
663 // TODO(flackr): We should use a better than linear search here, a trie would
665 for (size_t i = 0; i < arraysize(kDataResources); ++i) {
666 if (!strcmp(name, kDataResources[i].name)) {
667 base::StringPiece resource =
668 GetDataResource(kDataResources[i].id,
669 kDataResources[i].scale_factor);
670 return WebData(resource.data(), resource.size());
674 NOTREACHED() << "Unknown image resource " << name;
678 WebString WebKitPlatformSupportImpl::queryLocalizedString(
679 WebLocalizedString::Name name) {
680 int message_id = ToMessageID(name);
683 return GetLocalizedString(message_id);
686 WebString WebKitPlatformSupportImpl::queryLocalizedString(
687 WebLocalizedString::Name name, int numeric_value) {
688 return queryLocalizedString(name, base::IntToString16(numeric_value));
691 WebString WebKitPlatformSupportImpl::queryLocalizedString(
692 WebLocalizedString::Name name, const WebString& value) {
693 int message_id = ToMessageID(name);
696 return ReplaceStringPlaceholders(GetLocalizedString(message_id), value, NULL);
699 WebString WebKitPlatformSupportImpl::queryLocalizedString(
700 WebLocalizedString::Name name,
701 const WebString& value1,
702 const WebString& value2) {
703 int message_id = ToMessageID(name);
706 std::vector<base::string16> values;
708 values.push_back(value1);
709 values.push_back(value2);
710 return ReplaceStringPlaceholders(
711 GetLocalizedString(message_id), values, NULL);
714 double WebKitPlatformSupportImpl::currentTime() {
715 return base::Time::Now().ToDoubleT();
718 double WebKitPlatformSupportImpl::monotonicallyIncreasingTime() {
719 return base::TimeTicks::Now().ToInternalValue() /
720 static_cast<double>(base::Time::kMicrosecondsPerSecond);
723 void WebKitPlatformSupportImpl::cryptographicallyRandomValues(
724 unsigned char* buffer, size_t length) {
725 base::RandBytes(buffer, length);
728 void WebKitPlatformSupportImpl::setSharedTimerFiredFunction(void (*func)()) {
729 shared_timer_func_ = func;
732 void WebKitPlatformSupportImpl::setSharedTimerFireInterval(
733 double interval_seconds) {
734 shared_timer_fire_time_ = interval_seconds + monotonicallyIncreasingTime();
735 if (shared_timer_suspended_) {
736 shared_timer_fire_time_was_set_while_suspended_ = true;
740 // By converting between double and int64 representation, we run the risk
741 // of losing precision due to rounding errors. Performing computations in
742 // microseconds reduces this risk somewhat. But there still is the potential
743 // of us computing a fire time for the timer that is shorter than what we
745 // As the event loop will check event deadlines prior to actually firing
746 // them, there is a risk of needlessly rescheduling events and of
747 // needlessly looping if sleep times are too short even by small amounts.
748 // This results in measurable performance degradation unless we use ceil() to
749 // always round up the sleep times.
750 int64 interval = static_cast<int64>(
751 ceil(interval_seconds * base::Time::kMillisecondsPerSecond)
752 * base::Time::kMicrosecondsPerMillisecond);
757 shared_timer_.Stop();
758 shared_timer_.Start(FROM_HERE, base::TimeDelta::FromMicroseconds(interval),
759 this, &WebKitPlatformSupportImpl::DoTimeout);
760 OnStartSharedTimer(base::TimeDelta::FromMicroseconds(interval));
763 void WebKitPlatformSupportImpl::stopSharedTimer() {
764 shared_timer_.Stop();
767 void WebKitPlatformSupportImpl::callOnMainThread(
768 void (*func)(void*), void* context) {
769 main_loop_->PostTask(FROM_HERE, base::Bind(func, context));
772 base::PlatformFile WebKitPlatformSupportImpl::databaseOpenFile(
773 const blink::WebString& vfs_file_name, int desired_flags) {
774 return base::kInvalidPlatformFileValue;
777 int WebKitPlatformSupportImpl::databaseDeleteFile(
778 const blink::WebString& vfs_file_name, bool sync_dir) {
782 long WebKitPlatformSupportImpl::databaseGetFileAttributes(
783 const blink::WebString& vfs_file_name) {
787 long long WebKitPlatformSupportImpl::databaseGetFileSize(
788 const blink::WebString& vfs_file_name) {
792 long long WebKitPlatformSupportImpl::databaseGetSpaceAvailableForOrigin(
793 const blink::WebString& origin_identifier) {
797 blink::WebString WebKitPlatformSupportImpl::signedPublicKeyAndChallengeString(
798 unsigned key_size_index,
799 const blink::WebString& challenge,
800 const blink::WebURL& url) {
801 return blink::WebString("");
804 static scoped_ptr<base::ProcessMetrics> CurrentProcessMetrics() {
805 using base::ProcessMetrics;
806 #if defined(OS_MACOSX)
807 return scoped_ptr<ProcessMetrics>(
808 // The default port provider is sufficient to get data for the current
810 ProcessMetrics::CreateProcessMetrics(base::GetCurrentProcessHandle(),
813 return scoped_ptr<ProcessMetrics>(
814 ProcessMetrics::CreateProcessMetrics(base::GetCurrentProcessHandle()));
818 static size_t getMemoryUsageMB(bool bypass_cache) {
819 size_t current_mem_usage = 0;
820 MemoryUsageCache* mem_usage_cache_singleton = MemoryUsageCache::GetInstance();
822 mem_usage_cache_singleton->IsCachedValueValid(¤t_mem_usage))
823 return current_mem_usage;
825 current_mem_usage = MemoryUsageKB() >> 10;
826 mem_usage_cache_singleton->SetMemoryValue(current_mem_usage);
827 return current_mem_usage;
830 size_t WebKitPlatformSupportImpl::memoryUsageMB() {
831 return getMemoryUsageMB(false);
834 size_t WebKitPlatformSupportImpl::actualMemoryUsageMB() {
835 return getMemoryUsageMB(true);
838 size_t WebKitPlatformSupportImpl::physicalMemoryMB() {
839 return static_cast<size_t>(base::SysInfo::AmountOfPhysicalMemoryMB());
842 size_t WebKitPlatformSupportImpl::numberOfProcessors() {
843 return static_cast<size_t>(base::SysInfo::NumberOfProcessors());
846 void WebKitPlatformSupportImpl::startHeapProfiling(
847 const blink::WebString& prefix) {
848 // FIXME(morrita): Make this built on windows.
849 #if !defined(NO_TCMALLOC) && defined(USE_TCMALLOC) && !defined(OS_WIN)
850 HeapProfilerStart(prefix.utf8().data());
854 void WebKitPlatformSupportImpl::stopHeapProfiling() {
855 #if !defined(NO_TCMALLOC) && defined(USE_TCMALLOC) && !defined(OS_WIN)
860 void WebKitPlatformSupportImpl::dumpHeapProfiling(
861 const blink::WebString& reason) {
862 #if !defined(NO_TCMALLOC) && defined(USE_TCMALLOC) && !defined(OS_WIN)
863 HeapProfilerDump(reason.utf8().data());
867 WebString WebKitPlatformSupportImpl::getHeapProfile() {
868 #if !defined(NO_TCMALLOC) && defined(USE_TCMALLOC) && !defined(OS_WIN)
869 char* data = GetHeapProfile();
870 WebString result = WebString::fromUTF8(std::string(data));
878 bool WebKitPlatformSupportImpl::processMemorySizesInBytes(
879 size_t* private_bytes,
880 size_t* shared_bytes) {
881 return CurrentProcessMetrics()->GetMemoryBytes(private_bytes, shared_bytes);
884 bool WebKitPlatformSupportImpl::memoryAllocatorWasteInBytes(size_t* size) {
885 return base::allocator::GetAllocatorWasteSize(size);
888 size_t WebKitPlatformSupportImpl::maxDecodedImageBytes() {
889 #if defined(OS_ANDROID)
890 if (base::android::SysUtils::IsLowEndDevice()) {
891 // Limit image decoded size to 3M pixels on low end devices.
892 // 4 is maximum number of bytes per pixel.
893 return 3 * 1024 * 1024 * 4;
895 // For other devices, limit decoded image size based on the amount of physical
896 // memory. For a device with 2GB physical memory the limit is 16M pixels.
897 return base::SysInfo::AmountOfPhysicalMemory() / 32;
899 return noDecodedImageByteLimit;
903 void WebKitPlatformSupportImpl::SuspendSharedTimer() {
904 ++shared_timer_suspended_;
907 void WebKitPlatformSupportImpl::ResumeSharedTimer() {
908 // The shared timer may have fired or been adjusted while we were suspended.
909 if (--shared_timer_suspended_ == 0 &&
910 (!shared_timer_.IsRunning() ||
911 shared_timer_fire_time_was_set_while_suspended_)) {
912 shared_timer_fire_time_was_set_while_suspended_ = false;
913 setSharedTimerFireInterval(
914 shared_timer_fire_time_ - monotonicallyIncreasingTime());
918 } // namespace webkit_glue