OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/renderer/searchbox_extension.h" | 5 #include "chrome/renderer/searchbox_extension.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 return v8::Undefined(); | 347 return v8::Undefined(); |
348 } | 348 } |
349 | 349 |
350 // static | 350 // static |
351 bool Dispatch(WebFrame* frame, const std::string& event_name) { | 351 bool Dispatch(WebFrame* frame, const std::string& event_name) { |
352 DCHECK(frame) << "Dispatch requires frame"; | 352 DCHECK(frame) << "Dispatch requires frame"; |
353 if (!frame) return false; | 353 if (!frame) return false; |
354 | 354 |
355 v8::HandleScope handle_scope; | 355 v8::HandleScope handle_scope; |
356 v8::Local<v8::Context> context = frame->mainWorldScriptContext(); | 356 v8::Local<v8::Context> context = frame->mainWorldScriptContext(); |
| 357 if (context.IsEmpty()) |
| 358 return false; |
357 v8::Context::Scope context_scope(context); | 359 v8::Context::Scope context_scope(context); |
358 | 360 |
359 v8::Local<v8::Value> value = | 361 v8::Local<v8::Value> value = |
360 context->Global()->Get(v8::String::New("window")); | 362 context->Global()->Get(v8::String::New("window")); |
361 std::vector<std::string> components; | 363 std::vector<std::string> components; |
362 base::SplitStringDontTrim(event_name, '.', &components); | 364 base::SplitStringDontTrim(event_name, '.', &components); |
363 for (size_t i = 0; i < components.size(); ++i) { | 365 for (size_t i = 0; i < components.size(); ++i) { |
364 if (!value.IsEmpty() && value->IsObject()) | 366 if (!value.IsEmpty() && value->IsObject()) |
365 value = value->ToObject()->Get(v8::String::New(components[i].c_str())); | 367 value = value->ToObject()->Get(v8::String::New(components[i].c_str())); |
366 } | 368 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 if (Dispatch(frame, kResizeEventName)) | 403 if (Dispatch(frame, kResizeEventName)) |
402 return; | 404 return; |
403 frame->executeScript(WebScriptSource(kSetOmniboxBoundsScript)); | 405 frame->executeScript(WebScriptSource(kSetOmniboxBoundsScript)); |
404 } | 406 } |
405 | 407 |
406 // static | 408 // static |
407 bool SearchBoxExtension::PageSupportsInstant(WebFrame* frame) { | 409 bool SearchBoxExtension::PageSupportsInstant(WebFrame* frame) { |
408 DCHECK(frame) << "PageSupportsInstant requires frame"; | 410 DCHECK(frame) << "PageSupportsInstant requires frame"; |
409 if (!frame) return false; | 411 if (!frame) return false; |
410 | 412 |
411 bool supports_deprecated_api = frame->executeScriptAndReturnValue( | 413 v8::Handle<v8::Value> v = frame->executeScriptAndReturnValue( |
412 WebScriptSource(kSupportsInstantScript))->BooleanValue(); | 414 WebScriptSource(kSupportsInstantScript)); |
| 415 bool supports_deprecated_api = !v.IsEmpty() && v->BooleanValue(); |
413 // TODO(tonyg): Add way of detecting instant support to SearchBox API. | 416 // TODO(tonyg): Add way of detecting instant support to SearchBox API. |
414 bool supports_searchbox_api = supports_deprecated_api; | 417 bool supports_searchbox_api = supports_deprecated_api; |
415 | 418 |
416 // The deprecated API needs to notify the page of events it may have missed. | 419 // The deprecated API needs to notify the page of events it may have missed. |
417 // This isn't necessary in the SearchBox API, since the page can query the | 420 // This isn't necessary in the SearchBox API, since the page can query the |
418 // API at any time. | 421 // API at any time. |
419 static std::string init_script( | 422 static std::string init_script( |
420 StringPrintf(kInitScript, kSetOmniboxBoundsScript, kUserInputScript)); | 423 StringPrintf(kInitScript, kSetOmniboxBoundsScript, kUserInputScript)); |
421 if (supports_deprecated_api) { | 424 if (supports_deprecated_api) { |
422 frame->executeScript(WebScriptSource(WebString::fromUTF8(init_script))); | 425 frame->executeScript(WebScriptSource(WebString::fromUTF8(init_script))); |
423 } | 426 } |
424 | 427 |
425 return supports_searchbox_api || supports_deprecated_api; | 428 return supports_searchbox_api || supports_deprecated_api; |
426 } | 429 } |
427 | 430 |
428 // static | 431 // static |
429 v8::Extension* SearchBoxExtension::Get() { | 432 v8::Extension* SearchBoxExtension::Get() { |
430 return new SearchBoxExtensionWrapper(); | 433 return new SearchBoxExtensionWrapper(); |
431 } | 434 } |
432 | 435 |
433 } // namespace extensions_v8 | 436 } // namespace extensions_v8 |
OLD | NEW |