diff options
author | mpcomplete@chromium.org <mpcomplete@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-12 17:57:09 +0000 |
---|---|---|
committer | mpcomplete@chromium.org <mpcomplete@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-12 17:57:09 +0000 |
commit | b2a74caaf15e234b5e6c03151d12c28a319a06cb (patch) | |
tree | 4b0f606a6ee117a66e66e4b7fb6546d9943bd425 | |
parent | 293caa54c215807abcef6a31aa34ca0e49e51581 (diff) | |
download | chromium_src-b2a74caaf15e234b5e6c03151d12c28a319a06cb.zip chromium_src-b2a74caaf15e234b5e6c03151d12c28a319a06cb.tar.gz chromium_src-b2a74caaf15e234b5e6c03151d12c28a319a06cb.tar.bz2 |
Add chrome.extension.inIncognitoTab for content scripts.
Review URL: http://codereview.chromium.org/822008
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@41446 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/renderer_host/browser_render_process_host.cc | 5 | ||||
-rwxr-xr-x | chrome/common/extensions/api/extension_api.json | 9 | ||||
-rw-r--r-- | chrome/common/extensions/docs/extension.html | 87 | ||||
-rw-r--r-- | chrome/common/render_messages_internal.h | 10 | ||||
-rw-r--r-- | chrome/renderer/render_thread.cc | 11 | ||||
-rw-r--r-- | chrome/renderer/render_thread.h | 9 | ||||
-rw-r--r-- | chrome/renderer/resources/renderer_extension_bindings.js | 8 | ||||
-rw-r--r-- | chrome/renderer/user_script_slave.cc | 15 | ||||
-rw-r--r-- | chrome/renderer/user_script_slave.h | 7 | ||||
-rw-r--r-- | chrome/test/data/extensions/api_test/incognito/apis/background.html | 35 | ||||
-rw-r--r-- | chrome/test/data/extensions/api_test/incognito/apis/manifest.json | 2 |
11 files changed, 172 insertions, 26 deletions
diff --git a/chrome/browser/renderer_host/browser_render_process_host.cc b/chrome/browser/renderer_host/browser_render_process_host.cc index 02849ae..d4a42df 100644 --- a/chrome/browser/renderer_host/browser_render_process_host.cc +++ b/chrome/browser/renderer_host/browser_render_process_host.cc @@ -627,8 +627,7 @@ void BrowserRenderProcessHost::SendUserScriptsUpdate( } if (base::SharedMemory::IsHandleValid(handle_for_process)) { - Send(new ViewMsg_UserScripts_UpdatedScripts(handle_for_process, - profile()->IsOffTheRecord())); + Send(new ViewMsg_UserScripts_UpdatedScripts(handle_for_process)); } } @@ -924,6 +923,8 @@ void BrowserRenderProcessHost::OnProcessLaunched() { // Now that the process is created, set its backgrounding accordingly. SetBackgrounded(backgrounded_); + Send(new ViewMsg_SetIsIncognitoProcess(profile()->IsOffTheRecord())); + InitVisitedLinks(); InitUserScripts(); InitExtensions(); diff --git a/chrome/common/extensions/api/extension_api.json b/chrome/common/extensions/api/extension_api.json index 3058338..901cfc1 100755 --- a/chrome/common/extensions/api/extension_api.json +++ b/chrome/common/extensions/api/extension_api.json @@ -50,6 +50,15 @@ "properties": { "message": { "type": "string", "description": "Description of the error that has taken place." } } + }, + "inIncognitoTab": { + "type": "object", + "optional": true, + "unprivileged": true, + "description": "True for content scripts running inside incognito tabs. Will be undefined for non-content scripts.", + "properties": { + "message": { "type": "boolean", "description": "True if the script is in an incongito tab." } + } } }, "functions": [ diff --git a/chrome/common/extensions/docs/extension.html b/chrome/common/extensions/docs/extension.html index fdb5fde..6ae20ef 100644 --- a/chrome/common/extensions/docs/extension.html +++ b/chrome/common/extensions/docs/extension.html @@ -211,6 +211,8 @@ <ol> <li> <a href="#property-lastError">lastError</a> + </li><li> + <a href="#property-inIncognitoTab">inIncognitoTab</a> </li> </ol> </li> @@ -411,6 +413,91 @@ For details, see </dl> </dd> </div> + </div><div> + <a name="property-inIncognitoTab"></a> + <h4>inIncognitoTab</h4> + <div class="summary"> + <!-- Note: intentionally longer 80 columns --> + <span>chrome.extension.</span><span>inIncognitoTab</span> + </div> + <div> + <dt> + <var>inIncognitoTab</var> + <em> + + <!-- TYPE --> + <div style="display:inline"> + ( + <span class="optional">optional</span> + <span id="typeTemplate"> + <span style="display: none; "> + <a> Type</a> + </span> + <span> + <span style="display: none; "> + array of <span><span></span></span> + </span> + <span>object</span> + </span> + </span> + ) + </div> + + </em> + </dt> + <dd class="todo" style="display: none; "> + Undocumented. + </dd> + <dd>True for content scripts running inside incognito tabs. Will be undefined for non-content scripts.</dd> + + <!-- OBJECT PROPERTIES --> + <dd> + <dl> + <div> + <div> + <dt> + <var>message</var> + <em> + + <!-- TYPE --> + <div style="display:inline"> + ( + <span class="optional" style="display: none; ">optional</span> + <span id="typeTemplate"> + <span style="display: none; "> + <a> Type</a> + </span> + <span> + <span style="display: none; "> + array of <span><span></span></span> + </span> + <span>boolean</span> + </span> + </span> + ) + </div> + + </em> + </dt> + <dd class="todo" style="display: none; "> + Undocumented. + </dd> + <dd>True if the script is in an incongito tab.</dd> + + <!-- OBJECT PROPERTIES --> + <dd style="display: none; "> + <dl> + <div> + <div> + </div> + </div> + </dl> + </dd> + </div> + </div> + </dl> + </dd> + </div> </div> </div> <!-- /apiGroup --> diff --git a/chrome/common/render_messages_internal.h b/chrome/common/render_messages_internal.h index c20fd88..7d126dc 100644 --- a/chrome/common/render_messages_internal.h +++ b/chrome/common/render_messages_internal.h @@ -250,9 +250,8 @@ IPC_BEGIN_MESSAGES(View) // Notification that the user scripts have been updated. It has one // SharedMemoryHandle argument consisting of the pickled script data. This // handle is valid in the context of the renderer. - IPC_MESSAGE_CONTROL2(ViewMsg_UserScripts_UpdatedScripts, - base::SharedMemoryHandle, - bool /* only_inject_incognito */) + IPC_MESSAGE_CONTROL1(ViewMsg_UserScripts_UpdatedScripts, + base::SharedMemoryHandle) // Sent when the user wants to search for a word on the page (find in page). IPC_MESSAGE_ROUTED3(ViewMsg_Find, @@ -948,6 +947,11 @@ IPC_BEGIN_MESSAGES(View) int /* code */, std::string /* message */) + // Sent on process startup to indicate whether this process is running in + // incognito mode. + IPC_MESSAGE_CONTROL1(ViewMsg_SetIsIncognitoProcess, + bool /* is_incognito_processs */) + IPC_END_MESSAGES(View) diff --git a/chrome/renderer/render_thread.cc b/chrome/renderer/render_thread.cc index 7ad062e..3d63405 100644 --- a/chrome/renderer/render_thread.cc +++ b/chrome/renderer/render_thread.cc @@ -210,6 +210,7 @@ void RenderThread::Init() { std::string type_str = CommandLine::ForCurrentProcess()->GetSwitchValueASCII( switches::kProcessType); is_extension_process_ = type_str == switches::kExtensionProcess; + is_incognito_process_ = false; suspend_webkit_shared_timer_ = true; notify_webkit_of_modal_loop_ = true; did_notify_webkit_of_modal_loop_ = false; @@ -455,10 +456,9 @@ void RenderThread::OnSetZoomLevelForCurrentHost(const std::string& host, RenderView::ForEach(&zoomer); } -void RenderThread::OnUpdateUserScripts( - base::SharedMemoryHandle scripts, bool only_inject_incognito) { +void RenderThread::OnUpdateUserScripts(base::SharedMemoryHandle scripts) { DCHECK(base::SharedMemory::IsHandleValid(scripts)) << "Bad scripts handle"; - user_script_slave_->UpdateScripts(scripts, only_inject_incognito); + user_script_slave_->UpdateScripts(scripts); UpdateActiveExtensions(); } @@ -517,6 +517,7 @@ void RenderThread::OnControlMessageReceived(const IPC::Message& msg) { OnSetContentSettingsForCurrentHost) IPC_MESSAGE_HANDLER(ViewMsg_SetZoomLevelForCurrentHost, OnSetZoomLevelForCurrentHost) + IPC_MESSAGE_HANDLER(ViewMsg_SetIsIncognitoProcess, OnSetIsIncognitoProcess) IPC_MESSAGE_HANDLER(ViewMsg_SetNextPageID, OnSetNextPageID) IPC_MESSAGE_HANDLER(ViewMsg_SetCSSColors, OnSetCSSColors) // TODO(port): removed from render_messages_internal.h; @@ -923,3 +924,7 @@ void RenderThread::OnSpellCheckWordAdded(const std::string& word) { void RenderThread::OnSpellCheckEnableAutoSpellCorrect(bool enable) { spellchecker_->EnableAutoSpellCorrect(enable); } + +void RenderThread::OnSetIsIncognitoProcess(bool is_incognito_process) { + is_incognito_process_ = is_incognito_process; +} diff --git a/chrome/renderer/render_thread.h b/chrome/renderer/render_thread.h index 51d742e..77cd060 100644 --- a/chrome/renderer/render_thread.h +++ b/chrome/renderer/render_thread.h @@ -144,6 +144,8 @@ class RenderThread : public RenderThreadBase, bool is_extension_process() const { return is_extension_process_; } + bool is_incognito_process() const { return is_incognito_process_; } + // Do DNS prefetch resolution of a hostname. void Resolve(const char* name, size_t length); @@ -174,8 +176,7 @@ class RenderThread : public RenderThreadBase, void OnSetZoomLevelForCurrentHost(const std::string& host, int zoom_level); void OnSetContentSettingsForCurrentHost( const std::string& host, const ContentSettings& content_settings); - void OnUpdateUserScripts(base::SharedMemoryHandle table, - bool only_inject_incognito); + void OnUpdateUserScripts(base::SharedMemoryHandle table); void OnSetExtensionFunctionNames(const std::vector<std::string>& names); void OnPageActionsUpdated(const std::string& extension_id, const std::vector<std::string>& page_actions); @@ -190,6 +191,7 @@ class RenderThread : public RenderThreadBase, const std::string& extension_id, bool enabled); void OnSetNextPageID(int32 next_page_id); + void OnSetIsIncognitoProcess(bool is_incognito_process); void OnSetCSSColors(const std::vector<CSSColors::CSSColorMapping>& colors); void OnCreateNewView(const ViewMsg_New_Params& params); void OnTransferBitmap(const SkBitmap& bitmap, int resource_id); @@ -271,6 +273,9 @@ class RenderThread : public RenderThreadBase, // True if this renderer is running extensions. bool is_extension_process_; + // True if this renderer is incognito. + bool is_incognito_process_; + bool suspend_webkit_shared_timer_; bool notify_webkit_of_modal_loop_; bool did_notify_webkit_of_modal_loop_; diff --git a/chrome/renderer/resources/renderer_extension_bindings.js b/chrome/renderer/resources/renderer_extension_bindings.js index 1c69ffa..654a619 100644 --- a/chrome/renderer/resources/renderer_extension_bindings.js +++ b/chrome/renderer/resources/renderer_extension_bindings.js @@ -142,16 +142,16 @@ var chrome = chrome || {}; // This function is called on context initialization for both content scripts // and extension contexts. - chrome.initExtension = function(extensionId, warnOnPrivilegedApiAccess) { + chrome.initExtension = function(extensionId, warnOnPrivilegedApiAccess, + inIncognitoTab) { delete chrome.initExtension; chromeHidden.extensionId = extensionId; chrome.extension = chrome.extension || {}; - - // TODO(EXTENSIONS_DEPRECATED): chrome.self is obsolete. - // http://code.google.com/p/chromium/issues/detail?id=16356 chrome.self = chrome.extension; + chrome.extension.inIncognitoTab = inIncognitoTab; + // Events for when a message channel is opened to our extension. chrome.extension.onConnect = new chrome.Event(); chrome.extension.onConnectExternal = new chrome.Event(); diff --git a/chrome/renderer/user_script_slave.cc b/chrome/renderer/user_script_slave.cc index 33adfbd..66c15d1 100644 --- a/chrome/renderer/user_script_slave.cc +++ b/chrome/renderer/user_script_slave.cc @@ -16,6 +16,7 @@ #include "chrome/common/extensions/extension.h" #include "chrome/common/extensions/extension_constants.h" #include "chrome/renderer/extension_groups.h" +#include "chrome/renderer/render_thread.h" #include "googleurl/src/gurl.h" #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h" @@ -32,7 +33,7 @@ static const char kUserScriptTail[] = "\n})(window);"; // Sets up the chrome.extension module. This may be run multiple times per // context, but the init method deletes itself after the first time. static const char kInitExtension[] = - "if (chrome.initExtension) chrome.initExtension('%s', true);"; + "if (chrome.initExtension) chrome.initExtension('%s', true, %s);"; int UserScriptSlave::GetIsolatedWorldId(const std::string& extension_id) { @@ -68,10 +69,11 @@ void UserScriptSlave::GetActiveExtensions(std::set<std::string>* extension_ids) } } -bool UserScriptSlave::UpdateScripts(base::SharedMemoryHandle shared_memory, - bool only_inject_incognito) { +bool UserScriptSlave::UpdateScripts(base::SharedMemoryHandle shared_memory) { scripts_.clear(); + bool only_inject_incognito = RenderThread::current()->is_incognito_process(); + // Create the shared memory object (read only). shared_memory_.reset(new base::SharedMemory(shared_memory, true)); if (!shared_memory_.get()) @@ -134,9 +136,10 @@ bool UserScriptSlave::UpdateScripts(base::SharedMemoryHandle shared_memory, void UserScriptSlave::InsertInitExtensionCode( std::vector<WebScriptSource>* sources, const std::string& extension_id) { DCHECK(sources); - sources->insert(sources->begin(), - WebScriptSource(WebString::fromUTF8( - StringPrintf(kInitExtension, extension_id.c_str())))); + bool incognito = RenderThread::current()->is_incognito_process(); + sources->insert(sources->begin(), WebScriptSource(WebString::fromUTF8( + StringPrintf(kInitExtension, extension_id.c_str(), + incognito ? "true" : "false")))); } bool UserScriptSlave::InjectScripts(WebFrame* frame, diff --git a/chrome/renderer/user_script_slave.h b/chrome/renderer/user_script_slave.h index 4cbbfd2..cf8fb8d 100644 --- a/chrome/renderer/user_script_slave.h +++ b/chrome/renderer/user_script_slave.h @@ -31,11 +31,8 @@ class UserScriptSlave { // Returns the unique set of extension IDs this UserScriptSlave knows about. void GetActiveExtensions(std::set<std::string>* extension_ids); - // Update the parsed scripts from shared memory. If |only_inject_incognito| - // is true, we will only use the scripts that have been marked as enabled for - // incognito mode. - bool UpdateScripts(base::SharedMemoryHandle shared_memory, - bool only_inject_incognito); + // Update the parsed scripts from shared memory. + bool UpdateScripts(base::SharedMemoryHandle shared_memory); // Inject the appropriate scripts into a frame based on its URL. // TODO(aa): Extract a UserScriptFrame interface out of this to improve diff --git a/chrome/test/data/extensions/api_test/incognito/apis/background.html b/chrome/test/data/extensions/api_test/incognito/apis/background.html index ff13197..606f6b5 100644 --- a/chrome/test/data/extensions/api_test/incognito/apis/background.html +++ b/chrome/test/data/extensions/api_test/incognito/apis/background.html @@ -71,6 +71,41 @@ chrome.test.runTests([ chrome.tabs.remove(tab.id, pass()); })); })); + }, + + // Tests content script injection to verify that the script can tell its + // in incongnito. + function contentScriptTestIncognito() { + // The property is undefined outside of content scripts. + assertEq(undefined, chrome.extension.inIncognitoTab); + + var testUrl = "http://localhost:1337/files/extensions/test_file.html"; + + // Test that chrome.extension.inIncognitoTab is true for incognito tabs. + chrome.tabs.create({windowId: incognitoWindow.id, url: testUrl}, + pass(function(tab) { + chrome.tabs.executeScript(tab.id, + {code: 'document.title = chrome.extension.inIncognitoTab'}, + pass(function() { + assertEq(undefined, chrome.extension.lastError); + chrome.tabs.get(tab.id, pass(function(tab) { + assertEq("true", tab.title); + })); + })); + })); + + // ... and false for normal tabs. + chrome.tabs.create({windowId: normalWindow.id, url: testUrl}, + pass(function(tab) { + chrome.tabs.executeScript(tab.id, + {code: 'document.title = chrome.extension.inIncognitoTab'}, + pass(function() { + assertEq(undefined, chrome.extension.lastError); + chrome.tabs.get(tab.id, pass(function(tab) { + assertEq("false", tab.title); + })); + })); + })); } ]); </script> diff --git a/chrome/test/data/extensions/api_test/incognito/apis/manifest.json b/chrome/test/data/extensions/api_test/incognito/apis/manifest.json index 4781276..5bb9a63 100644 --- a/chrome/test/data/extensions/api_test/incognito/apis/manifest.json +++ b/chrome/test/data/extensions/api_test/incognito/apis/manifest.json @@ -3,5 +3,5 @@ "version": "0.1", "description": "test that an incognito extension behaves properly", "background_page": "background.html", - "permissions": ["tabs"] + "permissions": ["tabs", "http://*/*"] } |