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/WebCookie.h"
36 #include "third_party/WebKit/public/platform/WebData.h"
37 #include "third_party/WebKit/public/platform/WebDiscardableMemory.h"
38 #include "third_party/WebKit/public/platform/WebGestureCurve.h"
39 #include "third_party/WebKit/public/platform/WebPluginListBuilder.h"
40 #include "third_party/WebKit/public/platform/WebString.h"
41 #include "third_party/WebKit/public/platform/WebURL.h"
42 #include "third_party/WebKit/public/platform/WebVector.h"
43 #include "third_party/WebKit/public/web/WebFrameClient.h"
44 #include "third_party/WebKit/public/web/WebInputEvent.h"
45 #include "third_party/WebKit/public/web/WebScreenInfo.h"
46 #include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h"
47 #include "ui/base/layout.h"
48 #include "webkit/child/webkit_child_helpers.h"
49 #include "webkit/child/websocketstreamhandle_impl.h"
50 #include "webkit/child/weburlloader_impl.h"
51 #include "webkit/common/user_agent/user_agent.h"
52 #include "webkit/glue/webkit_glue.h"
54 #if defined(OS_ANDROID)
55 #include "base/android/sys_utils.h"
58 using WebKit::WebAudioBus;
59 using WebKit::WebCookie;
60 using WebKit::WebData;
61 using WebKit::WebLocalizedString;
62 using WebKit::WebPluginListBuilder;
63 using WebKit::WebString;
64 using WebKit::WebSocketStreamHandle;
66 using WebKit::WebURLError;
67 using WebKit::WebURLLoader;
68 using WebKit::WebVector;
72 // A simple class to cache the memory usage for a given amount of time.
73 class MemoryUsageCache {
75 // Retrieves the Singleton.
76 static MemoryUsageCache* GetInstance() {
77 return Singleton<MemoryUsageCache>::get();
80 MemoryUsageCache() : memory_value_(0) { Init(); }
81 ~MemoryUsageCache() {}
84 const unsigned int kCacheSeconds = 1;
85 cache_valid_time_ = base::TimeDelta::FromSeconds(kCacheSeconds);
88 // Returns true if the cached value is fresh.
89 // Returns false if the cached value is stale, or if |cached_value| is NULL.
90 bool IsCachedValueValid(size_t* cached_value) {
91 base::AutoLock scoped_lock(lock_);
94 if (base::Time::Now() - last_updated_time_ > cache_valid_time_)
96 *cached_value = memory_value_;
100 // Setter for |memory_value_|, refreshes |last_updated_time_|.
101 void SetMemoryValue(const size_t value) {
102 base::AutoLock scoped_lock(lock_);
103 memory_value_ = value;
104 last_updated_time_ = base::Time::Now();
108 // The cached memory value.
109 size_t memory_value_;
111 // How long the cached value should remain valid.
112 base::TimeDelta cache_valid_time_;
114 // The last time the cached value was updated.
115 base::Time last_updated_time_;
120 } // anonymous namespace
122 namespace webkit_glue {
124 static int ToMessageID(WebLocalizedString::Name name) {
126 case WebLocalizedString::AXAMPMFieldText:
127 return IDS_AX_AM_PM_FIELD_TEXT;
128 case WebLocalizedString::AXButtonActionVerb:
129 return IDS_AX_BUTTON_ACTION_VERB;
130 case WebLocalizedString::AXCheckedCheckBoxActionVerb:
131 return IDS_AX_CHECKED_CHECK_BOX_ACTION_VERB;
132 case WebLocalizedString::AXDateTimeFieldEmptyValueText:
133 return IDS_AX_DATE_TIME_FIELD_EMPTY_VALUE_TEXT;
134 case WebLocalizedString::AXDayOfMonthFieldText:
135 return IDS_AX_DAY_OF_MONTH_FIELD_TEXT;
136 case WebLocalizedString::AXHeadingText:
137 return IDS_AX_ROLE_HEADING;
138 case WebLocalizedString::AXHourFieldText:
139 return IDS_AX_HOUR_FIELD_TEXT;
140 case WebLocalizedString::AXImageMapText:
141 return IDS_AX_ROLE_IMAGE_MAP;
142 case WebLocalizedString::AXLinkActionVerb:
143 return IDS_AX_LINK_ACTION_VERB;
144 case WebLocalizedString::AXLinkText:
145 return IDS_AX_ROLE_LINK;
146 case WebLocalizedString::AXListMarkerText:
147 return IDS_AX_ROLE_LIST_MARKER;
148 case WebLocalizedString::AXMediaDefault:
149 return IDS_AX_MEDIA_DEFAULT;
150 case WebLocalizedString::AXMediaAudioElement:
151 return IDS_AX_MEDIA_AUDIO_ELEMENT;
152 case WebLocalizedString::AXMediaVideoElement:
153 return IDS_AX_MEDIA_VIDEO_ELEMENT;
154 case WebLocalizedString::AXMediaMuteButton:
155 return IDS_AX_MEDIA_MUTE_BUTTON;
156 case WebLocalizedString::AXMediaUnMuteButton:
157 return IDS_AX_MEDIA_UNMUTE_BUTTON;
158 case WebLocalizedString::AXMediaPlayButton:
159 return IDS_AX_MEDIA_PLAY_BUTTON;
160 case WebLocalizedString::AXMediaPauseButton:
161 return IDS_AX_MEDIA_PAUSE_BUTTON;
162 case WebLocalizedString::AXMediaSlider:
163 return IDS_AX_MEDIA_SLIDER;
164 case WebLocalizedString::AXMediaSliderThumb:
165 return IDS_AX_MEDIA_SLIDER_THUMB;
166 case WebLocalizedString::AXMediaRewindButton:
167 return IDS_AX_MEDIA_REWIND_BUTTON;
168 case WebLocalizedString::AXMediaReturnToRealTime:
169 return IDS_AX_MEDIA_RETURN_TO_REALTIME_BUTTON;
170 case WebLocalizedString::AXMediaCurrentTimeDisplay:
171 return IDS_AX_MEDIA_CURRENT_TIME_DISPLAY;
172 case WebLocalizedString::AXMediaTimeRemainingDisplay:
173 return IDS_AX_MEDIA_TIME_REMAINING_DISPLAY;
174 case WebLocalizedString::AXMediaStatusDisplay:
175 return IDS_AX_MEDIA_STATUS_DISPLAY;
176 case WebLocalizedString::AXMediaEnterFullscreenButton:
177 return IDS_AX_MEDIA_ENTER_FULL_SCREEN_BUTTON;
178 case WebLocalizedString::AXMediaExitFullscreenButton:
179 return IDS_AX_MEDIA_EXIT_FULL_SCREEN_BUTTON;
180 case WebLocalizedString::AXMediaSeekForwardButton:
181 return IDS_AX_MEDIA_SEEK_FORWARD_BUTTON;
182 case WebLocalizedString::AXMediaSeekBackButton:
183 return IDS_AX_MEDIA_SEEK_BACK_BUTTON;
184 case WebLocalizedString::AXMediaShowClosedCaptionsButton:
185 return IDS_AX_MEDIA_SHOW_CLOSED_CAPTIONS_BUTTON;
186 case WebLocalizedString::AXMediaHideClosedCaptionsButton:
187 return IDS_AX_MEDIA_HIDE_CLOSED_CAPTIONS_BUTTON;
188 case WebLocalizedString::AXMediaAudioElementHelp:
189 return IDS_AX_MEDIA_AUDIO_ELEMENT_HELP;
190 case WebLocalizedString::AXMediaVideoElementHelp:
191 return IDS_AX_MEDIA_VIDEO_ELEMENT_HELP;
192 case WebLocalizedString::AXMediaMuteButtonHelp:
193 return IDS_AX_MEDIA_MUTE_BUTTON_HELP;
194 case WebLocalizedString::AXMediaUnMuteButtonHelp:
195 return IDS_AX_MEDIA_UNMUTE_BUTTON_HELP;
196 case WebLocalizedString::AXMediaPlayButtonHelp:
197 return IDS_AX_MEDIA_PLAY_BUTTON_HELP;
198 case WebLocalizedString::AXMediaPauseButtonHelp:
199 return IDS_AX_MEDIA_PAUSE_BUTTON_HELP;
200 case WebLocalizedString::AXMediaSliderHelp:
201 return IDS_AX_MEDIA_SLIDER_HELP;
202 case WebLocalizedString::AXMediaSliderThumbHelp:
203 return IDS_AX_MEDIA_SLIDER_THUMB_HELP;
204 case WebLocalizedString::AXMediaRewindButtonHelp:
205 return IDS_AX_MEDIA_REWIND_BUTTON_HELP;
206 case WebLocalizedString::AXMediaReturnToRealTimeHelp:
207 return IDS_AX_MEDIA_RETURN_TO_REALTIME_BUTTON_HELP;
208 case WebLocalizedString::AXMediaCurrentTimeDisplayHelp:
209 return IDS_AX_MEDIA_CURRENT_TIME_DISPLAY_HELP;
210 case WebLocalizedString::AXMediaTimeRemainingDisplayHelp:
211 return IDS_AX_MEDIA_TIME_REMAINING_DISPLAY_HELP;
212 case WebLocalizedString::AXMediaStatusDisplayHelp:
213 return IDS_AX_MEDIA_STATUS_DISPLAY_HELP;
214 case WebLocalizedString::AXMediaEnterFullscreenButtonHelp:
215 return IDS_AX_MEDIA_ENTER_FULL_SCREEN_BUTTON_HELP;
216 case WebLocalizedString::AXMediaExitFullscreenButtonHelp:
217 return IDS_AX_MEDIA_EXIT_FULL_SCREEN_BUTTON_HELP;
218 case WebLocalizedString::AXMediaSeekForwardButtonHelp:
219 return IDS_AX_MEDIA_SEEK_FORWARD_BUTTON_HELP;
220 case WebLocalizedString::AXMediaSeekBackButtonHelp:
221 return IDS_AX_MEDIA_SEEK_BACK_BUTTON_HELP;
222 case WebLocalizedString::AXMediaShowClosedCaptionsButtonHelp:
223 return IDS_AX_MEDIA_SHOW_CLOSED_CAPTIONS_BUTTON_HELP;
224 case WebLocalizedString::AXMediaHideClosedCaptionsButtonHelp:
225 return IDS_AX_MEDIA_HIDE_CLOSED_CAPTIONS_BUTTON_HELP;
226 case WebLocalizedString::AXMillisecondFieldText:
227 return IDS_AX_MILLISECOND_FIELD_TEXT;
228 case WebLocalizedString::AXMinuteFieldText:
229 return IDS_AX_MINUTE_FIELD_TEXT;
230 case WebLocalizedString::AXMonthFieldText:
231 return IDS_AX_MONTH_FIELD_TEXT;
232 case WebLocalizedString::AXRadioButtonActionVerb:
233 return IDS_AX_RADIO_BUTTON_ACTION_VERB;
234 case WebLocalizedString::AXSecondFieldText:
235 return IDS_AX_SECOND_FIELD_TEXT;
236 case WebLocalizedString::AXTextFieldActionVerb:
237 return IDS_AX_TEXT_FIELD_ACTION_VERB;
238 case WebLocalizedString::AXUncheckedCheckBoxActionVerb:
239 return IDS_AX_UNCHECKED_CHECK_BOX_ACTION_VERB;
240 case WebLocalizedString::AXWebAreaText:
241 return IDS_AX_ROLE_WEB_AREA;
242 case WebLocalizedString::AXWeekOfYearFieldText:
243 return IDS_AX_WEEK_OF_YEAR_FIELD_TEXT;
244 case WebLocalizedString::AXYearFieldText:
245 return IDS_AX_YEAR_FIELD_TEXT;
246 case WebLocalizedString::CalendarClear:
247 return IDS_FORM_CALENDAR_CLEAR;
248 case WebLocalizedString::CalendarToday:
249 return IDS_FORM_CALENDAR_TODAY;
250 case WebLocalizedString::DateFormatDayInMonthLabel:
251 return IDS_FORM_DATE_FORMAT_DAY_IN_MONTH;
252 case WebLocalizedString::DateFormatMonthLabel:
253 return IDS_FORM_DATE_FORMAT_MONTH;
254 case WebLocalizedString::DateFormatYearLabel:
255 return IDS_FORM_DATE_FORMAT_YEAR;
256 case WebLocalizedString::DetailsLabel:
257 return IDS_DETAILS_WITHOUT_SUMMARY_LABEL;
258 case WebLocalizedString::FileButtonChooseFileLabel:
259 return IDS_FORM_FILE_BUTTON_LABEL;
260 case WebLocalizedString::FileButtonChooseMultipleFilesLabel:
261 return IDS_FORM_MULTIPLE_FILES_BUTTON_LABEL;
262 case WebLocalizedString::FileButtonNoFileSelectedLabel:
263 return IDS_FORM_FILE_NO_FILE_LABEL;
264 case WebLocalizedString::InputElementAltText:
265 return IDS_FORM_INPUT_ALT;
266 case WebLocalizedString::KeygenMenuHighGradeKeySize:
267 return IDS_KEYGEN_HIGH_GRADE_KEY;
268 case WebLocalizedString::KeygenMenuMediumGradeKeySize:
269 return IDS_KEYGEN_MED_GRADE_KEY;
270 case WebLocalizedString::MissingPluginText:
271 return IDS_PLUGIN_INITIALIZATION_ERROR;
272 case WebLocalizedString::MultipleFileUploadText:
273 return IDS_FORM_FILE_MULTIPLE_UPLOAD;
274 case WebLocalizedString::OtherColorLabel:
275 return IDS_FORM_OTHER_COLOR_LABEL;
276 case WebLocalizedString::OtherDateLabel:
277 return IDS_FORM_OTHER_DATE_LABEL;
278 case WebLocalizedString::OtherMonthLabel:
279 return IDS_FORM_OTHER_MONTH_LABEL;
280 case WebLocalizedString::OtherTimeLabel:
281 return IDS_FORM_OTHER_TIME_LABEL;
282 case WebLocalizedString::OtherWeekLabel:
283 return IDS_FORM_OTHER_WEEK_LABEL;
284 case WebLocalizedString::PlaceholderForDayOfMonthField:
285 return IDS_FORM_PLACEHOLDER_FOR_DAY_OF_MONTH_FIELD;
286 case WebLocalizedString::PlaceholderForMonthField:
287 return IDS_FORM_PLACEHOLDER_FOR_MONTH_FIELD;
288 case WebLocalizedString::PlaceholderForYearField:
289 return IDS_FORM_PLACEHOLDER_FOR_YEAR_FIELD;
290 case WebLocalizedString::ResetButtonDefaultLabel:
291 return IDS_FORM_RESET_LABEL;
292 case WebLocalizedString::SearchableIndexIntroduction:
293 return IDS_SEARCHABLE_INDEX_INTRO;
294 case WebLocalizedString::SearchMenuClearRecentSearchesText:
295 return IDS_RECENT_SEARCHES_CLEAR;
296 case WebLocalizedString::SearchMenuNoRecentSearchesText:
297 return IDS_RECENT_SEARCHES_NONE;
298 case WebLocalizedString::SearchMenuRecentSearchesText:
299 return IDS_RECENT_SEARCHES;
300 case WebLocalizedString::SubmitButtonDefaultLabel:
301 return IDS_FORM_SUBMIT_LABEL;
302 case WebLocalizedString::ThisMonthButtonLabel:
303 return IDS_FORM_THIS_MONTH_LABEL;
304 case WebLocalizedString::ThisWeekButtonLabel:
305 return IDS_FORM_THIS_WEEK_LABEL;
306 case WebLocalizedString::ValidationBadInputForDateTime:
307 return IDS_FORM_VALIDATION_BAD_INPUT_DATETIME;
308 case WebLocalizedString::ValidationBadInputForNumber:
309 return IDS_FORM_VALIDATION_BAD_INPUT_NUMBER;
310 case WebLocalizedString::ValidationPatternMismatch:
311 return IDS_FORM_VALIDATION_PATTERN_MISMATCH;
312 case WebLocalizedString::ValidationRangeOverflow:
313 return IDS_FORM_VALIDATION_RANGE_OVERFLOW;
314 case WebLocalizedString::ValidationRangeOverflowDateTime:
315 return IDS_FORM_VALIDATION_RANGE_OVERFLOW_DATETIME;
316 case WebLocalizedString::ValidationRangeUnderflow:
317 return IDS_FORM_VALIDATION_RANGE_UNDERFLOW;
318 case WebLocalizedString::ValidationRangeUnderflowDateTime:
319 return IDS_FORM_VALIDATION_RANGE_UNDERFLOW_DATETIME;
320 case WebLocalizedString::ValidationStepMismatch:
321 return IDS_FORM_VALIDATION_STEP_MISMATCH;
322 case WebLocalizedString::ValidationStepMismatchCloseToLimit:
323 return IDS_FORM_VALIDATION_STEP_MISMATCH_CLOSE_TO_LIMIT;
324 case WebLocalizedString::ValidationTooLong:
325 return IDS_FORM_VALIDATION_TOO_LONG;
326 case WebLocalizedString::ValidationTypeMismatch:
327 return IDS_FORM_VALIDATION_TYPE_MISMATCH;
328 case WebLocalizedString::ValidationTypeMismatchForEmail:
329 return IDS_FORM_VALIDATION_TYPE_MISMATCH_EMAIL;
330 case WebLocalizedString::ValidationTypeMismatchForEmailEmpty:
331 return IDS_FORM_VALIDATION_TYPE_MISMATCH_EMAIL_EMPTY;
332 case WebLocalizedString::ValidationTypeMismatchForEmailEmptyDomain:
333 return IDS_FORM_VALIDATION_TYPE_MISMATCH_EMAIL_EMPTY_DOMAIN;
334 case WebLocalizedString::ValidationTypeMismatchForEmailEmptyLocal:
335 return IDS_FORM_VALIDATION_TYPE_MISMATCH_EMAIL_EMPTY_LOCAL;
336 case WebLocalizedString::ValidationTypeMismatchForEmailInvalidDomain:
337 return IDS_FORM_VALIDATION_TYPE_MISMATCH_EMAIL_INVALID_DOMAIN;
338 case WebLocalizedString::ValidationTypeMismatchForEmailInvalidDots:
339 return IDS_FORM_VALIDATION_TYPE_MISMATCH_EMAIL_INVALID_DOTS;
340 case WebLocalizedString::ValidationTypeMismatchForEmailInvalidLocal:
341 return IDS_FORM_VALIDATION_TYPE_MISMATCH_EMAIL_INVALID_LOCAL;
342 case WebLocalizedString::ValidationTypeMismatchForEmailNoAtSign:
343 return IDS_FORM_VALIDATION_TYPE_MISMATCH_EMAIL_NO_AT_SIGN;
344 case WebLocalizedString::ValidationTypeMismatchForMultipleEmail:
345 return IDS_FORM_VALIDATION_TYPE_MISMATCH_MULTIPLE_EMAIL;
346 case WebLocalizedString::ValidationTypeMismatchForURL:
347 return IDS_FORM_VALIDATION_TYPE_MISMATCH_URL;
348 case WebLocalizedString::ValidationValueMissing:
349 return IDS_FORM_VALIDATION_VALUE_MISSING;
350 case WebLocalizedString::ValidationValueMissingForCheckbox:
351 return IDS_FORM_VALIDATION_VALUE_MISSING_CHECKBOX;
352 case WebLocalizedString::ValidationValueMissingForFile:
353 return IDS_FORM_VALIDATION_VALUE_MISSING_FILE;
354 case WebLocalizedString::ValidationValueMissingForMultipleFile:
355 return IDS_FORM_VALIDATION_VALUE_MISSING_MULTIPLE_FILE;
356 case WebLocalizedString::ValidationValueMissingForRadio:
357 return IDS_FORM_VALIDATION_VALUE_MISSING_RADIO;
358 case WebLocalizedString::ValidationValueMissingForSelect:
359 return IDS_FORM_VALIDATION_VALUE_MISSING_SELECT;
360 case WebLocalizedString::WeekFormatTemplate:
361 return IDS_FORM_INPUT_WEEK_TEMPLATE;
362 case WebLocalizedString::WeekNumberLabel:
363 return IDS_FORM_WEEK_NUMBER_LABEL;
364 // This "default:" line exists to avoid compile warnings about enum
365 // coverage when we add a new symbol to WebLocalizedString.h in WebKit.
366 // After a planned WebKit patch is landed, we need to add a case statement
367 // for the added symbol here.
374 WebKitPlatformSupportImpl::WebKitPlatformSupportImpl()
375 : main_loop_(base::MessageLoop::current()),
376 shared_timer_func_(NULL),
377 shared_timer_fire_time_(0.0),
378 shared_timer_fire_time_was_set_while_suspended_(false),
379 shared_timer_suspended_(0) {}
381 WebKitPlatformSupportImpl::~WebKitPlatformSupportImpl() {
384 WebURLLoader* WebKitPlatformSupportImpl::createURLLoader() {
385 return new WebURLLoaderImpl(this);
388 WebSocketStreamHandle* WebKitPlatformSupportImpl::createSocketStreamHandle() {
389 return new WebSocketStreamHandleImpl(this);
392 WebString WebKitPlatformSupportImpl::userAgent(const WebURL& url) {
393 return WebString::fromUTF8(webkit_glue::GetUserAgent(url));
396 WebData WebKitPlatformSupportImpl::parseDataURL(
398 WebString& mimetype_out,
399 WebString& charset_out) {
400 std::string mime_type, char_set, data;
401 if (net::DataURL::Parse(url, &mime_type, &char_set, &data)
402 && net::IsSupportedMimeType(mime_type)) {
403 mimetype_out = WebString::fromUTF8(mime_type);
404 charset_out = WebString::fromUTF8(char_set);
410 WebURLError WebKitPlatformSupportImpl::cancelledError(
411 const WebURL& unreachableURL) const {
412 return WebURLLoaderImpl::CreateError(unreachableURL, net::ERR_ABORTED);
415 void WebKitPlatformSupportImpl::decrementStatsCounter(const char* name) {
416 base::StatsCounter(name).Decrement();
419 void WebKitPlatformSupportImpl::incrementStatsCounter(const char* name) {
420 base::StatsCounter(name).Increment();
423 void WebKitPlatformSupportImpl::histogramCustomCounts(
424 const char* name, int sample, int min, int max, int bucket_count) {
425 // Copied from histogram macro, but without the static variable caching
426 // the histogram because name is dynamic.
427 base::HistogramBase* counter =
428 base::Histogram::FactoryGet(name, min, max, bucket_count,
429 base::HistogramBase::kUmaTargetedHistogramFlag);
430 DCHECK_EQ(name, counter->histogram_name());
431 counter->Add(sample);
434 void WebKitPlatformSupportImpl::histogramEnumeration(
435 const char* name, int sample, int boundary_value) {
436 // Copied from histogram macro, but without the static variable caching
437 // the histogram because name is dynamic.
438 base::HistogramBase* counter =
439 base::LinearHistogram::FactoryGet(name, 1, boundary_value,
440 boundary_value + 1, base::HistogramBase::kUmaTargetedHistogramFlag);
441 DCHECK_EQ(name, counter->histogram_name());
442 counter->Add(sample);
445 void WebKitPlatformSupportImpl::histogramSparse(const char* name, int sample) {
446 // For sparse histograms, we can use the macro, as it does not incorporate a
448 UMA_HISTOGRAM_SPARSE_SLOWLY(name, sample);
451 const unsigned char* WebKitPlatformSupportImpl::getTraceCategoryEnabledFlag(
452 const char* category_group) {
453 return TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_group);
456 long* WebKitPlatformSupportImpl::getTraceSamplingState(
457 const unsigned thread_bucket) {
458 switch (thread_bucket) {
460 return reinterpret_cast<long*>(&TRACE_EVENT_API_THREAD_BUCKET(0));
462 return reinterpret_cast<long*>(&TRACE_EVENT_API_THREAD_BUCKET(1));
464 return reinterpret_cast<long*>(&TRACE_EVENT_API_THREAD_BUCKET(2));
466 NOTREACHED() << "Unknown thread bucket type.";
472 sizeof(WebKit::Platform::TraceEventHandle) ==
473 sizeof(base::debug::TraceEventHandle),
474 TraceEventHandle_types_must_be_same_size);
476 WebKit::Platform::TraceEventHandle WebKitPlatformSupportImpl::addTraceEvent(
478 const unsigned char* category_group_enabled,
480 unsigned long long id,
482 const char** arg_names,
483 const unsigned char* arg_types,
484 const unsigned long long* arg_values,
485 unsigned char flags) {
486 base::debug::TraceEventHandle handle = TRACE_EVENT_API_ADD_TRACE_EVENT(
487 phase, category_group_enabled, name, id,
488 num_args, arg_names, arg_types, arg_values, NULL, flags);
489 WebKit::Platform::TraceEventHandle result;
490 memcpy(&result, &handle, sizeof(result));
494 void WebKitPlatformSupportImpl::updateTraceEventDuration(
495 TraceEventHandle handle) {
496 base::debug::TraceEventHandle traceEventHandle;
497 memcpy(&traceEventHandle, &handle, sizeof(handle));
498 TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(traceEventHandle);
503 WebData loadAudioSpatializationResource(WebKitPlatformSupportImpl* platform,
505 #ifdef IDR_AUDIO_SPATIALIZATION_COMPOSITE
506 if (!strcmp(name, "Composite")) {
507 base::StringPiece resource =
508 platform->GetDataResource(IDR_AUDIO_SPATIALIZATION_COMPOSITE,
509 ui::SCALE_FACTOR_NONE);
510 return WebData(resource.data(), resource.size());
514 #ifdef IDR_AUDIO_SPATIALIZATION_T000_P000
515 const size_t kExpectedSpatializationNameLength = 31;
516 if (strlen(name) != kExpectedSpatializationNameLength) {
520 // Extract the azimuth and elevation from the resource name.
524 sscanf(name, "IRC_Composite_C_R0195_T%3d_P%3d", &azimuth, &elevation);
525 if (values_parsed != 2) {
529 // The resource index values go through the elevations first, then azimuths.
530 const int kAngleSpacing = 15;
532 // 0 <= elevation <= 90 (or 315 <= elevation <= 345)
533 // in increments of 15 degrees.
534 int elevation_index =
535 elevation <= 90 ? elevation / kAngleSpacing :
536 7 + (elevation - 315) / kAngleSpacing;
537 bool is_elevation_index_good = 0 <= elevation_index && elevation_index < 10;
539 // 0 <= azimuth < 360 in increments of 15 degrees.
540 int azimuth_index = azimuth / kAngleSpacing;
541 bool is_azimuth_index_good = 0 <= azimuth_index && azimuth_index < 24;
543 const int kNumberOfElevations = 10;
544 const int kNumberOfAudioResources = 240;
545 int resource_index = kNumberOfElevations * azimuth_index + elevation_index;
546 bool is_resource_index_good = 0 <= resource_index &&
547 resource_index < kNumberOfAudioResources;
549 if (is_azimuth_index_good && is_elevation_index_good &&
550 is_resource_index_good) {
551 const int kFirstAudioResourceIndex = IDR_AUDIO_SPATIALIZATION_T000_P000;
552 base::StringPiece resource =
553 platform->GetDataResource(kFirstAudioResourceIndex + resource_index,
554 ui::SCALE_FACTOR_NONE);
555 return WebData(resource.data(), resource.size());
557 #endif // IDR_AUDIO_SPATIALIZATION_T000_P000
563 struct DataResource {
566 ui::ScaleFactor scale_factor;
569 const DataResource kDataResources[] = {
570 { "missingImage", IDR_BROKENIMAGE, ui::SCALE_FACTOR_100P },
571 { "missingImage@2x", IDR_BROKENIMAGE, ui::SCALE_FACTOR_200P },
572 { "mediaplayerPause", IDR_MEDIAPLAYER_PAUSE_BUTTON, ui::SCALE_FACTOR_100P },
573 { "mediaplayerPauseHover",
574 IDR_MEDIAPLAYER_PAUSE_BUTTON_HOVER, ui::SCALE_FACTOR_100P },
575 { "mediaplayerPauseDown",
576 IDR_MEDIAPLAYER_PAUSE_BUTTON_DOWN, ui::SCALE_FACTOR_100P },
577 { "mediaplayerPlay", IDR_MEDIAPLAYER_PLAY_BUTTON, ui::SCALE_FACTOR_100P },
578 { "mediaplayerPlayHover",
579 IDR_MEDIAPLAYER_PLAY_BUTTON_HOVER, ui::SCALE_FACTOR_100P },
580 { "mediaplayerPlayDown",
581 IDR_MEDIAPLAYER_PLAY_BUTTON_DOWN, ui::SCALE_FACTOR_100P },
582 { "mediaplayerPlayDisabled",
583 IDR_MEDIAPLAYER_PLAY_BUTTON_DISABLED, ui::SCALE_FACTOR_100P },
584 { "mediaplayerSoundLevel3",
585 IDR_MEDIAPLAYER_SOUND_LEVEL3_BUTTON, ui::SCALE_FACTOR_100P },
586 { "mediaplayerSoundLevel3Hover",
587 IDR_MEDIAPLAYER_SOUND_LEVEL3_BUTTON_HOVER, ui::SCALE_FACTOR_100P },
588 { "mediaplayerSoundLevel3Down",
589 IDR_MEDIAPLAYER_SOUND_LEVEL3_BUTTON_DOWN, ui::SCALE_FACTOR_100P },
590 { "mediaplayerSoundLevel2",
591 IDR_MEDIAPLAYER_SOUND_LEVEL2_BUTTON, ui::SCALE_FACTOR_100P },
592 { "mediaplayerSoundLevel2Hover",
593 IDR_MEDIAPLAYER_SOUND_LEVEL2_BUTTON_HOVER, ui::SCALE_FACTOR_100P },
594 { "mediaplayerSoundLevel2Down",
595 IDR_MEDIAPLAYER_SOUND_LEVEL2_BUTTON_DOWN, ui::SCALE_FACTOR_100P },
596 { "mediaplayerSoundLevel1",
597 IDR_MEDIAPLAYER_SOUND_LEVEL1_BUTTON, ui::SCALE_FACTOR_100P },
598 { "mediaplayerSoundLevel1Hover",
599 IDR_MEDIAPLAYER_SOUND_LEVEL1_BUTTON_HOVER, ui::SCALE_FACTOR_100P },
600 { "mediaplayerSoundLevel1Down",
601 IDR_MEDIAPLAYER_SOUND_LEVEL1_BUTTON_DOWN, ui::SCALE_FACTOR_100P },
602 { "mediaplayerSoundLevel0",
603 IDR_MEDIAPLAYER_SOUND_LEVEL0_BUTTON, ui::SCALE_FACTOR_100P },
604 { "mediaplayerSoundLevel0Hover",
605 IDR_MEDIAPLAYER_SOUND_LEVEL0_BUTTON_HOVER, ui::SCALE_FACTOR_100P },
606 { "mediaplayerSoundLevel0Down",
607 IDR_MEDIAPLAYER_SOUND_LEVEL0_BUTTON_DOWN, ui::SCALE_FACTOR_100P },
608 { "mediaplayerSoundDisabled",
609 IDR_MEDIAPLAYER_SOUND_DISABLED, ui::SCALE_FACTOR_100P },
610 { "mediaplayerSliderThumb",
611 IDR_MEDIAPLAYER_SLIDER_THUMB, ui::SCALE_FACTOR_100P },
612 { "mediaplayerSliderThumbHover",
613 IDR_MEDIAPLAYER_SLIDER_THUMB_HOVER, ui::SCALE_FACTOR_100P },
614 { "mediaplayerSliderThumbDown",
615 IDR_MEDIAPLAYER_SLIDER_THUMB_DOWN, ui::SCALE_FACTOR_100P },
616 { "mediaplayerVolumeSliderThumb",
617 IDR_MEDIAPLAYER_VOLUME_SLIDER_THUMB, ui::SCALE_FACTOR_100P },
618 { "mediaplayerVolumeSliderThumbHover",
619 IDR_MEDIAPLAYER_VOLUME_SLIDER_THUMB_HOVER, ui::SCALE_FACTOR_100P },
620 { "mediaplayerVolumeSliderThumbDown",
621 IDR_MEDIAPLAYER_VOLUME_SLIDER_THUMB_DOWN, ui::SCALE_FACTOR_100P },
622 { "mediaplayerVolumeSliderThumbDisabled",
623 IDR_MEDIAPLAYER_VOLUME_SLIDER_THUMB_DISABLED, ui::SCALE_FACTOR_100P },
624 { "mediaplayerClosedCaption",
625 IDR_MEDIAPLAYER_CLOSEDCAPTION_BUTTON, ui::SCALE_FACTOR_100P },
626 { "mediaplayerClosedCaptionHover",
627 IDR_MEDIAPLAYER_CLOSEDCAPTION_BUTTON_HOVER, ui::SCALE_FACTOR_100P },
628 { "mediaplayerClosedCaptionDown",
629 IDR_MEDIAPLAYER_CLOSEDCAPTION_BUTTON_DOWN, ui::SCALE_FACTOR_100P },
630 { "mediaplayerClosedCaptionDisabled",
631 IDR_MEDIAPLAYER_CLOSEDCAPTION_BUTTON_DISABLED, ui::SCALE_FACTOR_100P },
632 { "mediaplayerFullscreen",
633 IDR_MEDIAPLAYER_FULLSCREEN_BUTTON, ui::SCALE_FACTOR_100P },
634 { "mediaplayerFullscreenHover",
635 IDR_MEDIAPLAYER_FULLSCREEN_BUTTON_HOVER, ui::SCALE_FACTOR_100P },
636 { "mediaplayerFullscreenDown",
637 IDR_MEDIAPLAYER_FULLSCREEN_BUTTON_DOWN, ui::SCALE_FACTOR_100P },
638 { "mediaplayerFullscreenDisabled",
639 IDR_MEDIAPLAYER_FULLSCREEN_BUTTON_DISABLED, ui::SCALE_FACTOR_100P },
640 #if defined(OS_ANDROID)
641 { "mediaplayerOverlayPlay",
642 IDR_MEDIAPLAYER_OVERLAY_PLAY_BUTTON, ui::SCALE_FACTOR_100P },
644 #if defined(OS_MACOSX)
645 { "overhangPattern", IDR_OVERHANG_PATTERN, ui::SCALE_FACTOR_100P },
646 { "overhangShadow", IDR_OVERHANG_SHADOW, ui::SCALE_FACTOR_100P },
648 { "panIcon", IDR_PAN_SCROLL_ICON, ui::SCALE_FACTOR_100P },
649 { "searchCancel", IDR_SEARCH_CANCEL, ui::SCALE_FACTOR_100P },
650 { "searchCancelPressed", IDR_SEARCH_CANCEL_PRESSED, ui::SCALE_FACTOR_100P },
651 { "searchMagnifier", IDR_SEARCH_MAGNIFIER, ui::SCALE_FACTOR_100P },
652 { "searchMagnifierResults",
653 IDR_SEARCH_MAGNIFIER_RESULTS, ui::SCALE_FACTOR_100P },
654 { "textAreaResizeCorner", IDR_TEXTAREA_RESIZER, ui::SCALE_FACTOR_100P },
655 { "textAreaResizeCorner@2x", IDR_TEXTAREA_RESIZER, ui::SCALE_FACTOR_200P },
656 { "inputSpeech", IDR_INPUT_SPEECH, ui::SCALE_FACTOR_100P },
657 { "inputSpeechRecording", IDR_INPUT_SPEECH_RECORDING, ui::SCALE_FACTOR_100P },
658 { "inputSpeechWaiting", IDR_INPUT_SPEECH_WAITING, ui::SCALE_FACTOR_100P },
659 { "americanExpressCC", IDR_AUTOFILL_CC_AMEX, ui::SCALE_FACTOR_100P },
660 { "dinersCC", IDR_AUTOFILL_CC_DINERS, ui::SCALE_FACTOR_100P },
661 { "discoverCC", IDR_AUTOFILL_CC_DISCOVER, ui::SCALE_FACTOR_100P },
662 { "genericCC", IDR_AUTOFILL_CC_GENERIC, ui::SCALE_FACTOR_100P },
663 { "jcbCC", IDR_AUTOFILL_CC_JCB, ui::SCALE_FACTOR_100P },
664 { "masterCardCC", IDR_AUTOFILL_CC_MASTERCARD, ui::SCALE_FACTOR_100P },
665 { "visaCC", IDR_AUTOFILL_CC_VISA, ui::SCALE_FACTOR_100P },
666 { "generatePassword", IDR_PASSWORD_GENERATION_ICON, ui::SCALE_FACTOR_100P },
667 { "generatePasswordHover",
668 IDR_PASSWORD_GENERATION_ICON_HOVER, ui::SCALE_FACTOR_100P },
669 { "syntheticTouchCursor",
670 IDR_SYNTHETIC_TOUCH_CURSOR, ui::SCALE_FACTOR_100P },
675 WebData WebKitPlatformSupportImpl::loadResource(const char* name) {
676 // Some clients will call into this method with an empty |name| when they have
677 // optional resources. For example, the PopupMenuChromium code can have icons
678 // for some Autofill items but not for others.
682 // Check the name prefix to see if it's an audio resource.
683 if (StartsWithASCII(name, "IRC_Composite", true) ||
684 StartsWithASCII(name, "Composite", true))
685 return loadAudioSpatializationResource(this, name);
687 // TODO(flackr): We should use a better than linear search here, a trie would
689 for (size_t i = 0; i < arraysize(kDataResources); ++i) {
690 if (!strcmp(name, kDataResources[i].name)) {
691 base::StringPiece resource =
692 GetDataResource(kDataResources[i].id,
693 kDataResources[i].scale_factor);
694 return WebData(resource.data(), resource.size());
698 NOTREACHED() << "Unknown image resource " << name;
702 WebString WebKitPlatformSupportImpl::queryLocalizedString(
703 WebLocalizedString::Name name) {
704 int message_id = ToMessageID(name);
707 return GetLocalizedString(message_id);
710 WebString WebKitPlatformSupportImpl::queryLocalizedString(
711 WebLocalizedString::Name name, int numeric_value) {
712 return queryLocalizedString(name, base::IntToString16(numeric_value));
715 WebString WebKitPlatformSupportImpl::queryLocalizedString(
716 WebLocalizedString::Name name, const WebString& value) {
717 int message_id = ToMessageID(name);
720 return ReplaceStringPlaceholders(GetLocalizedString(message_id), value, NULL);
723 WebString WebKitPlatformSupportImpl::queryLocalizedString(
724 WebLocalizedString::Name name,
725 const WebString& value1,
726 const WebString& value2) {
727 int message_id = ToMessageID(name);
730 std::vector<base::string16> values;
732 values.push_back(value1);
733 values.push_back(value2);
734 return ReplaceStringPlaceholders(
735 GetLocalizedString(message_id), values, NULL);
738 double WebKitPlatformSupportImpl::currentTime() {
739 return base::Time::Now().ToDoubleT();
742 double WebKitPlatformSupportImpl::monotonicallyIncreasingTime() {
743 return base::TimeTicks::Now().ToInternalValue() /
744 static_cast<double>(base::Time::kMicrosecondsPerSecond);
747 void WebKitPlatformSupportImpl::cryptographicallyRandomValues(
748 unsigned char* buffer, size_t length) {
749 base::RandBytes(buffer, length);
752 void WebKitPlatformSupportImpl::setSharedTimerFiredFunction(void (*func)()) {
753 shared_timer_func_ = func;
756 void WebKitPlatformSupportImpl::setSharedTimerFireInterval(
757 double interval_seconds) {
758 shared_timer_fire_time_ = interval_seconds + monotonicallyIncreasingTime();
759 if (shared_timer_suspended_) {
760 shared_timer_fire_time_was_set_while_suspended_ = true;
764 // By converting between double and int64 representation, we run the risk
765 // of losing precision due to rounding errors. Performing computations in
766 // microseconds reduces this risk somewhat. But there still is the potential
767 // of us computing a fire time for the timer that is shorter than what we
769 // As the event loop will check event deadlines prior to actually firing
770 // them, there is a risk of needlessly rescheduling events and of
771 // needlessly looping if sleep times are too short even by small amounts.
772 // This results in measurable performance degradation unless we use ceil() to
773 // always round up the sleep times.
774 int64 interval = static_cast<int64>(
775 ceil(interval_seconds * base::Time::kMillisecondsPerSecond)
776 * base::Time::kMicrosecondsPerMillisecond);
781 shared_timer_.Stop();
782 shared_timer_.Start(FROM_HERE, base::TimeDelta::FromMicroseconds(interval),
783 this, &WebKitPlatformSupportImpl::DoTimeout);
784 OnStartSharedTimer(base::TimeDelta::FromMicroseconds(interval));
787 void WebKitPlatformSupportImpl::stopSharedTimer() {
788 shared_timer_.Stop();
791 void WebKitPlatformSupportImpl::callOnMainThread(
792 void (*func)(void*), void* context) {
793 main_loop_->PostTask(FROM_HERE, base::Bind(func, context));
796 base::PlatformFile WebKitPlatformSupportImpl::databaseOpenFile(
797 const WebKit::WebString& vfs_file_name, int desired_flags) {
798 return base::kInvalidPlatformFileValue;
801 int WebKitPlatformSupportImpl::databaseDeleteFile(
802 const WebKit::WebString& vfs_file_name, bool sync_dir) {
806 long WebKitPlatformSupportImpl::databaseGetFileAttributes(
807 const WebKit::WebString& vfs_file_name) {
811 long long WebKitPlatformSupportImpl::databaseGetFileSize(
812 const WebKit::WebString& vfs_file_name) {
816 long long WebKitPlatformSupportImpl::databaseGetSpaceAvailableForOrigin(
817 const WebKit::WebString& origin_identifier) {
821 WebKit::WebString WebKitPlatformSupportImpl::signedPublicKeyAndChallengeString(
822 unsigned key_size_index,
823 const WebKit::WebString& challenge,
824 const WebKit::WebURL& url) {
825 return WebKit::WebString("");
828 static scoped_ptr<base::ProcessMetrics> CurrentProcessMetrics() {
829 using base::ProcessMetrics;
830 #if defined(OS_MACOSX)
831 return scoped_ptr<ProcessMetrics>(
832 // The default port provider is sufficient to get data for the current
834 ProcessMetrics::CreateProcessMetrics(base::GetCurrentProcessHandle(),
837 return scoped_ptr<ProcessMetrics>(
838 ProcessMetrics::CreateProcessMetrics(base::GetCurrentProcessHandle()));
842 static size_t getMemoryUsageMB(bool bypass_cache) {
843 size_t current_mem_usage = 0;
844 MemoryUsageCache* mem_usage_cache_singleton = MemoryUsageCache::GetInstance();
846 mem_usage_cache_singleton->IsCachedValueValid(¤t_mem_usage))
847 return current_mem_usage;
849 current_mem_usage = MemoryUsageKB() >> 10;
850 mem_usage_cache_singleton->SetMemoryValue(current_mem_usage);
851 return current_mem_usage;
854 size_t WebKitPlatformSupportImpl::memoryUsageMB() {
855 return getMemoryUsageMB(false);
858 size_t WebKitPlatformSupportImpl::actualMemoryUsageMB() {
859 return getMemoryUsageMB(true);
862 void WebKitPlatformSupportImpl::startHeapProfiling(
863 const WebKit::WebString& prefix) {
864 // FIXME(morrita): Make this built on windows.
865 #if !defined(NO_TCMALLOC) && defined(USE_TCMALLOC) && !defined(OS_WIN)
866 HeapProfilerStart(prefix.utf8().data());
870 void WebKitPlatformSupportImpl::stopHeapProfiling() {
871 #if !defined(NO_TCMALLOC) && defined(USE_TCMALLOC) && !defined(OS_WIN)
876 void WebKitPlatformSupportImpl::dumpHeapProfiling(
877 const WebKit::WebString& reason) {
878 #if !defined(NO_TCMALLOC) && defined(USE_TCMALLOC) && !defined(OS_WIN)
879 HeapProfilerDump(reason.utf8().data());
883 WebString WebKitPlatformSupportImpl::getHeapProfile() {
884 #if !defined(NO_TCMALLOC) && defined(USE_TCMALLOC) && !defined(OS_WIN)
885 char* data = GetHeapProfile();
886 WebString result = WebString::fromUTF8(std::string(data));
894 bool WebKitPlatformSupportImpl::processMemorySizesInBytes(
895 size_t* private_bytes,
896 size_t* shared_bytes) {
897 return CurrentProcessMetrics()->GetMemoryBytes(private_bytes, shared_bytes);
900 bool WebKitPlatformSupportImpl::memoryAllocatorWasteInBytes(size_t* size) {
901 return base::allocator::GetAllocatorWasteSize(size);
904 size_t WebKitPlatformSupportImpl::maxDecodedImageBytes() {
905 #if defined(OS_ANDROID)
906 if (base::android::SysUtils::IsLowEndDevice()) {
907 // Limit image decoded size to 3M pixels on low end devices.
908 // 4 is maximum number of bytes per pixel.
909 return 3 * 1024 * 1024 * 4;
911 // For other devices, limit decoded image size based on the amount of physical
912 // memory. For a device with 2GB physical memory the limit is 16M pixels.
913 return base::SysInfo::AmountOfPhysicalMemory() / 32;
915 return noDecodedImageByteLimit;
919 void WebKitPlatformSupportImpl::SuspendSharedTimer() {
920 ++shared_timer_suspended_;
923 void WebKitPlatformSupportImpl::ResumeSharedTimer() {
924 // The shared timer may have fired or been adjusted while we were suspended.
925 if (--shared_timer_suspended_ == 0 &&
926 (!shared_timer_.IsRunning() ||
927 shared_timer_fire_time_was_set_while_suspended_)) {
928 shared_timer_fire_time_was_set_while_suspended_ = false;
929 setSharedTimerFireInterval(
930 shared_timer_fire_time_ - monotonicallyIncreasingTime());
934 } // namespace webkit_glue