OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/browser/extensions/extension_function_dispatcher.h" | 5 #include "chrome/browser/extensions/extension_function_registry.h" |
6 | 6 |
7 #include <map> | |
8 | |
9 #include "base/json/json_string_value_serializer.h" | |
10 #include "base/memory/ref_counted.h" | |
11 #include "base/memory/singleton.h" | |
12 #include "base/process_util.h" | |
13 #include "base/values.h" | |
14 #include "build/build_config.h" | |
15 #include "chrome/browser/accessibility/accessibility_extension_api.h" | 7 #include "chrome/browser/accessibility/accessibility_extension_api.h" |
16 #include "chrome/browser/bookmarks/bookmark_extension_api.h" | 8 #include "chrome/browser/bookmarks/bookmark_extension_api.h" |
17 #include "chrome/browser/bookmarks/bookmark_manager_extension_api.h" | 9 #include "chrome/browser/bookmarks/bookmark_manager_extension_api.h" |
18 #include "chrome/browser/download/download_extension_api.h" | 10 #include "chrome/browser/download/download_extension_api.h" |
19 #include "chrome/browser/extensions/api/app/app_api.h" | 11 #include "chrome/browser/extensions/api/app/app_api.h" |
20 #include "chrome/browser/extensions/api/browsing_data/browsing_data_api.h" | 12 #include "chrome/browser/extensions/api/browsing_data/browsing_data_api.h" |
21 #include "chrome/browser/extensions/api/declarative/declarative_api.h" | 13 #include "chrome/browser/extensions/api/declarative/declarative_api.h" |
22 #include "chrome/browser/extensions/api/dns/dns_api.h" | |
23 #include "chrome/browser/extensions/api/extension_action/extension_browser_actio
ns_api.h" | 14 #include "chrome/browser/extensions/api/extension_action/extension_browser_actio
ns_api.h" |
24 #include "chrome/browser/extensions/api/extension_action/extension_page_actions_
api.h" | 15 #include "chrome/browser/extensions/api/extension_action/extension_page_actions_
api.h" |
25 #include "chrome/browser/extensions/api/permissions/permissions_api.h" | 16 #include "chrome/browser/extensions/api/permissions/permissions_api.h" |
26 #include "chrome/browser/extensions/api/serial/serial_api.h" | 17 #include "chrome/browser/extensions/api/serial/serial_api.h" |
27 #include "chrome/browser/extensions/api/socket/socket_api.h" | 18 #include "chrome/browser/extensions/api/socket/socket_api.h" |
28 #include "chrome/browser/extensions/api/web_request/web_request_api.h" | 19 #include "chrome/browser/extensions/api/web_request/web_request_api.h" |
29 #include "chrome/browser/extensions/execute_code_in_tab_function.h" | 20 #include "chrome/browser/extensions/execute_code_in_tab_function.h" |
30 #include "chrome/browser/extensions/extension_activity_log.h" | |
31 #include "chrome/browser/extensions/extension_chrome_auth_private_api.h" | 21 #include "chrome/browser/extensions/extension_chrome_auth_private_api.h" |
32 #include "chrome/browser/extensions/extension_content_settings_api.h" | 22 #include "chrome/browser/extensions/extension_content_settings_api.h" |
33 #include "chrome/browser/extensions/extension_context_menu_api.h" | 23 #include "chrome/browser/extensions/extension_context_menu_api.h" |
34 #include "chrome/browser/extensions/extension_cookies_api.h" | 24 #include "chrome/browser/extensions/extension_cookies_api.h" |
35 #include "chrome/browser/extensions/extension_debugger_api.h" | 25 #include "chrome/browser/extensions/extension_debugger_api.h" |
36 #include "chrome/browser/extensions/extension_font_settings_api.h" | 26 #include "chrome/browser/extensions/extension_font_settings_api.h" |
37 #include "chrome/browser/extensions/extension_function.h" | |
38 #include "chrome/browser/extensions/extension_i18n_api.h" | 27 #include "chrome/browser/extensions/extension_i18n_api.h" |
39 #include "chrome/browser/extensions/extension_idle_api.h" | 28 #include "chrome/browser/extensions/extension_idle_api.h" |
40 #include "chrome/browser/extensions/extension_managed_mode_api.h" | 29 #include "chrome/browser/extensions/extension_managed_mode_api.h" |
41 #include "chrome/browser/extensions/extension_management_api.h" | 30 #include "chrome/browser/extensions/extension_management_api.h" |
42 #include "chrome/browser/extensions/extension_metrics_module.h" | 31 #include "chrome/browser/extensions/extension_metrics_module.h" |
43 #include "chrome/browser/extensions/extension_module.h" | 32 #include "chrome/browser/extensions/extension_module.h" |
44 #include "chrome/browser/extensions/extension_omnibox_api.h" | 33 #include "chrome/browser/extensions/extension_omnibox_api.h" |
45 #include "chrome/browser/extensions/extension_page_capture_api.h" | 34 #include "chrome/browser/extensions/extension_page_capture_api.h" |
46 #include "chrome/browser/extensions/extension_preference_api.h" | 35 #include "chrome/browser/extensions/extension_preference_api.h" |
47 #include "chrome/browser/extensions/extension_processes_api.h" | 36 #include "chrome/browser/extensions/extension_processes_api.h" |
48 #include "chrome/browser/extensions/extension_service.h" | |
49 #include "chrome/browser/extensions/extension_tabs_module.h" | 37 #include "chrome/browser/extensions/extension_tabs_module.h" |
50 #include "chrome/browser/extensions/extension_test_api.h" | 38 #include "chrome/browser/extensions/extension_test_api.h" |
51 #include "chrome/browser/extensions/extension_tts_api.h" | 39 #include "chrome/browser/extensions/extension_tts_api.h" |
52 #include "chrome/browser/extensions/extension_tts_engine_api.h" | 40 #include "chrome/browser/extensions/extension_tts_engine_api.h" |
53 #include "chrome/browser/extensions/extension_web_socket_proxy_private_api.h" | 41 #include "chrome/browser/extensions/extension_web_socket_proxy_private_api.h" |
54 #include "chrome/browser/extensions/extension_web_ui.h" | |
55 #include "chrome/browser/extensions/extension_webnavigation_api.h" | 42 #include "chrome/browser/extensions/extension_webnavigation_api.h" |
56 #include "chrome/browser/extensions/extension_webstore_private_api.h" | 43 #include "chrome/browser/extensions/extension_webstore_private_api.h" |
57 #include "chrome/browser/extensions/extensions_quota_service.h" | |
58 #include "chrome/browser/extensions/process_map.h" | |
59 #include "chrome/browser/extensions/settings/settings_api.h" | 44 #include "chrome/browser/extensions/settings/settings_api.h" |
60 #include "chrome/browser/extensions/system/system_api.h" | 45 #include "chrome/browser/extensions/system/system_api.h" |
61 #include "chrome/browser/external_protocol/external_protocol_handler.h" | |
62 #include "chrome/browser/history/history_extension_api.h" | 46 #include "chrome/browser/history/history_extension_api.h" |
63 #include "chrome/browser/history/top_sites_extension_api.h" | 47 #include "chrome/browser/history/top_sites_extension_api.h" |
64 #include "chrome/browser/infobars/infobar_extension_api.h" | 48 #include "chrome/browser/infobars/infobar_extension_api.h" |
65 #include "chrome/browser/profiles/profile.h" | |
66 #include "chrome/browser/renderer_host/chrome_render_message_filter.h" | |
67 #include "chrome/browser/rlz/rlz_extension_api.h" | 49 #include "chrome/browser/rlz/rlz_extension_api.h" |
68 #include "chrome/browser/speech/speech_input_extension_api.h" | 50 #include "chrome/browser/speech/speech_input_extension_api.h" |
69 #include "chrome/browser/ui/browser_list.h" | 51 #include "chrome/common/extensions/api/generated_api.h" |
70 #include "chrome/browser/ui/browser_window.h" | |
71 #include "chrome/common/extensions/api/extension_api.h" | |
72 #include "chrome/common/extensions/extension_messages.h" | |
73 #include "chrome/common/extensions/extension_set.h" | |
74 #include "chrome/common/url_constants.h" | |
75 #include "content/public/browser/render_process_host.h" | |
76 #include "content/public/browser/render_view_host.h" | |
77 #include "ipc/ipc_message.h" | |
78 #include "ipc/ipc_message_macros.h" | |
79 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" | |
80 #include "third_party/skia/include/core/SkBitmap.h" | |
81 | 52 |
82 #if defined(TOOLKIT_VIEWS) | 53 #if defined(TOOLKIT_VIEWS) |
83 #include "chrome/browser/extensions/extension_input_api.h" | 54 #include "chrome/browser/extensions/extension_input_api.h" |
84 #endif | 55 #endif |
85 | 56 |
86 #if defined(OS_CHROMEOS) && defined(USE_VIRTUAL_KEYBOARD) | 57 #if defined(OS_CHROMEOS) && defined(USE_VIRTUAL_KEYBOARD) |
87 #include "chrome/browser/extensions/extension_input_ui_api.h" | 58 #include "chrome/browser/extensions/extension_input_ui_api.h" |
88 #endif | 59 #endif |
89 | 60 |
90 #if defined(OS_CHROMEOS) | 61 #if defined(OS_CHROMEOS) |
91 #include "chrome/browser/chromeos/extensions/file_browser_private_api.h" | 62 #include "chrome/browser/chromeos/extensions/file_browser_private_api.h" |
92 #include "chrome/browser/chromeos/media/media_player_extension_api.h" | 63 #include "chrome/browser/chromeos/media/media_player_extension_api.h" |
93 #include "chrome/browser/extensions/api/terminal/terminal_private_api.h" | 64 #include "chrome/browser/extensions/api/terminal/terminal_private_api.h" |
94 #include "chrome/browser/extensions/extension_info_private_api_chromeos.h" | 65 #include "chrome/browser/extensions/extension_info_private_api_chromeos.h" |
95 #include "chrome/browser/extensions/extension_input_ime_api.h" | 66 #include "chrome/browser/extensions/extension_input_ime_api.h" |
96 #include "chrome/browser/extensions/extension_input_method_api.h" | 67 #include "chrome/browser/extensions/extension_input_method_api.h" |
97 #endif | 68 #endif |
98 | 69 |
99 using extensions::ExtensionAPI; | 70 // static |
100 using content::RenderViewHost; | 71 ExtensionFunctionRegistry* ExtensionFunctionRegistry::GetInstance() { |
101 using WebKit::WebSecurityOrigin; | 72 return Singleton<ExtensionFunctionRegistry>::get(); |
102 | |
103 // FactoryRegistry ------------------------------------------------------------- | |
104 | |
105 namespace { | |
106 | |
107 // Template for defining ExtensionFunctionFactory. | |
108 template<class T> | |
109 ExtensionFunction* NewExtensionFunction() { | |
110 return new T(); | |
111 } | 73 } |
112 | 74 |
113 // Contains a list of all known extension functions and allows clients to | 75 ExtensionFunctionRegistry::ExtensionFunctionRegistry() { |
114 // create instances of them. | 76 ResetFunctions(); |
115 class FactoryRegistry { | |
116 public: | |
117 static FactoryRegistry* GetInstance(); | |
118 FactoryRegistry() { ResetFunctions(); } | |
119 | |
120 // Resets all functions to their default values. | |
121 void ResetFunctions(); | |
122 | |
123 // Adds all function names to 'names'. | |
124 void GetAllNames(std::vector<std::string>* names); | |
125 | |
126 // Allows overriding of specific functions (e.g. for testing). Functions | |
127 // must be previously registered. Returns true if successful. | |
128 bool OverrideFunction(const std::string& name, | |
129 ExtensionFunctionFactory factory); | |
130 | |
131 // Factory method for the ExtensionFunction registered as 'name'. | |
132 ExtensionFunction* NewFunction(const std::string& name); | |
133 | |
134 private: | |
135 template<class T> | |
136 void RegisterFunction() { | |
137 factories_[T::function_name()] = &NewExtensionFunction<T>; | |
138 } | |
139 | |
140 typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap; | |
141 FactoryMap factories_; | |
142 }; | |
143 | |
144 FactoryRegistry* FactoryRegistry::GetInstance() { | |
145 return Singleton<FactoryRegistry>::get(); | |
146 } | 77 } |
147 | 78 |
148 void FactoryRegistry::ResetFunctions() { | 79 ExtensionFunctionRegistry::~ExtensionFunctionRegistry() { |
| 80 } |
| 81 |
| 82 void ExtensionFunctionRegistry::ResetFunctions() { |
149 // Register all functions here. | 83 // Register all functions here. |
150 | 84 |
151 // Windows | 85 // Windows |
152 RegisterFunction<GetWindowFunction>(); | 86 RegisterFunction<GetWindowFunction>(); |
153 RegisterFunction<GetCurrentWindowFunction>(); | 87 RegisterFunction<GetCurrentWindowFunction>(); |
154 RegisterFunction<GetLastFocusedWindowFunction>(); | 88 RegisterFunction<GetLastFocusedWindowFunction>(); |
155 RegisterFunction<GetAllWindowsFunction>(); | 89 RegisterFunction<GetAllWindowsFunction>(); |
156 RegisterFunction<CreateWindowFunction>(); | 90 RegisterFunction<CreateWindowFunction>(); |
157 RegisterFunction<UpdateWindowFunction>(); | 91 RegisterFunction<UpdateWindowFunction>(); |
158 RegisterFunction<RemoveWindowFunction>(); | 92 RegisterFunction<RemoveWindowFunction>(); |
(...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
516 RegisterFunction<extensions::SerialWriteFunction>(); | 450 RegisterFunction<extensions::SerialWriteFunction>(); |
517 | 451 |
518 // Sockets | 452 // Sockets |
519 RegisterFunction<extensions::SocketCreateFunction>(); | 453 RegisterFunction<extensions::SocketCreateFunction>(); |
520 RegisterFunction<extensions::SocketDestroyFunction>(); | 454 RegisterFunction<extensions::SocketDestroyFunction>(); |
521 RegisterFunction<extensions::SocketConnectFunction>(); | 455 RegisterFunction<extensions::SocketConnectFunction>(); |
522 RegisterFunction<extensions::SocketDisconnectFunction>(); | 456 RegisterFunction<extensions::SocketDisconnectFunction>(); |
523 RegisterFunction<extensions::SocketReadFunction>(); | 457 RegisterFunction<extensions::SocketReadFunction>(); |
524 RegisterFunction<extensions::SocketWriteFunction>(); | 458 RegisterFunction<extensions::SocketWriteFunction>(); |
525 | 459 |
526 // DNS | |
527 RegisterFunction<extensions::DNSResolveFunction>(); | |
528 | |
529 // System | 460 // System |
530 RegisterFunction<extensions::GetIncognitoModeAvailabilityFunction>(); | 461 RegisterFunction<extensions::GetIncognitoModeAvailabilityFunction>(); |
531 RegisterFunction<extensions::GetUpdateStatusFunction>(); | 462 RegisterFunction<extensions::GetUpdateStatusFunction>(); |
532 | 463 |
533 // Net | 464 // Net |
534 RegisterFunction<extensions::AddRulesFunction>(); | 465 RegisterFunction<extensions::AddRulesFunction>(); |
535 RegisterFunction<extensions::RemoveRulesFunction>(); | 466 RegisterFunction<extensions::RemoveRulesFunction>(); |
536 RegisterFunction<extensions::GetRulesFunction>(); | 467 RegisterFunction<extensions::GetRulesFunction>(); |
| 468 |
| 469 // Generated APIs |
| 470 extensions::api::GeneratedFunctionRegistry::RegisterAll(this); |
537 } | 471 } |
538 | 472 |
539 void FactoryRegistry::GetAllNames(std::vector<std::string>* names) { | 473 void ExtensionFunctionRegistry::GetAllNames(std::vector<std::string>* names) { |
540 for (FactoryMap::iterator iter = factories_.begin(); | 474 for (FactoryMap::iterator iter = factories_.begin(); |
541 iter != factories_.end(); ++iter) { | 475 iter != factories_.end(); ++iter) { |
542 names->push_back(iter->first); | 476 names->push_back(iter->first); |
543 } | 477 } |
544 } | 478 } |
545 | 479 |
546 bool FactoryRegistry::OverrideFunction(const std::string& name, | 480 bool ExtensionFunctionRegistry::OverrideFunction( |
547 ExtensionFunctionFactory factory) { | 481 const std::string& name, |
| 482 ExtensionFunctionFactory factory) { |
548 FactoryMap::iterator iter = factories_.find(name); | 483 FactoryMap::iterator iter = factories_.find(name); |
549 if (iter == factories_.end()) { | 484 if (iter == factories_.end()) { |
550 return false; | 485 return false; |
551 } else { | 486 } else { |
552 iter->second = factory; | 487 iter->second = factory; |
553 return true; | 488 return true; |
554 } | 489 } |
555 } | 490 } |
556 | 491 |
557 ExtensionFunction* FactoryRegistry::NewFunction(const std::string& name) { | 492 ExtensionFunction* ExtensionFunctionRegistry::NewFunction( |
| 493 const std::string& name) { |
558 FactoryMap::iterator iter = factories_.find(name); | 494 FactoryMap::iterator iter = factories_.find(name); |
559 DCHECK(iter != factories_.end()); | 495 DCHECK(iter != factories_.end()); |
560 ExtensionFunction* function = iter->second(); | 496 ExtensionFunction* function = iter->second(); |
561 function->set_name(name); | 497 function->set_name(name); |
562 return function; | 498 return function; |
563 } | 499 } |
564 | |
565 const char kAccessDenied[] = "access denied"; | |
566 const char kQuotaExceeded[] = "quota exceeded"; | |
567 | |
568 void LogSuccess(const Extension* extension, | |
569 const ExtensionHostMsg_Request_Params& params) { | |
570 ExtensionActivityLog* extension_activity_log = | |
571 ExtensionActivityLog::GetInstance(); | |
572 if (extension_activity_log->HasObservers(extension)) { | |
573 std::string call_signature = params.name + "("; | |
574 ListValue::const_iterator it = params.arguments.begin(); | |
575 for (; it != params.arguments.end(); ++it) { | |
576 std::string arg; | |
577 JSONStringValueSerializer serializer(&arg); | |
578 if (serializer.SerializeAndOmitBinaryValues(**it)) { | |
579 if (it != params.arguments.begin()) | |
580 call_signature += ", "; | |
581 call_signature += arg; | |
582 } | |
583 } | |
584 call_signature += ")"; | |
585 | |
586 extension_activity_log->Log( | |
587 extension, | |
588 ExtensionActivityLog::ACTIVITY_EXTENSION_API_CALL, | |
589 call_signature); | |
590 } | |
591 } | |
592 | |
593 void LogFailure(const Extension* extension, | |
594 const std::string& func_name, | |
595 const char* reason) { | |
596 ExtensionActivityLog* extension_activity_log = | |
597 ExtensionActivityLog::GetInstance(); | |
598 if (extension_activity_log->HasObservers(extension)) { | |
599 extension_activity_log->Log( | |
600 extension, | |
601 ExtensionActivityLog::ACTIVITY_EXTENSION_API_BLOCK, | |
602 func_name + ": " + reason); | |
603 } | |
604 } | |
605 | |
606 }; // namespace | |
607 | |
608 // ExtensionFunctionDispatcher ------------------------------------------------- | |
609 | |
610 void ExtensionFunctionDispatcher::GetAllFunctionNames( | |
611 std::vector<std::string>* names) { | |
612 FactoryRegistry::GetInstance()->GetAllNames(names); | |
613 } | |
614 | |
615 bool ExtensionFunctionDispatcher::OverrideFunction( | |
616 const std::string& name, ExtensionFunctionFactory factory) { | |
617 return FactoryRegistry::GetInstance()->OverrideFunction(name, factory); | |
618 } | |
619 | |
620 void ExtensionFunctionDispatcher::ResetFunctions() { | |
621 FactoryRegistry::GetInstance()->ResetFunctions(); | |
622 } | |
623 | |
624 // static | |
625 void ExtensionFunctionDispatcher::DispatchOnIOThread( | |
626 ExtensionInfoMap* extension_info_map, | |
627 void* profile, | |
628 int render_process_id, | |
629 base::WeakPtr<ChromeRenderMessageFilter> ipc_sender, | |
630 int routing_id, | |
631 const ExtensionHostMsg_Request_Params& params) { | |
632 const Extension* extension = | |
633 extension_info_map->extensions().GetByID(params.extension_id); | |
634 | |
635 scoped_refptr<ExtensionFunction> function( | |
636 CreateExtensionFunction(params, extension, render_process_id, | |
637 extension_info_map->process_map(), profile, | |
638 ipc_sender, routing_id)); | |
639 if (!function) { | |
640 LogFailure(extension, params.name, kAccessDenied); | |
641 return; | |
642 } | |
643 | |
644 IOThreadExtensionFunction* function_io = | |
645 function->AsIOThreadExtensionFunction(); | |
646 if (!function_io) { | |
647 NOTREACHED(); | |
648 return; | |
649 } | |
650 function_io->set_ipc_sender(ipc_sender, routing_id); | |
651 function_io->set_extension_info_map(extension_info_map); | |
652 function->set_include_incognito( | |
653 extension_info_map->IsIncognitoEnabled(extension->id())); | |
654 | |
655 ExtensionsQuotaService* quota = extension_info_map->quota_service(); | |
656 if (quota->Assess(extension->id(), function, ¶ms.arguments, | |
657 base::TimeTicks::Now())) { | |
658 function->Run(); | |
659 LogSuccess(extension, params); | |
660 } else { | |
661 function->OnQuotaExceeded(); | |
662 LogFailure(extension, params.name, kQuotaExceeded); | |
663 } | |
664 } | |
665 | |
666 ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(Profile* profile, | |
667 Delegate* delegate) | |
668 : profile_(profile), | |
669 delegate_(delegate) { | |
670 } | |
671 | |
672 ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() { | |
673 } | |
674 | |
675 Browser* ExtensionFunctionDispatcher::GetCurrentBrowser( | |
676 RenderViewHost* render_view_host, bool include_incognito) { | |
677 Browser* browser = delegate_->GetBrowser(); | |
678 | |
679 // If the delegate has an associated browser, that is always the right answer. | |
680 if (browser) | |
681 return browser; | |
682 | |
683 // Otherwise, try to default to a reasonable browser. If |include_incognito| | |
684 // is true, we will also search browsers in the incognito version of this | |
685 // profile. Note that the profile may already be incognito, in which case | |
686 // we will search the incognito version only, regardless of the value of | |
687 // |include_incognito|. | |
688 Profile* profile = Profile::FromBrowserContext( | |
689 render_view_host->GetProcess()->GetBrowserContext()); | |
690 browser = BrowserList::FindAnyBrowser(profile, include_incognito); | |
691 | |
692 // NOTE(rafaelw): This can return NULL in some circumstances. In particular, | |
693 // a background_page onload chrome.tabs api call can make it into here | |
694 // before the browser is sufficiently initialized to return here. | |
695 // A similar situation may arise during shutdown. | |
696 // TODO(rafaelw): Delay creation of background_page until the browser | |
697 // is available. http://code.google.com/p/chromium/issues/detail?id=13284 | |
698 return browser; | |
699 } | |
700 | |
701 void ExtensionFunctionDispatcher::Dispatch( | |
702 const ExtensionHostMsg_Request_Params& params, | |
703 RenderViewHost* render_view_host) { | |
704 ExtensionService* service = profile()->GetExtensionService(); | |
705 extensions::ProcessMap* process_map = service->process_map(); | |
706 if (!service || !process_map) | |
707 return; | |
708 | |
709 const Extension* extension = service->extensions()->GetByID( | |
710 params.extension_id); | |
711 if (!extension) | |
712 extension = service->extensions()->GetHostedAppByURL(ExtensionURLInfo( | |
713 WebSecurityOrigin::createFromString(params.source_origin), | |
714 params.source_url)); | |
715 | |
716 scoped_refptr<ExtensionFunction> function( | |
717 CreateExtensionFunction(params, extension, | |
718 render_view_host->GetProcess()->GetID(), | |
719 *(service->process_map()), | |
720 profile(), render_view_host, | |
721 render_view_host->GetRoutingID())); | |
722 if (!function) { | |
723 LogFailure(extension, params.name, kAccessDenied); | |
724 return; | |
725 } | |
726 | |
727 UIThreadExtensionFunction* function_ui = | |
728 function->AsUIThreadExtensionFunction(); | |
729 if (!function_ui) { | |
730 NOTREACHED(); | |
731 return; | |
732 } | |
733 function_ui->SetRenderViewHost(render_view_host); | |
734 function_ui->set_dispatcher(AsWeakPtr()); | |
735 function_ui->set_profile(profile_); | |
736 function->set_include_incognito(service->CanCrossIncognito(extension)); | |
737 | |
738 ExtensionsQuotaService* quota = service->quota_service(); | |
739 if (quota->Assess(extension->id(), function, ¶ms.arguments, | |
740 base::TimeTicks::Now())) { | |
741 // See crbug.com/39178. | |
742 ExternalProtocolHandler::PermitLaunchUrl(); | |
743 | |
744 function->Run(); | |
745 LogSuccess(extension, params); | |
746 } else { | |
747 function->OnQuotaExceeded(); | |
748 LogFailure(extension, params.name, kQuotaExceeded); | |
749 } | |
750 | |
751 // We only adjust the keepalive count for UIThreadExtensionFunction for | |
752 // now, largely for simplicity's sake. This is OK because currently, only | |
753 // the webRequest API uses IOThreadExtensionFunction, and that API is not | |
754 // compatible with lazy background pages. | |
755 profile()->GetExtensionProcessManager()->IncrementLazyKeepaliveCount( | |
756 extension); | |
757 } | |
758 | |
759 void ExtensionFunctionDispatcher::OnExtensionFunctionCompleted( | |
760 const Extension* extension) { | |
761 profile()->GetExtensionProcessManager()->DecrementLazyKeepaliveCount( | |
762 extension); | |
763 } | |
764 | |
765 // static | |
766 ExtensionFunction* ExtensionFunctionDispatcher::CreateExtensionFunction( | |
767 const ExtensionHostMsg_Request_Params& params, | |
768 const Extension* extension, | |
769 int requesting_process_id, | |
770 const extensions::ProcessMap& process_map, | |
771 void* profile, | |
772 IPC::Message::Sender* ipc_sender, | |
773 int routing_id) { | |
774 if (!extension) { | |
775 LOG(ERROR) << "Specified extension does not exist."; | |
776 SendAccessDenied(ipc_sender, routing_id, params.request_id); | |
777 return NULL; | |
778 } | |
779 | |
780 if (ExtensionAPI::GetInstance()->IsPrivileged(params.name) && | |
781 !process_map.Contains(extension->id(), requesting_process_id)) { | |
782 LOG(ERROR) << "Extension API called from incorrect process " | |
783 << requesting_process_id | |
784 << " from URL " << params.source_url.spec(); | |
785 SendAccessDenied(ipc_sender, routing_id, params.request_id); | |
786 return NULL; | |
787 } | |
788 | |
789 if (!extension->HasAPIPermission(params.name)) { | |
790 LOG(ERROR) << "Extension " << extension->id() << " does not have " | |
791 << "permission to function: " << params.name; | |
792 SendAccessDenied(ipc_sender, routing_id, params.request_id); | |
793 return NULL; | |
794 } | |
795 | |
796 ExtensionFunction* function = | |
797 FactoryRegistry::GetInstance()->NewFunction(params.name); | |
798 function->SetArgs(¶ms.arguments); | |
799 function->set_source_url(params.source_url); | |
800 function->set_request_id(params.request_id); | |
801 function->set_has_callback(params.has_callback); | |
802 function->set_user_gesture(params.user_gesture); | |
803 function->set_extension(extension); | |
804 function->set_profile_id(profile); | |
805 return function; | |
806 } | |
807 | |
808 // static | |
809 void ExtensionFunctionDispatcher::SendAccessDenied( | |
810 IPC::Message::Sender* ipc_sender, int routing_id, int request_id) { | |
811 ipc_sender->Send(new ExtensionMsg_Response( | |
812 routing_id, request_id, false, std::string(), | |
813 "Access to extension API denied.")); | |
814 } | |
OLD | NEW |