Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(45)

Unified Diff: chrome/browser/dom_ui/chrome_url_data_manager_backend.h

Issue 6286131: Splits ChromeURLDataManager into 2 chunks:... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/dom_ui/chrome_url_data_manager_backend.h
===================================================================
--- chrome/browser/dom_ui/chrome_url_data_manager_backend.h (revision 73882)
+++ chrome/browser/dom_ui/chrome_url_data_manager_backend.h (working copy)
@@ -1,152 +1,86 @@
-// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#ifndef CHROME_BROWSER_DOM_UI_CHROME_URL_DATA_MANAGER_H_
-#define CHROME_BROWSER_DOM_UI_CHROME_URL_DATA_MANAGER_H_
+#ifndef CHROME_BROWSER_DOM_UI_CHROME_URL_DATA_MANAGER_BACKEND_H_
+#define CHROME_BROWSER_DOM_UI_CHROME_URL_DATA_MANAGER_BACKEND_H_
#pragma once
+#include "base/ref_counted.h"
+#include "base/synchronization/lock.h"
+#include "chrome/browser/dom_ui/chrome_url_data_manager.h"
+
#include <map>
#include <string>
+#include <vector>
-#include "base/singleton.h"
-#include "base/task.h"
-#include "base/ref_counted.h"
-#include "chrome/browser/browser_thread.h"
-
-class DictionaryValue;
class FilePath;
class GURL;
-class MessageLoop;
-class RefCountedMemory;
class URLRequestChromeJob;
namespace net {
class URLRequest;
class URLRequestJob;
-} // namespace net
+}
-// To serve dynamic data off of chrome: URLs, implement the
-// ChromeURLDataManager::DataSource interface and register your handler
-// with AddDataSource.
-
-// ChromeURLDataManager lives on the IO thread, so any interfacing with
-// it from the UI thread needs to go through an InvokeLater.
-class ChromeURLDataManager {
+// ChromeURLDataManagerBackend is used internally by ChromeURLDataManager (and
+// during shutdown). In most cases you can use the API in ChromeURLDataManager
+// and ignore this class.
+//
+// ChromeURLDataManagerBackend stores the DataSources and is responsible for
+// mapping URLRequests to DataSources. ChromeURLDataManagerBackend is used on
+// the IO thread (except during shutdown). ChromeURLDataManager ensures
+// ChromeURLDataManagerBackend is used on the IO thread.
+class ChromeURLDataManagerBackend
+ : public base::RefCountedThreadSafe<ChromeURLDataManagerBackend> {
public:
- // Returns the singleton instance.
- static ChromeURLDataManager* GetInstance();
-
typedef int RequestID;
- // A DataSource is an object that can answer requests for data
- // asynchronously. DataSources are collectively owned with refcounting smart
- // pointers and should never be deleted on the IO thread, since their calls
- // are handled almost always on the UI thread and there's a possibility of a
- // data race. The |DeleteOnUIThread| trait is used to enforce this.
- //
- // An implementation of DataSource should handle calls to
- // StartDataRequest() by starting its (implementation-specific) asynchronous
- // request for the data, then call SendResponse() to notify.
- class DataSource : public base::RefCountedThreadSafe<
- DataSource, BrowserThread::DeleteOnUIThread> {
- public:
- // See source_name_ and message_loop_ below for docs on these parameters.
- DataSource(const std::string& source_name,
- MessageLoop* message_loop);
+ ChromeURLDataManagerBackend();
- // Sent by the DataManager to request data at |path|. The source should
- // call SendResponse() when the data is available or if the request could
- // not be satisfied.
- virtual void StartDataRequest(const std::string& path,
- bool is_off_the_record,
- int request_id) = 0;
+ // Invoked to register the protocol factories.
+ static void Register();
willchan no longer on Chromium 2011/02/08 04:57:52 You should probably document that this has to be r
sky 2011/02/08 05:35:26 Done.
- // Return the mimetype that should be sent with this response, or empty
- // string to specify no mime type.
- virtual std::string GetMimeType(const std::string& path) const = 0;
+ // Invoked on the UI thread prior to shutdown.
+ static void PrepareForShutdown();
- // Report that a request has resulted in the data |bytes|.
- // If the request can't be satisfied, pass NULL for |bytes| to indicate
- // the request is over.
- virtual void SendResponse(int request_id, RefCountedMemory* bytes);
+ // Finishes shutdown so that the none of the existing
+ // ChromeURLDataManagerBackends reference any DataSources. This is invoked on
+ // the UI thread so that the DataSources can be destroyed on the UI thread.
+ static void CompleteShutdown();
- // Returns the MessageLoop on which the DataSource wishes to have
- // StartDataRequest called to handle the request for |path|. If the
- // DataSource does not care which thread StartDataRequest is called on,
- // this should return NULL. The default implementation always returns
- // message_loop_, which generally results in processing on the UI thread.
- // It may be beneficial to return NULL for requests that are safe to handle
- // directly on the IO thread. This can improve performance by satisfying
- // such requests more rapidly when there is a large amount of UI thread
- // contention.
- virtual MessageLoop* MessageLoopForRequestPath(const std::string& path)
- const;
+ // Adds a DataSource to the collection of data sources.
+ void AddDataSource(ChromeURLDataManager::DataSource* source);
- const std::string& source_name() const { return source_name_; }
-
- static void SetFontAndTextDirection(DictionaryValue* localized_strings);
-
- protected:
- friend class base::RefCountedThreadSafe<DataSource>;
- friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>;
- friend class DeleteTask<DataSource>;
-
- virtual ~DataSource();
-
- private:
- // The name of this source.
- // E.g., for favicons, this could be "favicon", which results in paths for
- // specific resources like "favicon/34" getting sent to this source.
- const std::string source_name_;
-
- // The MessageLoop for the thread where this DataSource lives.
- // Used to send messages to the DataSource.
- MessageLoop* message_loop_;
- };
-
- // Add a DataSource to the collection of data sources.
- // Because we don't track users of a given path, we can't know when it's
- // safe to remove them, so the added source effectively leaks.
- // This could be improved in the future but currently the users of this
- // interface are conceptually permanent registration anyway.
- // Adding a second DataSource with the same name clobbers the first.
- // NOTE: Calling this from threads other the IO thread must be done via
- // InvokeLater.
- void AddDataSource(scoped_refptr<DataSource> source);
- // Called during shutdown, before destruction of |BrowserThread|.
- void RemoveDataSourceForTest(const char* source_name); // For unit tests.
- void RemoveAllDataSources(); // For the browser.
-
// Add/remove a path from the collection of file sources.
// A file source acts like a file:// URL to the specified path.
// Calling this from threads other the IO thread must be done via
// InvokeLater.
void AddFileSource(const std::string& source_name, const FilePath& path);
- void RemoveFileSource(const std::string& source_name);
+ // DataSource invokes this. Sends the data to the URLRequest.
+ void DataAvailable(RequestID request_id, RefCountedMemory* bytes);
+
static net::URLRequestJob* Factory(net::URLRequest* request,
const std::string& scheme);
private:
+ friend class base::RefCountedThreadSafe<ChromeURLDataManagerBackend>;
friend class URLRequestChromeJob;
- friend struct DefaultSingletonTraits<ChromeURLDataManager>;
- ChromeURLDataManager();
- ~ChromeURLDataManager();
+ ~ChromeURLDataManagerBackend();
// Parse a URL into the components used to resolve its request.
- static void URLToRequest(const GURL& url,
- std::string* source,
- std::string* path);
+ void URLToRequest(const GURL& url, std::string* source, std::string* path);
// Translate a chrome resource URL into a local file path if there is one.
// Returns false if there is no file handler for this URL
- static bool URLToFilePath(const GURL& url, FilePath* file_path);
+ bool URLToFilePath(const GURL& url, FilePath* file_path);
// Called by the job when it's starting up.
// Returns false if |url| is not a URL managed by this object.
bool StartRequest(const GURL& url, URLRequestChromeJob* job);
+
// Remove a request from the list of pending requests.
void RemoveRequest(URLRequestChromeJob* job);
@@ -155,18 +89,24 @@
// up to date.
bool HasPendingJob(URLRequestChromeJob* job) const;
- // Sent by Request::SendResponse.
- void DataAvailable(RequestID request_id,
- scoped_refptr<RefCountedMemory> bytes);
+ // Resets the |backend_| of each DataSource and clears out |data_sources_|.
+ // This may be invoked on any thread.
+ void ReleaseDataSources();
// File sources of data, keyed by source name (e.g. "inspector").
typedef std::map<std::string, FilePath> FileSourceMap;
FileSourceMap file_sources_;
// Custom sources of data, keyed by source path (e.g. "favicon").
- typedef std::map<std::string, scoped_refptr<DataSource> > DataSourceMap;
+ typedef std::map<std::string,
willchan no longer on Chromium 2011/02/08 04:57:52 I know it's inconvenient to put them further away
sky 2011/02/08 05:35:26 I didn't realize that was part of the style guide.
+ scoped_refptr<ChromeURLDataManager::DataSource> > DataSourceMap;
+ // WARNING: this may be accessed from the IO or UI threads. Access must be
+ // guarded by |data_sources_lock_|.
DataSourceMap data_sources_;
+ // Lock used to access data sources.
+ base::Lock data_sources_lock_;
+
// All pending URLRequestChromeJobs, keyed by ID of the request.
// URLRequestChromeJob calls into this object when it's constructed and
// destructed to ensure that the pointers in this map remain valid.
@@ -175,17 +115,22 @@
// The ID we'll use for the next request we receive.
RequestID next_request_id_;
-};
-// Since we have a single global ChromeURLDataManager, we don't need to
-// grab a reference to it when creating Tasks involving it.
-DISABLE_RUNNABLE_METHOD_REFCOUNT(ChromeURLDataManager);
+ // Lock used to access |instances_| and |shutdown_instances_|.
+ static base::Lock* instances_lock_;
-// Register our special URL handler under our special URL scheme.
-// Must be done once at startup.
-void RegisterURLRequestChromeJob();
+ // ChromeURLDataManagerBackend instances. All access must be done with
+ // |instance_lock_| held. See comment in PrepareForShutdown for details.
+ typedef std::vector<ChromeURLDataManagerBackend*> Instances;
+ static Instances* instances_;
-// Undoes the registration done by RegisterURLRequestChromeJob.
-void UnregisterURLRequestChromeJob();
+ typedef std::vector<scoped_refptr<ChromeURLDataManagerBackend> >
+ LockedInstances;
+ // If non-null, we're in the process of shuting down. See comment in
+ // PrepareForShutdown for details.
+ static LockedInstances* shutdown_instances_;
-#endif // CHROME_BROWSER_DOM_UI_CHROME_URL_DATA_MANAGER_H_
+ DISALLOW_COPY_AND_ASSIGN(ChromeURLDataManagerBackend);
willchan no longer on Chromium 2011/02/08 04:57:52 You need basictypes.h for this header file.
sky 2011/02/08 05:35:26 Done.
+};
+
+#endif // CHROME_BROWSER_DOM_UI_CHROME_URL_DATA_MANAGER_BACKEND_H_

Powered by Google App Engine
This is Rietveld 408576698