15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CHROME_COMMON_CUSTOM_HANDLERS_PROTOCOL_HANDLER_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHROME_COMMON_CUSTOM_HANDLERS_PROTOCOL_HANDLER_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/values.h"
117dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "url/gurl.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
13cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// A single tuple of (protocol, url) that indicates how URLs of the
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// given protocol should be rewritten to be handled.
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ProtocolHandler {
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static ProtocolHandler CreateProtocolHandler(const std::string& protocol,
19cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                               const GURL& url);
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Creates a ProtocolHandler with fields from the dictionary. Returns an
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // empty ProtocolHandler if the input is invalid.
237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  static ProtocolHandler CreateProtocolHandler(
247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      const base::DictionaryValue* value);
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the dictionary value has all the necessary fields to
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // define a ProtocolHandler.
287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  static bool IsValidDict(const base::DictionaryValue* value);
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if this handler's url has the same origin as the given one.
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsSameOrigin(const ProtocolHandler& handler) const;
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Canonical empty ProtocolHandler.
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static const ProtocolHandler& EmptyProtocolHandler();
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Interpolates the given URL into the URL template of this handler.
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GURL TranslateUrl(const GURL& url) const;
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the handlers are considered equivalent when determining
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // if both handlers can be registered, or if a handler has previously been
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // ignored.
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsEquivalent(const ProtocolHandler& other) const;
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Encodes this protocol handler as a DictionaryValue. The caller is
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // responsible for deleting the returned value.
467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  base::DictionaryValue* Encode() const;
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string& protocol() const { return protocol_; }
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const GURL& url() const { return url_;}
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsEmpty() const {
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return protocol_.empty();
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if !defined(NDEBUG)
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a string representation suitable for use in debugging.
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string ToString() const;
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool operator==(const ProtocolHandler& other) const;
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool operator<(const ProtocolHandler& other) const;
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ProtocolHandler(const std::string& protocol,
66cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                  const GURL& url);
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ProtocolHandler();
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string protocol_;
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GURL url_;
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CHROME_COMMON_CUSTOM_HANDLERS_PROTOCOL_HANDLER_H_
74